diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae1c5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test + +dist \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..51e7ecd --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Michał Rączka + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..64d0bb5 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +go-cron +========= + +Simple golang wrapper over `github.com/robfig/cron` and `os/exec` as a cron replacement + +## usage + +`go-cron "* * * * * *" /bin/bash -c "echo 1"` diff --git a/bin/build b/bin/build new file mode 100755 index 0000000..a6e4048 --- /dev/null +++ b/bin/build @@ -0,0 +1,10 @@ +#!/bin/bash + + +cd /scheduler +go get +rm -rf dist +mkdir -p dist +cd ./dist +go build -o go-cron /scheduler/scheduler.go +tar -cf go-cron.tar.gz go-cron diff --git a/bin/docker b/bin/docker new file mode 100755 index 0000000..16b68da --- /dev/null +++ b/bin/docker @@ -0,0 +1,10 @@ +#!/bin/bash + +VOLUME="${DOCKER_REMOTE_PATH}${PWD#${DOCKER_CLIENT_PATH}}/:/scheduler" + +docker run -i -t \ + --rm \ + -w /scheduler \ + -v $VOLUME \ + michaloo/golangdev \ + $@ diff --git a/scheduler.go b/scheduler.go new file mode 100644 index 0000000..01ff782 --- /dev/null +++ b/scheduler.go @@ -0,0 +1,74 @@ +package main +import "os" +import "os/exec" +import "strings" +import "sync" +import "os/signal" +import "syscall" +import "github.com/robfig/cron" + +func execute(command string, args []string)(output string, e error) { + + println("executing:", command, strings.Join(args, " ")) + + cmd := exec.Command(command, args...) + out, err := cmd.Output() + + if err != nil { + return "", err + } + + return string(out), nil +} + +func create() (cr *cron.Cron, wgr *sync.WaitGroup) { + var schedule string = os.Args[1] + var command string = os.Args[2] + var args []string = os.Args[3:len(os.Args)] + + wg := &sync.WaitGroup{} + + c := cron.New() + println("new cron:", schedule) + + c.AddFunc(schedule, func() { + wg.Add(1) + out, err := execute(command, args) + wg.Done() + if err != nil { + println(err.Error()) + } + + println(out) + }) + + return c, wg +} + +func start(c *cron.Cron, wg *sync.WaitGroup) { + c.Start() +} + +func stop(c *cron.Cron, wg *sync.WaitGroup) { + println("Stopping") + c.Stop() + println("Waiting") + wg.Wait() + println("Exiting") + os.Exit(0) +} + +func main() { + + c, wg := create() + + go start(c, wg) + + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + println(<-ch) + + stop(c, wg) +} + + diff --git a/test/run b/test/run new file mode 100755 index 0000000..c0c6a5c --- /dev/null +++ b/test/run @@ -0,0 +1,3 @@ +#!/bin/bash + +go run ./scheduler.go "* * * * * *" /bin/bash -c "echo 1;"