├── .gitignore ├── LICENSE ├── README.md ├── bin └── build ├── crane.yml ├── go-cron.go └── test └── run /.gitignore: -------------------------------------------------------------------------------- 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: -------------------------------------------------------------------------------- 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: -------------------------------------------------------------------------------- 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"` 9 | -------------------------------------------------------------------------------- /bin/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | go get 5 | 6 | rm -rf dist 7 | mkdir -p dist 8 | 9 | cd ./dist 10 | 11 | go build -o go-cron ../go-cron.go 12 | 13 | tar -cf go-cron.tar.gz go-cron 14 | -------------------------------------------------------------------------------- /crane.yml: -------------------------------------------------------------------------------- 1 | containers: 2 | go-cron: 3 | image: michaloo/golangdev 4 | run: 5 | volume: 6 | - ".:/go-cron" 7 | interactive: true 8 | tty: true 9 | workdir: "/go-cron" 10 | entrypoint: /bin/bash 11 | cmd: 12 | - -c 13 | - "bash" 14 | rm: true 15 | -------------------------------------------------------------------------------- /go-cron.go: -------------------------------------------------------------------------------- 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)() { 11 | 12 | println("executing:", command, strings.Join(args, " ")) 13 | 14 | cmd := exec.Command(command, args...) 15 | 16 | cmd.Stdout = os.Stdout 17 | cmd.Stderr = os.Stderr 18 | 19 | cmd.Run() 20 | 21 | cmd.Wait() 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 | execute(command, args) 37 | wg.Done() 38 | }) 39 | 40 | return c, wg 41 | } 42 | 43 | func start(c *cron.Cron, wg *sync.WaitGroup) { 44 | c.Start() 45 | } 46 | 47 | func stop(c *cron.Cron, wg *sync.WaitGroup) { 48 | println("Stopping") 49 | c.Stop() 50 | println("Waiting") 51 | wg.Wait() 52 | println("Exiting") 53 | os.Exit(0) 54 | } 55 | 56 | func main() { 57 | 58 | c, wg := create() 59 | 60 | go start(c, wg) 61 | 62 | ch := make(chan os.Signal, 1) 63 | signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) 64 | println(<-ch) 65 | 66 | stop(c, wg) 67 | } 68 | 69 | 70 | -------------------------------------------------------------------------------- /test/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | go run ./go-cron.go "* * * * * *" /bin/bash -c "echo 1;" 4 | --------------------------------------------------------------------------------