generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
135 additions
and
99 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 jobs | ||
|
||
import ( | ||
"github.com/robfig/cron/v3" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/config" | ||
cfgcompiler "github.com/FMotalleb/crontab-go/config/compiler" | ||
"github.com/FMotalleb/crontab-go/core/goutils" | ||
) | ||
|
||
func initEventSignal(schedulers []abstraction.Scheduler, logger *logrus.Entry) <-chan any { | ||
signals := make([]<-chan any, 0, len(schedulers)) | ||
for _, sh := range schedulers { | ||
signals = append(signals, sh.BuildTickChannel()) | ||
} | ||
logger.Trace("Signals Built") | ||
signal := goutils.Zip(signals...) | ||
return signal | ||
} | ||
|
||
func initTasks(job config.JobConfig, logger *logrus.Entry) ([]abstraction.Executable, []abstraction.Executable, []abstraction.Executable) { | ||
tasks := make([]abstraction.Executable, 0, len(job.Tasks)) | ||
doneHooks := make([]abstraction.Executable, 0, len(job.Hooks.Done)) | ||
failHooks := make([]abstraction.Executable, 0, len(job.Hooks.Failed)) | ||
for _, t := range job.Tasks { | ||
tasks = append(tasks, cfgcompiler.CompileTask(&t, logger)) | ||
} | ||
logger.Trace("Compiled Tasks") | ||
for _, t := range job.Hooks.Done { | ||
doneHooks = append(doneHooks, cfgcompiler.CompileTask(&t, logger)) | ||
} | ||
logger.Trace("Compiled Hooks.Done") | ||
for _, t := range job.Hooks.Failed { | ||
failHooks = append(failHooks, cfgcompiler.CompileTask(&t, logger)) | ||
} | ||
logger.Trace("Compiled Hooks.Fail") | ||
return tasks, doneHooks, failHooks | ||
} | ||
|
||
func initSchedulers(job config.JobConfig, cronInstance *cron.Cron, logger *logrus.Entry) []abstraction.Scheduler { | ||
schedulers := make([]abstraction.Scheduler, 0, len(job.Schedulers)) | ||
for _, sh := range job.Schedulers { | ||
schedulers = append(schedulers, cfgcompiler.CompileScheduler(&sh, cronInstance, logger)) | ||
} | ||
return schedulers | ||
} |
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 jobs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/robfig/cron/v3" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/cmd" | ||
"github.com/FMotalleb/crontab-go/config" | ||
"github.com/FMotalleb/crontab-go/ctxutils" | ||
) | ||
|
||
func InitializeJobs(log *logrus.Entry, cronInstance *cron.Cron) { | ||
for _, job := range cmd.CFG.Jobs { | ||
if !job.Enabled { | ||
log.Warnf("job %s is disabled", job.Name) | ||
continue | ||
} | ||
|
||
c := context.Background() | ||
c = context.WithValue(c, ctxutils.JobKey, job) | ||
|
||
logger := initLogger(c, log, job) | ||
|
||
if err := job.Validate(); err != nil { | ||
log.Panicf("failed to validate job (%s): %v", job.Name, err) | ||
} | ||
|
||
schedulers := initSchedulers(job, cronInstance, logger) | ||
logger.Trace("Schedulers initialized") | ||
|
||
tasks, doneHooks, failHooks := initTasks(job, logger) | ||
logger.Trace("Tasks initialized") | ||
|
||
signal := initEventSignal(schedulers, logger) | ||
|
||
go taskHandler(c, logger, signal, tasks, doneHooks, failHooks) | ||
logger.Trace("EventLoop initialized") | ||
} | ||
log.Debugln("Jobs Are Ready") | ||
} | ||
|
||
func initLogger(c context.Context, log *logrus.Entry, job config.JobConfig) *logrus.Entry { | ||
logger := log.WithContext(c).WithField("job.name", job.Name) | ||
logger.Trace("Initializing Job") | ||
return logger | ||
} |
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,35 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/FMotalleb/crontab-go/abstraction" | ||
"github.com/FMotalleb/crontab-go/ctxutils" | ||
) | ||
|
||
func taskHandler(c context.Context, logger *logrus.Entry, signal <-chan any, tasks []abstraction.Executable, doneHooks []abstraction.Executable, failHooks []abstraction.Executable) { | ||
logger.Debug("Spawning task handler") | ||
for range signal { | ||
logger.Trace("Signal Received") | ||
for _, task := range tasks { | ||
go executeTask(c, task, doneHooks, failHooks) | ||
} | ||
} | ||
} | ||
|
||
func executeTask(c context.Context, task abstraction.Executable, doneHooks []abstraction.Executable, failHooks []abstraction.Executable) { | ||
ctx := context.WithValue(c, ctxutils.TaskKey, task) | ||
err := task.Execute(ctx) | ||
switch err { | ||
case nil: | ||
for _, task := range doneHooks { | ||
_ = task.Execute(ctx) | ||
} | ||
default: | ||
for _, task := range failHooks { | ||
_ = task.Execute(ctx) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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