Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaloo committed Aug 21, 2014
0 parents commit d8c8e55
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"`
10 changes: 10 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions bin/docker
Original file line number Diff line number Diff line change
@@ -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 \
$@
74 changes: 74 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
@@ -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)
}


3 changes: 3 additions & 0 deletions test/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

go run ./scheduler.go "* * * * * *" /bin/bash -c "echo 1;"

0 comments on commit d8c8e55

Please sign in to comment.