Skip to content

Commit d8c8e55

Browse files
committedAug 21, 2014
Initial commit
0 parents  commit d8c8e55

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed
 

‎.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
25+
dist

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Michał Rączka
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
go-cron
2+
=========
3+
4+
Simple golang wrapper over `github.com/robfig/cron` and `os/exec` as a cron replacement
5+
6+
## usage
7+
8+
`go-cron "* * * * * *" /bin/bash -c "echo 1"`

‎bin/build

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
4+
cd /scheduler
5+
go get
6+
rm -rf dist
7+
mkdir -p dist
8+
cd ./dist
9+
go build -o go-cron /scheduler/scheduler.go
10+
tar -cf go-cron.tar.gz go-cron

‎bin/docker

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
VOLUME="${DOCKER_REMOTE_PATH}${PWD#${DOCKER_CLIENT_PATH}}/:/scheduler"
4+
5+
docker run -i -t \
6+
--rm \
7+
-w /scheduler \
8+
-v $VOLUME \
9+
michaloo/golangdev \
10+
$@

‎scheduler.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
import "os"
3+
import "os/exec"
4+
import "strings"
5+
import "sync"
6+
import "os/signal"
7+
import "syscall"
8+
import "github.com/robfig/cron"
9+
10+
func execute(command string, args []string)(output string, e error) {
11+
12+
println("executing:", command, strings.Join(args, " "))
13+
14+
cmd := exec.Command(command, args...)
15+
out, err := cmd.Output()
16+
17+
if err != nil {
18+
return "", err
19+
}
20+
21+
return string(out), nil
22+
}
23+
24+
func create() (cr *cron.Cron, wgr *sync.WaitGroup) {
25+
var schedule string = os.Args[1]
26+
var command string = os.Args[2]
27+
var args []string = os.Args[3:len(os.Args)]
28+
29+
wg := &sync.WaitGroup{}
30+
31+
c := cron.New()
32+
println("new cron:", schedule)
33+
34+
c.AddFunc(schedule, func() {
35+
wg.Add(1)
36+
out, err := execute(command, args)
37+
wg.Done()
38+
if err != nil {
39+
println(err.Error())
40+
}
41+
42+
println(out)
43+
})
44+
45+
return c, wg
46+
}
47+
48+
func start(c *cron.Cron, wg *sync.WaitGroup) {
49+
c.Start()
50+
}
51+
52+
func stop(c *cron.Cron, wg *sync.WaitGroup) {
53+
println("Stopping")
54+
c.Stop()
55+
println("Waiting")
56+
wg.Wait()
57+
println("Exiting")
58+
os.Exit(0)
59+
}
60+
61+
func main() {
62+
63+
c, wg := create()
64+
65+
go start(c, wg)
66+
67+
ch := make(chan os.Signal, 1)
68+
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
69+
println(<-ch)
70+
71+
stop(c, wg)
72+
}
73+
74+

‎test/run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
go run ./scheduler.go "* * * * * *" /bin/bash -c "echo 1;"

0 commit comments

Comments
 (0)
Please sign in to comment.