-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresolver.go
151 lines (130 loc) · 3.87 KB
/
resolver.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cron_gql
//go:generate go run github.com/99designs/gqlgen
import (
"context"
"encoding/json"
"log"
"os"
"os/exec"
"time"
"github.com/dustin/go-humanize"
"github.com/robfig/cron/v3"
) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
// Resolver is...
type Resolver struct {
Cron *cron.Cron
RunningJobs map[int]Job
}
// Mutation is...
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
// Query is...
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
type mutationResolver struct{ *Resolver }
func (r *mutationResolver) AddJob(ctx context.Context, jobInput AddJobInput) (*Job, error) {
entryID, err := r.Cron.AddFunc(jobInput.CronExp, func() { execute(jobInput) })
job := Job{JobID: int(entryID), CronExp: jobInput.CronExp, RootDir: jobInput.RootDir, Cmd: jobInput.Cmd, Args: jobInput.Args, Tags: jobInput.Tags}
r.RunningJobs[job.JobID] = job
return &job, err
}
func (r *mutationResolver) RemoveJob(ctx context.Context, jobID int) (*Job, error) {
job := Job{}
entry := r.Cron.Entry(cron.EntryID(jobID))
if entry.Valid() {
r.Cron.Remove(cron.EntryID(jobID))
}
if runningJob, ok := r.RunningJobs[jobID]; ok {
marshalledJob, _ := json.Marshal(runningJob)
_ = json.Unmarshal(marshalledJob, &job)
delete(r.RunningJobs, jobID)
}
return job.humanizeTime(), nil
}
func (r *mutationResolver) RunJob(ctx context.Context, jobID int) (*Job, error) {
job := Job{}
entry := r.Cron.Entry(cron.EntryID(jobID))
if entry.Valid() {
go entry.Job.Run()
job = r.RunningJobs[jobID]
lastRunTime, nextRunTime, forcedRunTime := int(entry.Prev.Unix()), int(entry.Next.Unix()), int(time.Now().Unix())
job.LastScheduledTime = &lastRunTime
job.NextScheduledTime = &nextRunTime
job.LastForcedTime = &forcedRunTime
r.RunningJobs[jobID] = *job.humanizeTime()
}
return &job, nil
}
type queryResolver struct{ *Resolver }
func (r *queryResolver) Jobs(ctx context.Context, input *JobsInput) ([]*Job, error) {
jobs := []*Job{}
for _, entry := range r.Cron.Entries() {
job, lastRunTime, nextRunTime := r.RunningJobs[int(entry.ID)], int(entry.Prev.Unix()), int(entry.Next.Unix())
job.LastScheduledTime = &lastRunTime
job.NextScheduledTime = &nextRunTime
job.humanizeTime()
if input != nil && (len(input.Tags) > 0 || input.JobID != nil) {
if input.JobID != nil { // jobID input takes precedence over tag input
if *input.JobID == int(entry.ID) {
jobs = append(jobs, &job)
break
}
} else if job.matchTags(input.Tags) {
jobs = append(jobs, &job)
}
} else { // return all jobs
jobs = append(jobs, &job)
}
}
return jobs, nil
}
func (r *Job) matchTags(tagsToCheck []*string) bool {
result := false
tagMap := make(map[string]bool)
for _, jobTag := range r.Tags {
tagMap[*jobTag] = true
}
for _, tag := range tagsToCheck {
if tagMap[*tag] {
result = true
break
}
}
return result
}
func (r *Job) humanizeTime() *Job {
if r.LastScheduledTime != nil {
lastScheduledRun := humanize.Time(time.Unix(int64(*r.LastScheduledTime), 0))
r.LastScheduledRun = &lastScheduledRun
}
if r.NextScheduledTime != nil {
nextScheduledRun := humanize.Time(time.Unix(int64(*r.NextScheduledTime), 0))
r.NextScheduledRun = &nextScheduledRun
}
if r.LastForcedTime != nil {
lastForcedRun := humanize.Time(time.Unix(int64(*r.LastForcedTime), 0))
r.LastForcedRun = &lastForcedRun
}
return r
}
func execute(job AddJobInput) (string, error) {
log.Printf("Changing directory to '%s'", job.RootDir)
err := os.Chdir(job.RootDir)
if err != nil {
log.Printf("%s", err)
}
var args []string
for _, v := range job.Args {
args = append(args, *v)
}
log.Printf("Executing command '%s' with arguments '%s'", job.Cmd, args)
out, err := exec.Command(job.Cmd, args...).CombinedOutput()
if err != nil {
log.Println(err)
}
output := string(out[:])
log.Println(output)
return output, err
}