Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"errors"
"fmt"
"net/url"
"time"

"github.com/telkomdev/tob/config"
"github.com/telkomdev/tob/services/dummy"
"github.com/telkomdev/tob/services/mongodb"
"github.com/telkomdev/tob/services/mysqldb"
"github.com/telkomdev/tob/services/postgres"
"github.com/telkomdev/tob/services/redisdb"
"github.com/telkomdev/tob/services/web"
"net/url"
"time"
)

// Runner the tob runner
Expand Down Expand Up @@ -170,6 +171,8 @@ func healthCheck(n string, s Service, t *time.Ticker, waiter Waiter, notificator
resp := s.Ping()
respStr := string(resp)
if respStr == NotOk && s.IsRecover() {
// set last downtime
s.SetLastDownTimeNow()
// set recover to false
s.SetRecover(false)

Expand All @@ -189,7 +192,7 @@ func healthCheck(n string, s Service, t *time.Ticker, waiter Waiter, notificator

for _, notificator := range notificators {
if notificator.IsEnabled() {
err := notificator.Send(fmt.Sprintf("%s is UP", n))
err := notificator.Send(fmt.Sprintf("%s is UP. It was down for %s", n, s.GetDownTimeDiff()))
if err != nil {
Logger.Println(err)
}
Expand Down
6 changes: 6 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type Service interface {
// IsRecover will return recovered status
IsRecover() bool

// LastDownTime will set last down time of service to current time
SetLastDownTimeNow()

// GetDownTimeDiff will return down time service difference in minutes
GetDownTimeDiff() string

// SetCheckInterval will set check interval to service
SetCheckInterval(interval int)

Expand Down
16 changes: 16 additions & 0 deletions services/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package dummy
import (
"log"
"math/rand"
"time"

"github.com/telkomdev/tob/util"
)

// Dummy service
type Dummy struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -78,6 +82,18 @@ func (d *Dummy) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *Dummy) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *Dummy) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *Dummy) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
19 changes: 17 additions & 2 deletions services/mongodb/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package mongodb

import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"

"github.com/telkomdev/tob/util"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Mongo service
type Mongo struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -119,6 +122,18 @@ func (d *Mongo) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *Mongo) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *Mongo) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *Mongo) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
18 changes: 17 additions & 1 deletion services/mysqldb/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package mysqldb

import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"log"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/telkomdev/tob/util"
)

// MySQL service
type MySQL struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -106,6 +110,18 @@ func (d *MySQL) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *MySQL) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *MySQL) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *MySQL) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
18 changes: 17 additions & 1 deletion services/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package postgres

import (
"database/sql"
_ "github.com/lib/pq"
"log"
"time"

_ "github.com/lib/pq"
"github.com/telkomdev/tob/util"
)

// Postgres service
type Postgres struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -106,6 +110,18 @@ func (d *Postgres) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *Postgres) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *Postgres) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *Postgres) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
18 changes: 17 additions & 1 deletion services/redisdb/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package redisdb

import (
"context"
"github.com/redis/go-redis/v9"
"log"
"net/url"
"time"

"github.com/redis/go-redis/v9"
"github.com/telkomdev/tob/util"
)

// Redis service
type Redis struct {
url string
recovered bool
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -128,6 +132,18 @@ func (d *Redis) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *Redis) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *Redis) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *Redis) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
19 changes: 18 additions & 1 deletion services/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package web

import (
"fmt"
"github.com/telkomdev/tob/httpx"
"log"
"time"

"github.com/telkomdev/tob/httpx"
"github.com/telkomdev/tob/util"
)

// Web service
type Web struct {
url string
recovered bool
serviceName string
lastDownTime string
enabled bool
verbose bool
logger *log.Logger
Expand Down Expand Up @@ -92,6 +97,18 @@ func (d *Web) IsRecover() bool {
return d.recovered
}

// LastDownTime will set last down time of service to current time
func (d *Web) SetLastDownTimeNow() {
if d.recovered {
d.lastDownTime = time.Now().Format(util.YYMMDD)
}
}

// GetDownTimeDiff will return down time service difference in minutes
func (d *Web) GetDownTimeDiff() string {
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
}

// SetCheckInterval will set check interval to service
func (d *Web) SetCheckInterval(interval int) {
d.checkInterval = interval
Expand Down
29 changes: 29 additions & 0 deletions util/timedate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"fmt"
"time"
)

// Define constant YYMMDD as the date format used in the function
const (
YYMMDD = "2006/01/02 15:04:05"
)

// TimeDifference calculates the difference in minutes between two timestamps
// timeFrom and timeNow, passed as strings in the format defined in YYMMDD
// The function returns a string with the difference in minutes
func TimeDifference(timeFrom string, timeNow string) string {
parsedTimeFrom, err := time.Parse(YYMMDD, timeFrom)
if err != nil {
fmt.Printf("error: parsing time from: %v\n", err)
}

parsedTimeNow, err := time.Parse(YYMMDD, timeNow)
if err != nil {
fmt.Printf("error: parsing time now: %v\n", err)
}

diff := parsedTimeNow.Sub(parsedTimeFrom).Minutes()
return fmt.Sprintf("%d minutes", uint(diff))
}