@@ -3,8 +3,10 @@ package incidentpoller
3
3
import (
4
4
"context"
5
5
"github.com/jackc/pgx/v5"
6
+ "github.com/metoro-io/statusphere/common/api"
6
7
"github.com/metoro-io/statusphere/common/db"
7
8
"github.com/metoro-io/statusphere/common/jobs/slack_webhook"
9
+ "github.com/metoro-io/statusphere/common/jobs/twitter_post"
8
10
"github.com/pkg/errors"
9
11
"github.com/riverqueue/river"
10
12
"go.uber.org/zap"
@@ -14,18 +16,20 @@ import (
14
16
// IncidentPoller is a poller that polls incidents from the database, if the jobs have not been started for them, then it starts them
15
17
16
18
type IncidentPoller struct {
17
- db * db.DbClient
18
- logger * zap.Logger
19
- riverClient * river.Client [pgx.Tx ]
20
- slackWebhookUrl string
19
+ db * db.DbClient
20
+ logger * zap.Logger
21
+ riverClient * river.Client [pgx.Tx ]
22
+ slackWebhookUrl string
23
+ twitterWebhookUrl string
21
24
}
22
25
23
- func NewIncidentPoller (db * db.DbClient , logger * zap.Logger , client * river.Client [pgx.Tx ], webhookUrl string ) * IncidentPoller {
26
+ func NewIncidentPoller (db * db.DbClient , logger * zap.Logger , client * river.Client [pgx.Tx ], slackWebhookUrl string , twitterWebhookUrl string ) * IncidentPoller {
24
27
return & IncidentPoller {
25
- db : db ,
26
- logger : logger ,
27
- riverClient : client ,
28
- slackWebhookUrl : webhookUrl ,
28
+ db : db ,
29
+ logger : logger ,
30
+ riverClient : client ,
31
+ slackWebhookUrl : slackWebhookUrl ,
32
+ twitterWebhookUrl : twitterWebhookUrl ,
29
33
}
30
34
}
31
35
@@ -64,11 +68,9 @@ func (p *IncidentPoller) pollInner() error {
64
68
p .logger .Info ("found incidents without jobs started" , zap .Int ("count" , len (incidents )))
65
69
66
70
jobArgs := make ([]river.InsertManyParams , 0 )
67
- for _ , incident := range incidents {
68
- if p .slackWebhookUrl == "" {
69
- continue
70
- }
71
71
72
+ var incidentsToProcess = make ([]api.Incident , 0 )
73
+ for _ , incident := range incidents {
72
74
// We only want to notify about incidents that have started in the last hour
73
75
// Otherwise, we will be sending notifications for incidents that have already been resolved
74
76
if incident .StartTime .Before (time .Now ().Add (- 1 * time .Hour )) {
@@ -78,12 +80,32 @@ func (p *IncidentPoller) pollInner() error {
78
80
if incident .Impact == "maintenance" {
79
81
continue
80
82
}
83
+
84
+ incidentsToProcess = append (incidentsToProcess , incident )
85
+ }
86
+
87
+ // Slack webhook notifications
88
+ for _ , incident := range incidentsToProcess {\
89
+ if p .slackWebhookUrl == "" {
90
+ continue
91
+ }
81
92
jobArgs = append (jobArgs , river.InsertManyParams {Args : slack_webhook.SlackWebhookArgs {
82
93
Incident : incident ,
83
94
WebhookUrl : p .slackWebhookUrl ,
84
95
}})
85
96
}
86
97
98
+ // Twitter post notifications
99
+ for _ , incident := range incidentsToProcess {
100
+ if p .twitterWebhookUrl == "" {
101
+ continue
102
+ }
103
+ jobArgs = append (jobArgs , river.InsertManyParams {Args : twitter_post.TwitterPostArgs {
104
+ WebhookUrl : p .twitterWebhookUrl ,
105
+ Incident : incident ,
106
+ }})
107
+ }
108
+
87
109
p .logger .Info ("starting to insert jobs" , zap .Int ("count" , len (jobArgs )))
88
110
// Start the jobs for each incident and update the database
89
111
if len (jobArgs ) != 0 {
0 commit comments