-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd.tasker): add tasker for standalone usage as task daemon
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |