Skip to content

Commit

Permalink
Merge pull request #3 from odise/commandline_args
Browse files Browse the repository at this point in the history
Commandline args, HTTP port
  • Loading branch information
Jan Nabbefeld committed Mar 11, 2015
2 parents 609a669 + 60eb16c commit 6f160c2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
go-cron
=========

Simple golang wrapper over `github.com/robfig/cron` and `os/exec` as a cron replacement
Simple golang wrapper over `github.com/robfig/cron` and `os/exec` as a cron replacement.
Additionally the application opens a HTTP port that can be used as a healthcheck.

## usage

`go-cron "* * * * * *" /bin/bash -c "echo 1"`
`go-cron -s "* * * * * *" -p 8080 -- /bin/bash -c "echo 1"`

Check the healthcheck:

```
$ curl -v localhost:18080
* Rebuilt URL to: localhost:18080/
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 18080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:18080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Wed, 11 Mar 2015 12:59:07 GMT
< Content-Length: 237
<
{
"Running": {},
"Last": {
"Exit_status": 0,
"Stdout": "1\n",
"Stderr": "",
"ExitTime": "2015-03-11T13:59:05+01:00",
"Pid": 14420,
"StartingTime": "2015-03-11T13:59:05+01:00"
},
"Schedule": "*/5 * * * *"
* Connection #0 to host localhost left intact
}
```
52 changes: 46 additions & 6 deletions bin/go-cron.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
//"fmt"
"flag"
gocron "github.com/odise/go-cron"
"log"
"os"
Expand All @@ -12,19 +12,59 @@ import (
var build string

func main() {
log.Println("Running version:", build)
flagArgs, execArgs := splitArgs()
os.Args = flagArgs

var (
help = flag.Bool("h", false, "display usage")
port = flag.String("p", "18080", "bind healthcheck to a specific port, set to 0 to not open HTTP port at all")
schedule = flag.String("s", "* * * * *", "schedule the task the cron style")
)

flag.Parse()

if len(os.Args) < 3 {
log.Fatalf("run: go-cron <schedule> <command>")
if *help {
println("Usage of", os.Args[0], "(build", build, ")")
println(os.Args[0], " [ OPTIONS ] -- [ COMMAND ]")
flag.PrintDefaults()
os.Exit(1)
}
log.Println("Running version:", build)

c, wg := gocron.Create()
c, wg := gocron.Create(*schedule, execArgs[0], execArgs[1:len(execArgs)])

go gocron.Start(c)
go gocron.Http_server()
if *port != "0" {
go gocron.Http_server(*port)
}

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
println(<-ch)
gocron.Stop(c, wg)
}

func splitArgs() (flagArgs []string, execArgs []string) {

split := len(os.Args)

for idx, e := range os.Args {

if e == "--" {
split = idx
break
}

}

flagArgs = os.Args[0:split]

if split < len(os.Args) {
execArgs = os.Args[split+1 : len(os.Args)]
} else {
execArgs = []string{}
}

return flagArgs, execArgs

}
5 changes: 1 addition & 4 deletions go-cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ func execute(command string, args []string) {
Current_state.Last = run
}

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)]
func Create(schedule string, command string, args []string) (cr *cron.Cron, wgr *sync.WaitGroup) {

wg := &sync.WaitGroup{}

Expand Down
5 changes: 3 additions & 2 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
w.Write(js)
}

func Http_server() {
func Http_server(port string) {
log.Println("Opening port", port, "for health checking")
http.HandleFunc("/", handler)
err := http.ListenAndServe(":18080", nil)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
Expand Down

0 comments on commit 6f160c2

Please sign in to comment.