-
Notifications
You must be signed in to change notification settings - Fork 2
/
job_stuck_service.go
59 lines (50 loc) · 1.8 KB
/
job_stuck_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package tasks
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/ottogroup/penelope/pkg/processor"
"github.com/ottogroup/penelope/pkg/repository"
"github.com/ottogroup/penelope/pkg/secret"
"go.opencensus.io/trace"
"strings"
)
type jobStuckService struct {
scheduleProcessor processor.ScheduleProcessor
ctx context.Context
}
func newJobsStuckService(ctxIn context.Context, credentialsProvider secret.SecretProvider) (*jobStuckService, error) {
ctx, span := trace.StartSpan(ctxIn, "newJobsStuckService")
defer span.End()
scheduleProcessor, err := processor.NewScheduleProcessor(ctx, credentialsProvider)
if err != nil {
return &jobStuckService{}, fmt.Errorf("could not instantiate new ScheduleProcessor: %s", err)
}
return &jobStuckService{scheduleProcessor: scheduleProcessor, ctx: ctx}, nil
}
func (j *jobStuckService) Run(ctxIn context.Context) {
ctx, span := trace.StartSpan(ctxIn, "(*jobStuckService).Run")
defer span.End()
deltaHours := 1
statuses := []repository.JobStatus{repository.NotScheduled, repository.Scheduled, repository.Pending}
jobs, err := j.scheduleProcessor.GetByStatusAndAfter(ctx, statuses, deltaHours)
if err != nil {
glog.Errorf("could not get list of jobs with status %v before %d hours: %s", statuses, deltaHours, err)
return
}
glog.Infof("[START] Checking stucked jobs")
if len(jobs) == 0 {
glog.Infof("[SUCCESS] No jobs stuck with status %v before %d hours", statuses, deltaHours)
return
}
glog.Infof("Alerting on %d stuck jobs in status %v before %d hours:", len(jobs), statuses, deltaHours)
logMessage := "[FAIL] stuck jobs:"
logMessage += strings.Join(toString(jobs), "|")
glog.Info(logMessage)
}
func toString(jobs []*repository.Job) (result []string) {
for _, job := range jobs {
result = append(result, job.String())
}
return result
}