mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 05:06:04 +03:00
method to start all regular jobs in manager
This commit is contained in:
parent
c3d24f3298
commit
d7bad94495
@ -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 {
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user