Skip to content

Commit 044edf3

Browse files
authored
Merge pull request #1 from lutfailham96/feat-downtime
Add down time message
2 parents 3410b3f + 927fd65 commit 044edf3

File tree

9 files changed

+143
-9
lines changed

9 files changed

+143
-9
lines changed

runner.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/url"
8+
"time"
9+
710
"github.com/telkomdev/tob/config"
811
"github.com/telkomdev/tob/services/dummy"
912
"github.com/telkomdev/tob/services/mongodb"
1013
"github.com/telkomdev/tob/services/mysqldb"
1114
"github.com/telkomdev/tob/services/postgres"
1215
"github.com/telkomdev/tob/services/redisdb"
1316
"github.com/telkomdev/tob/services/web"
14-
"net/url"
15-
"time"
1617
)
1718

1819
// Runner the tob runner
@@ -170,6 +171,8 @@ func healthCheck(n string, s Service, t *time.Ticker, waiter Waiter, notificator
170171
resp := s.Ping()
171172
respStr := string(resp)
172173
if respStr == NotOk && s.IsRecover() {
174+
// set last downtime
175+
s.SetLastDownTimeNow()
173176
// set recover to false
174177
s.SetRecover(false)
175178

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

190193
for _, notificator := range notificators {
191194
if notificator.IsEnabled() {
192-
err := notificator.Send(fmt.Sprintf("%s is UP", n))
195+
err := notificator.Send(fmt.Sprintf("%s is UP. It was down for %s", n, s.GetDownTimeDiff()))
193196
if err != nil {
194197
Logger.Println(err)
195198
}

service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ type Service interface {
4747
// IsRecover will return recovered status
4848
IsRecover() bool
4949

50+
// LastDownTime will set last down time of service to current time
51+
SetLastDownTimeNow()
52+
53+
// GetDownTimeDiff will return down time service difference in minutes
54+
GetDownTimeDiff() string
55+
5056
// SetCheckInterval will set check interval to service
5157
SetCheckInterval(interval int)
5258

services/dummy/dummy.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package dummy
33
import (
44
"log"
55
"math/rand"
6+
"time"
7+
8+
"github.com/telkomdev/tob/util"
69
)
710

811
// Dummy service
912
type Dummy struct {
1013
url string
1114
recovered bool
15+
lastDownTime string
1216
enabled bool
1317
verbose bool
1418
logger *log.Logger
@@ -78,6 +82,18 @@ func (d *Dummy) IsRecover() bool {
7882
return d.recovered
7983
}
8084

85+
// LastDownTime will set last down time of service to current time
86+
func (d *Dummy) SetLastDownTimeNow() {
87+
if d.recovered {
88+
d.lastDownTime = time.Now().Format(util.YYMMDD)
89+
}
90+
}
91+
92+
// GetDownTimeDiff will return down time service difference in minutes
93+
func (d *Dummy) GetDownTimeDiff() string {
94+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
95+
}
96+
8197
// SetCheckInterval will set check interval to service
8298
func (d *Dummy) SetCheckInterval(interval int) {
8399
d.checkInterval = interval

services/mongodb/mongo.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ package mongodb
22

33
import (
44
"context"
5-
"go.mongodb.org/mongo-driver/mongo"
6-
"go.mongodb.org/mongo-driver/mongo/options"
75
"log"
86
"time"
7+
8+
"github.com/telkomdev/tob/util"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
911
)
1012

1113
// Mongo service
1214
type Mongo struct {
1315
url string
1416
recovered bool
17+
lastDownTime string
1518
enabled bool
1619
verbose bool
1720
logger *log.Logger
@@ -119,6 +122,18 @@ func (d *Mongo) IsRecover() bool {
119122
return d.recovered
120123
}
121124

125+
// LastDownTime will set last down time of service to current time
126+
func (d *Mongo) SetLastDownTimeNow() {
127+
if d.recovered {
128+
d.lastDownTime = time.Now().Format(util.YYMMDD)
129+
}
130+
}
131+
132+
// GetDownTimeDiff will return down time service difference in minutes
133+
func (d *Mongo) GetDownTimeDiff() string {
134+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
135+
}
136+
122137
// SetCheckInterval will set check interval to service
123138
func (d *Mongo) SetCheckInterval(interval int) {
124139
d.checkInterval = interval

services/mysqldb/mysql.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package mysqldb
22

33
import (
44
"database/sql"
5-
_ "github.com/go-sql-driver/mysql"
65
"log"
6+
"time"
7+
8+
_ "github.com/go-sql-driver/mysql"
9+
"github.com/telkomdev/tob/util"
710
)
811

912
// MySQL service
1013
type MySQL struct {
1114
url string
1215
recovered bool
16+
lastDownTime string
1317
enabled bool
1418
verbose bool
1519
logger *log.Logger
@@ -106,6 +110,18 @@ func (d *MySQL) IsRecover() bool {
106110
return d.recovered
107111
}
108112

113+
// LastDownTime will set last down time of service to current time
114+
func (d *MySQL) SetLastDownTimeNow() {
115+
if d.recovered {
116+
d.lastDownTime = time.Now().Format(util.YYMMDD)
117+
}
118+
}
119+
120+
// GetDownTimeDiff will return down time service difference in minutes
121+
func (d *MySQL) GetDownTimeDiff() string {
122+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
123+
}
124+
109125
// SetCheckInterval will set check interval to service
110126
func (d *MySQL) SetCheckInterval(interval int) {
111127
d.checkInterval = interval

services/postgres/postgres.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ package postgres
22

33
import (
44
"database/sql"
5-
_ "github.com/lib/pq"
65
"log"
6+
"time"
7+
8+
_ "github.com/lib/pq"
9+
"github.com/telkomdev/tob/util"
710
)
811

912
// Postgres service
1013
type Postgres struct {
1114
url string
1215
recovered bool
16+
lastDownTime string
1317
enabled bool
1418
verbose bool
1519
logger *log.Logger
@@ -106,6 +110,18 @@ func (d *Postgres) IsRecover() bool {
106110
return d.recovered
107111
}
108112

113+
// LastDownTime will set last down time of service to current time
114+
func (d *Postgres) SetLastDownTimeNow() {
115+
if d.recovered {
116+
d.lastDownTime = time.Now().Format(util.YYMMDD)
117+
}
118+
}
119+
120+
// GetDownTimeDiff will return down time service difference in minutes
121+
func (d *Postgres) GetDownTimeDiff() string {
122+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
123+
}
124+
109125
// SetCheckInterval will set check interval to service
110126
func (d *Postgres) SetCheckInterval(interval int) {
111127
d.checkInterval = interval

services/redisdb/redis.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package redisdb
22

33
import (
44
"context"
5-
"github.com/redis/go-redis/v9"
65
"log"
76
"net/url"
7+
"time"
8+
9+
"github.com/redis/go-redis/v9"
10+
"github.com/telkomdev/tob/util"
811
)
912

1013
// Redis service
1114
type Redis struct {
1215
url string
1316
recovered bool
17+
lastDownTime string
1418
enabled bool
1519
verbose bool
1620
logger *log.Logger
@@ -128,6 +132,18 @@ func (d *Redis) IsRecover() bool {
128132
return d.recovered
129133
}
130134

135+
// LastDownTime will set last down time of service to current time
136+
func (d *Redis) SetLastDownTimeNow() {
137+
if d.recovered {
138+
d.lastDownTime = time.Now().Format(util.YYMMDD)
139+
}
140+
}
141+
142+
// GetDownTimeDiff will return down time service difference in minutes
143+
func (d *Redis) GetDownTimeDiff() string {
144+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
145+
}
146+
131147
// SetCheckInterval will set check interval to service
132148
func (d *Redis) SetCheckInterval(interval int) {
133149
d.checkInterval = interval

services/web/web.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ package web
22

33
import (
44
"fmt"
5-
"github.com/telkomdev/tob/httpx"
65
"log"
6+
"time"
7+
8+
"github.com/telkomdev/tob/httpx"
9+
"github.com/telkomdev/tob/util"
710
)
811

912
// Web service
1013
type Web struct {
1114
url string
1215
recovered bool
16+
serviceName string
17+
lastDownTime string
1318
enabled bool
1419
verbose bool
1520
logger *log.Logger
@@ -92,6 +97,18 @@ func (d *Web) IsRecover() bool {
9297
return d.recovered
9398
}
9499

100+
// LastDownTime will set last down time of service to current time
101+
func (d *Web) SetLastDownTimeNow() {
102+
if d.recovered {
103+
d.lastDownTime = time.Now().Format(util.YYMMDD)
104+
}
105+
}
106+
107+
// GetDownTimeDiff will return down time service difference in minutes
108+
func (d *Web) GetDownTimeDiff() string {
109+
return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD))
110+
}
111+
95112
// SetCheckInterval will set check interval to service
96113
func (d *Web) SetCheckInterval(interval int) {
97114
d.checkInterval = interval

util/timedate.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// Define constant YYMMDD as the date format used in the function
9+
const (
10+
YYMMDD = "2006/01/02 15:04:05"
11+
)
12+
13+
// TimeDifference calculates the difference in minutes between two timestamps
14+
// timeFrom and timeNow, passed as strings in the format defined in YYMMDD
15+
// The function returns a string with the difference in minutes
16+
func TimeDifference(timeFrom string, timeNow string) string {
17+
parsedTimeFrom, err := time.Parse(YYMMDD, timeFrom)
18+
if err != nil {
19+
fmt.Printf("error: parsing time from: %v\n", err)
20+
}
21+
22+
parsedTimeNow, err := time.Parse(YYMMDD, timeNow)
23+
if err != nil {
24+
fmt.Printf("error: parsing time now: %v\n", err)
25+
}
26+
27+
diff := parsedTimeNow.Sub(parsedTimeFrom).Minutes()
28+
return fmt.Sprintf("%d minutes", uint(diff))
29+
}

0 commit comments

Comments
 (0)