-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
219 lines (195 loc) · 5.04 KB
/
main.go
File metadata and controls
219 lines (195 loc) · 5.04 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"bufio"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/codegangsta/cli"
"github.com/robfig/cron"
)
var running bool
var mu = &sync.Mutex{}
func main() {
app := cli.NewApp()
app.Name = "docker-cron"
app.Usage = "used to run shell commands at specified intervals / times, based on cron syntax"
app.Version = "1.0"
app.Authors = []cli.Author{
cli.Author{
Name: "Daniel Baldwin",
},
}
app.Copyright = `
The MIT License (MIT)
Copyright (c) 2015 MasteryConnect
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "seconds",
Value: "*",
Usage: "seconds: 0-59, */10",
EnvVar: "DC_SECS",
},
cli.StringFlag{
Name: "minutes",
Value: "*",
Usage: "minutes: 0-59, */10",
EnvVar: "DC_MINS",
},
cli.StringFlag{
Name: "hours",
Value: "*",
Usage: "hours: 0-23, */10",
EnvVar: "DC_HOURS",
},
cli.StringFlag{
Name: "day-of-month",
Value: "*",
Usage: "day of month: 1-31",
EnvVar: "DC_DOM",
},
cli.StringFlag{
Name: "months",
Value: "*",
Usage: "month: 1-12 or JAN-DEC, */10",
EnvVar: "DC_MONTHS",
},
cli.StringFlag{
Name: "day-of-week",
Value: "*",
Usage: "day of week: 0-6 or SUN-SAT",
EnvVar: "DC_DOW",
},
cli.BoolFlag{
Name: "sync-jobs",
Usage: "should the jobs be run one at a time (true), or whenever they are scheduled",
EnvVar: "DC_SYNC",
},
}
app.Action = func(con *cli.Context) {
// Vars
var command string
// Checks
if len(con.Args()) > 0 {
command = strings.Join(con.Args(), " ")
} else {
log.Fatal("Not enough args, need a command to run.")
cli.ShowAppHelp(con)
}
// Ensure handling of SIGTERM and Interrupt
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
signal.Notify(signalChan, syscall.SIGTERM)
go func() {
<-signalChan
os.Exit(1)
}()
// Setup cron job
c := cron.New()
schedule := strings.Join([]string{
con.String("seconds"),
con.String("minutes"),
con.String("hours"),
con.String("day-of-month"),
con.String("months"),
con.String("day-of-week"),
}, " ")
log.Printf("Setup cron to run on schedule: %s\n", schedule)
c.AddFunc(schedule, func() {
// Run one at a time, syncFlag=true, or whenever scheduled
syncFlag := con.Bool("sync-jobs")
if runJob(syncFlag) {
defer jobDone(syncFlag)
log.Printf("Running cron on schedule: %s\n", schedule)
cmd := exec.Command("sh", "-c", command)
setupStdout(cmd)
setupStderr(cmd)
err := cmd.Start()
if err != nil {
log.Fatal("Error running command", err)
}
err = cmd.Wait()
if err != nil {
log.Fatal("Error waiting for command", err)
}
} else {
log.Println("A job is already running. The sync-jobs flag is true so we only run one at a time")
}
})
c.Start()
// Hold and let the cron job run
for {
time.Sleep(30 * time.Second)
}
}
app.Run(os.Args)
}
func setupStdout(cmd *exec.Cmd) {
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Fatal("Error creating stdoutpipe for command", err)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Println(scanner.Text())
}
}()
}
func setupStderr(cmd *exec.Cmd) {
cmdReader, err := cmd.StderrPipe()
if err != nil {
log.Fatal("Error creating stderrpipe for command", err)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Printf("ERR: %s\n", scanner.Text())
}
}()
}
// Should we run a job. If syncFlag is false, then we always run a job even if
// there is already one running. If syncFlag is true, then we only run a job
// if one is not already running
func runJob(syncFlag bool) bool {
if syncFlag {
mu.Lock()
defer mu.Unlock()
if running {
return false
} else {
running = true
return true
}
}
// Always run, even if there is already one running
return true
}
func jobDone(syncFlag bool) {
// We only need to change the running state if we have syncrhonous job runs
if syncFlag {
mu.Lock()
defer mu.Unlock()
running = false
}
}