Public | Automated Build

Last pushed: a year ago
Short Description
arsenal images
Full Description

Arsenal

Arsenal is a task scheduler written by Golang. You can use it to manage your cron job.

Examples

Cron job

This is a demo code, which using BashRunner print a value into a file every second.

package main

import (
    "github.com/cloudfly/arsenal"
    "time"
)

func main() {
    job := arsenal.NewJob(arsenal.BashRunner{"echo 3.1415926 >> /tmp/test"}) // let it print 3.1415926
    trigger := arsenal.NewTrigger(arsenal.GenTrigger{"* * * * * *"}).WithJob(job)
        trigger.Activate()
        defer trigger.Inactivate()
    time.Sleep(time.Second * 3) // waitint for 3 second
        // cat /tmp/test check the content
}
// Content in /tmp/test:
// 3.1415926
// 3.1415926
// 3.1415926

Job chain

Arsenal job support listeners, you can create a listener which can run another job or do something else when handling some event.
You can also using Job.WithJob() to simply let another job auto run when Job emit the specific event.

job2 := arsenal.NewJob(arsenal.BashRunner{}).WithValue("command", "echo 2.71828 >> /tmp/test") // let it print 2.71828
jobMain := arsenal.NewJob(arsenal.BashRunner{}).
        WithValue("command", "echo 3.1415926 >> /tmp/test").
        WithJob(job2) // run job2 when jobMain run succeeded

// create a new trigger to fire jobMain
trigger := arsenal.NewTrigger(arsenal.GenTrigger{}).WithValue("rule", "* * * * * *").WithJob(jobMain)
trigger.Activate()
defer trigger.Inactivate()

time.Sleep(time.Second * 3)

// Content in /tmp/test:
// 3.1415926
// 2.71828
// 3.1415926
// 2.71828
// 3.1415926
// 2.71828

Repeat Rule

Except Cron rule, GenTrigger also support another rule. It like ISO8601's repeat interval setting, but having a little different. Check the docs for details.

trigger := arsenal.NewTrigger(arsenal.GenTrigger{}).WithValue("rule", "R/P1s")

// same with

trigger := arsenal.NewTrigger(arsenal.GenTrigger{}).WithValue("rule", "* * * * * *")

Plugin support

By default, only BashRunner, GenTrigger and LogListener supported.
You can also write a Runner or Notifier or Listener plugin in a independent program.
please read the docs for detail.

The Runner plugin demo:

package main

import (
    "fmt"
    "golang.org/x/net/context"
    "github.com/cloudfly/arsenal/plugin"
    "os"
)

type TestRunner struct{}

func (runner TestRunner) Run(ctx context.Context, actx *plugin.Context) (*plugin.Data, error) {
        // do something
        return &plugin.Data{map[string]string{}}, nil
}

func main() {
    runner := plugin.NewRunner(TestRunner{})
    if err := runner.Serve(); err != nil {
        panic(err)
    }
}

Then in the host program:

runner := arsenal.NewRunnerPlugin("/path/of/plugin/program", "args")
job := arsenal.New(runner)

Arsenal daemon

Command line tool arsenal can run arsenal as a daemon. It serve a RPC service.
User can add module(plugin) into daemon and using these to create job or triggers.

Install:

go install github.com/cloudfly/arsenal/cmd/arsenal

You can also create your own arsenal daemon by using http package

More Examples

License

Deployd is released under the Apache License.

Docker Pull Command
Owner
cloudfly
Source Repository

Comments (0)