From d7bad94495c3653e558d4839bce76702dc1cb146 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 17 Dec 2019 14:11:29 +0300 Subject: [PATCH] method to start all regular jobs in manager --- core/job_manager.go | 26 +++++++++++++++++++++++++- core/job_manager_test.go | 17 +++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/core/job_manager.go b/core/job_manager.go index b489aad..c30d067 100644 --- a/core/job_manager.go +++ b/core/job_manager.go @@ -34,7 +34,21 @@ type Job struct { // JobManager controls jobs execution flow. Jobs can be added just for later use (e.g. JobManager can be used as // singleton), or jobs can be executed as regular jobs. Example initialization: -// TODO example initialization +// manager := NewJobManager(). +// SetLogger(logger). +// SetLogging(false) +// _ = manager.RegisterJob("updateTokens", &Job{ +// Command: func(logFunc JobLogFunc) error { +// // logic goes here... +// logFunc("All tokens were updated successfully", logging.INFO) +// return nil +// }, +// ErrorHandler: DefaultJobErrorHandler(), +// PanicHandler: DefaultJobPanicHandler(), +// Interval: time.Hour * 3, +// Regular: true, +// }) +// manager.Start() type JobManager struct { jobs *sync.Map enableLogging bool @@ -229,6 +243,16 @@ func (j *JobManager) RunJobOnceSync(name string) error { return fmt.Errorf("cannot find job `%s`", name) } +// Start all jobs in the manager +func (j *JobManager) Start() { + j.jobs.Range(func(key, value interface{}) bool { + name := key.(string) + job := value.(*Job) + job.run(name, j.log) + return true + }) +} + // log logs via logger or as plaintext func (j *JobManager) log(format string, severity logging.Level, args ...interface{}) { if !j.enableLogging { diff --git a/core/job_manager_test.go b/core/job_manager_test.go index 5e626c1..fc994dc 100644 --- a/core/job_manager_test.go +++ b/core/job_manager_test.go @@ -475,6 +475,23 @@ func (t *JobManagerTest) Test_UnregisterJobDoesntExist() { assert.EqualError(t.T(), err, "cannot find job `doesn't exist`") } +func (t *JobManagerTest) Test_Start() { + defer func() { + require.Nil(t.T(), recover()) + }() + + manager := NewJobManager() + _ = manager.RegisterJob("job", &Job{ + Command: func(logFunc JobLogFunc) error { + logFunc("alive!", logging.INFO) + return nil + }, + ErrorHandler: DefaultJobErrorHandler(), + PanicHandler: DefaultJobPanicHandler(), + }) + manager.Start() +} + func (t *JobManagerTest) Test_log() { defer func() { require.Nil(t.T(), recover())