Skip to content

Commit

Permalink
feat(cmd.tasker): add tasker for standalone usage as task daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed May 2, 2021
1 parent e7f1811 commit 0d99409
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/tasker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"flag"
"log"
"os"
"time"

"github.com/adhocore/gronx/pkg/tasker"
)

func main() {
opt := mustGetOption()
taskr := tasker.New(opt)

for _, task := range tasker.MustParseTaskfile(opt) {
taskr.Task(task.Expr, tasker.Taskify(task.Cmd, opt))
}

if opt.Until > 0 {
taskr.Until(time.Duration(opt.Until) * time.Minute)
}

taskr.Run()
}

func mustGetOption() tasker.Option {
var opt tasker.Option

flag.StringVar(&opt.File, "file", "", "The task file in crontab format")
flag.StringVar(&opt.Tz, "tz", "Local", "The timezone to use for tasks")
flag.StringVar(&opt.Shell, "shell", tasker.Shell()[0], "The shell to use for running tasks")
flag.StringVar(&opt.Out, "out", "", "The fullpath to file where output from tasks are sent to")
flag.BoolVar(&opt.Verbose, "verbose", false, "The verbose mode outputs as much as possible")
flag.Int64Var(&opt.Until, "until", 0, "The timeout for task daemon in minutes")
flag.Parse()

if opt.File == "" {
flag.Usage()
os.Exit(1)
}

if _, err := os.Stat(opt.File); err != nil {
log.Fatalf("can't read taskfile: %s", opt.File)
}

return opt
}

0 comments on commit 0d99409

Please sign in to comment.