2019-12-16 17:24:40 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
"github.com/retailcrm/mg-transport-core/v2/core/logger"
|
2019-12-16 17:24:40 +03:00
|
|
|
)
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// JobFunc is empty func which should be executed in a parallel goroutine.
|
2021-12-01 15:40:23 +03:00
|
|
|
type JobFunc func(logger.Logger) error
|
2019-12-16 17:24:40 +03:00
|
|
|
|
2022-12-30 10:10:40 +03:00
|
|
|
// JobAfterCallback will be called after specific job is done.
|
|
|
|
// This can be used to run something after asynchronous job is done. The function will
|
|
|
|
// receive an error from the job which can be used to alter the callback behavior. If callback
|
|
|
|
// returns an error, it will be processed using job ErrorHandler.
|
|
|
|
// The callback won't be executed in case of panic. Panicked callback will be show in logs
|
|
|
|
// as if the job itself panicked, ergo, do not write jobs or callbacks that can panic,
|
|
|
|
// or process the panic by yourself.
|
|
|
|
type JobAfterCallback func(jobError error, log logger.Logger) error
|
|
|
|
|
2019-12-16 17:24:40 +03:00
|
|
|
// JobErrorHandler is a function to handle jobs errors. First argument is a job name.
|
2021-12-01 15:40:23 +03:00
|
|
|
type JobErrorHandler func(string, error, logger.Logger)
|
2019-12-16 17:24:40 +03:00
|
|
|
|
|
|
|
// JobPanicHandler is a function to handle jobs panics. First argument is a job name.
|
2021-12-01 15:40:23 +03:00
|
|
|
type JobPanicHandler func(string, interface{}, logger.Logger)
|
2019-12-16 17:24:40 +03:00
|
|
|
|
|
|
|
// Job represents single job. Regular job will be executed every Interval.
|
|
|
|
type Job struct {
|
|
|
|
Command JobFunc
|
|
|
|
ErrorHandler JobErrorHandler
|
|
|
|
PanicHandler JobPanicHandler
|
2021-12-01 15:40:23 +03:00
|
|
|
stopChannel chan bool
|
2019-12-16 17:24:40 +03:00
|
|
|
Interval time.Duration
|
2020-10-20 15:19:36 +03:00
|
|
|
writeLock sync.RWMutex
|
2019-12-17 10:39:48 +03:00
|
|
|
Regular bool
|
|
|
|
active bool
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2022-12-30 10:10:40 +03:00
|
|
|
//
|
|
|
|
// manager := NewJobManager().
|
|
|
|
// SetLogger(logger).
|
|
|
|
// SetLogging(false)
|
|
|
|
// _ = manager.RegisterJob("updateTokens", &Job{
|
|
|
|
// Command: func(log logger.Logger) error {
|
|
|
|
// // logic goes here...
|
|
|
|
// logger.Info("All tokens were updated successfully")
|
|
|
|
// return nil
|
|
|
|
// },
|
|
|
|
// ErrorHandler: DefaultJobErrorHandler(),
|
|
|
|
// PanicHandler: DefaultJobPanicHandler(),
|
|
|
|
// Interval: time.Hour * 3,
|
|
|
|
// Regular: true,
|
|
|
|
// })
|
|
|
|
// manager.Start()
|
2019-12-16 17:24:40 +03:00
|
|
|
type JobManager struct {
|
2021-12-01 15:40:23 +03:00
|
|
|
logger logger.Logger
|
|
|
|
nilLogger logger.Logger
|
2019-12-17 10:39:48 +03:00
|
|
|
jobs *sync.Map
|
|
|
|
enableLogging bool
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// getWrappedFunc wraps job into function.
|
2022-12-30 10:10:40 +03:00
|
|
|
func (j *Job) getWrappedFunc(name string, log logger.Logger) func(callback JobAfterCallback) {
|
|
|
|
return func(callback JobAfterCallback) {
|
2019-12-17 10:39:48 +03:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil && j.PanicHandler != nil {
|
|
|
|
j.PanicHandler(name, r, log)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-30 10:10:40 +03:00
|
|
|
err := j.Command(log)
|
|
|
|
if err != nil && j.ErrorHandler != nil {
|
2019-12-17 10:39:48 +03:00
|
|
|
j.ErrorHandler(name, err, log)
|
|
|
|
}
|
2022-12-30 10:10:40 +03:00
|
|
|
if callback != nil {
|
|
|
|
err := callback(err, log)
|
|
|
|
if j.ErrorHandler != nil {
|
|
|
|
j.ErrorHandler(name, err, log)
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// getWrappedTimerFunc returns job timer func to run in the separate goroutine.
|
2021-12-01 15:40:23 +03:00
|
|
|
func (j *Job) getWrappedTimerFunc(name string, log logger.Logger) func(chan bool) {
|
2019-12-17 13:50:30 +03:00
|
|
|
return func(stopChannel chan bool) {
|
2019-12-17 10:39:48 +03:00
|
|
|
for range time.NewTicker(j.Interval).C {
|
|
|
|
select {
|
2019-12-17 13:50:30 +03:00
|
|
|
case <-stopChannel:
|
2019-12-17 10:39:48 +03:00
|
|
|
return
|
|
|
|
default:
|
2022-12-30 10:10:40 +03:00
|
|
|
j.getWrappedFunc(name, log)(nil)
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// run job.
|
2021-12-01 15:40:23 +03:00
|
|
|
func (j *Job) run(name string, log logger.Logger) {
|
2020-10-20 15:19:36 +03:00
|
|
|
j.writeLock.RLock()
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
if j.Regular && j.Interval > 0 && !j.active {
|
2020-10-20 15:19:36 +03:00
|
|
|
j.writeLock.RUnlock()
|
|
|
|
defer j.writeLock.Unlock()
|
|
|
|
j.writeLock.Lock()
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
j.stopChannel = make(chan bool)
|
2019-12-17 13:50:30 +03:00
|
|
|
go j.getWrappedTimerFunc(name, log)(j.stopChannel)
|
2019-12-17 10:39:48 +03:00
|
|
|
j.active = true
|
2020-10-20 15:19:36 +03:00
|
|
|
} else {
|
|
|
|
j.writeLock.RUnlock()
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// stop running job.
|
2021-12-01 15:40:23 +03:00
|
|
|
func (j *Job) stop() {
|
2020-10-20 15:19:36 +03:00
|
|
|
j.writeLock.RLock()
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
if j.active && j.stopChannel != nil {
|
2020-10-20 15:19:36 +03:00
|
|
|
j.writeLock.RUnlock()
|
2019-12-17 13:50:30 +03:00
|
|
|
go func() {
|
2020-10-20 15:19:36 +03:00
|
|
|
defer j.writeLock.Unlock()
|
|
|
|
j.writeLock.Lock()
|
2019-12-17 13:50:30 +03:00
|
|
|
j.stopChannel <- true
|
|
|
|
j.active = false
|
|
|
|
}()
|
2020-10-20 15:19:36 +03:00
|
|
|
} else {
|
|
|
|
j.writeLock.RUnlock()
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// runOnce run job once.
|
2022-12-30 10:10:40 +03:00
|
|
|
func (j *Job) runOnce(name string, log logger.Logger, callback JobAfterCallback) {
|
|
|
|
go j.getWrappedFunc(name, log)(callback)
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// runOnceSync run job once in current goroutine.
|
2021-12-01 15:40:23 +03:00
|
|
|
func (j *Job) runOnceSync(name string, log logger.Logger) {
|
2022-12-30 10:10:40 +03:00
|
|
|
j.getWrappedFunc(name, log)(nil)
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// NewJobManager is a JobManager constructor.
|
2019-12-17 10:39:48 +03:00
|
|
|
func NewJobManager() *JobManager {
|
2021-12-01 15:40:23 +03:00
|
|
|
return &JobManager{jobs: &sync.Map{}, nilLogger: logger.NewNil()}
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultJobErrorHandler returns default error handler for a job.
|
2019-12-17 10:39:48 +03:00
|
|
|
func DefaultJobErrorHandler() JobErrorHandler {
|
2021-12-01 15:40:23 +03:00
|
|
|
return func(name string, err error, log logger.Logger) {
|
2019-12-17 10:39:48 +03:00
|
|
|
if err != nil && name != "" {
|
2021-12-01 15:40:23 +03:00
|
|
|
log.Errorf("Job `%s` errored with an error: `%s`", name, err.Error())
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 17:24:40 +03:00
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultJobPanicHandler returns default panic handler for a job.
|
2019-12-17 10:39:48 +03:00
|
|
|
func DefaultJobPanicHandler() JobPanicHandler {
|
2021-12-01 15:40:23 +03:00
|
|
|
return func(name string, recoverValue interface{}, log logger.Logger) {
|
2019-12-17 10:39:48 +03:00
|
|
|
if recoverValue != nil && name != "" {
|
2021-12-01 15:40:23 +03:00
|
|
|
log.Errorf("Job `%s` panicked with value: `%#v`", name, recoverValue)
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// SetLogger sets logger into JobManager.
|
2021-12-01 15:40:23 +03:00
|
|
|
func (j *JobManager) SetLogger(logger logger.Logger) *JobManager {
|
2019-12-16 17:24:40 +03:00
|
|
|
if logger != nil {
|
|
|
|
j.logger = logger
|
|
|
|
}
|
|
|
|
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// Logger returns logger.
|
|
|
|
func (j *JobManager) Logger() logger.Logger {
|
|
|
|
if !j.enableLogging {
|
|
|
|
return j.nilLogger
|
|
|
|
}
|
|
|
|
return j.logger
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// SetLogging enables or disables JobManager logging.
|
2019-12-16 17:24:40 +03:00
|
|
|
func (j *JobManager) SetLogging(enableLogging bool) *JobManager {
|
|
|
|
j.enableLogging = enableLogging
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// RegisterJob registers new job.
|
2019-12-17 10:39:48 +03:00
|
|
|
func (j *JobManager) RegisterJob(name string, job *Job) error {
|
|
|
|
if job == nil {
|
|
|
|
return errors.New("job shouldn't be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := j.FetchJob(name); ok {
|
|
|
|
return errors.New("job already exists")
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
j.jobs.Store(name, job)
|
2019-12-17 10:39:48 +03:00
|
|
|
|
2019-12-16 17:24:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
// UnregisterJob unregisters job if it's exists. Returns error if job doesn't exist.
|
|
|
|
func (j *JobManager) UnregisterJob(name string) error {
|
|
|
|
if i, ok := j.FetchJob(name); ok {
|
|
|
|
i.stop()
|
|
|
|
j.jobs.Delete(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// FetchJob fetches already exist job.
|
2019-12-17 10:39:48 +03:00
|
|
|
func (j *JobManager) FetchJob(name string) (value *Job, ok bool) {
|
2019-12-16 17:24:40 +03:00
|
|
|
if i, ok := j.jobs.Load(name); ok {
|
2019-12-17 12:16:54 +03:00
|
|
|
if job, ok := i.(*Job); ok {
|
2019-12-16 17:24:40 +03:00
|
|
|
return job, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
return &Job{}, false
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// UpdateJob updates job.
|
2019-12-17 10:39:48 +03:00
|
|
|
func (j *JobManager) UpdateJob(name string, job *Job) error {
|
2021-12-01 15:40:23 +03:00
|
|
|
if _, ok := j.FetchJob(name); ok {
|
2019-12-17 10:39:48 +03:00
|
|
|
_ = j.UnregisterJob(name)
|
2019-12-16 17:24:40 +03:00
|
|
|
return j.RegisterJob(name, job)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// RunJob starts provided regular job if it's exists.
|
|
|
|
// It runs asynchronously and error returns only of job wasn't executed at all.
|
2019-12-17 10:39:48 +03:00
|
|
|
func (j *JobManager) RunJob(name string) error {
|
2019-12-16 17:24:40 +03:00
|
|
|
if job, ok := j.FetchJob(name); ok {
|
2021-12-01 15:40:23 +03:00
|
|
|
job.run(name, j.Logger())
|
2019-12-17 10:39:48 +03:00
|
|
|
return nil
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
// StopJob stops provided regular regular job if it's exists.
|
|
|
|
func (j *JobManager) StopJob(name string) error {
|
|
|
|
if job, ok := j.FetchJob(name); ok {
|
|
|
|
job.stop()
|
|
|
|
return nil
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
// RunJobOnce starts provided job once if it exists. It's also async.
|
2022-12-30 10:10:40 +03:00
|
|
|
func (j *JobManager) RunJobOnce(name string, callback ...JobAfterCallback) error {
|
2019-12-17 10:39:48 +03:00
|
|
|
if job, ok := j.FetchJob(name); ok {
|
2022-12-30 10:10:40 +03:00
|
|
|
var cb JobAfterCallback
|
|
|
|
if len(callback) > 0 {
|
|
|
|
cb = callback[0]
|
|
|
|
}
|
|
|
|
job.runOnce(name, j.Logger(), cb)
|
2019-12-17 10:39:48 +03:00
|
|
|
return nil
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2022-12-30 10:10:40 +03:00
|
|
|
// RunJobsOnceSequentially will execute provided jobs asynchronously. It uses JobAfterCallback under the hood.
|
|
|
|
// You can prevent subsequent jobs from running using stopOnError flag.
|
|
|
|
func (j *JobManager) RunJobsOnceSequentially(names []string, stopOnError bool) error {
|
|
|
|
if len(names) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var chained JobAfterCallback
|
|
|
|
for i := len(names) - 1; i > 0; i-- {
|
|
|
|
i := i
|
|
|
|
if chained == nil {
|
|
|
|
chained = func(jobError error, log logger.Logger) error {
|
|
|
|
if jobError != nil && stopOnError {
|
|
|
|
return jobError
|
|
|
|
}
|
|
|
|
return j.RunJobOnce(names[i])
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
oldCallback := chained
|
|
|
|
chained = func(jobError error, log logger.Logger) error {
|
|
|
|
if jobError != nil && stopOnError {
|
|
|
|
return jobError
|
|
|
|
}
|
|
|
|
err := j.RunJobOnce(names[i], oldCallback)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return j.RunJobOnce(names[0], chained)
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
// RunJobOnceSync starts provided job once in current goroutine if job exists. Will wait for job to end it's work.
|
|
|
|
func (j *JobManager) RunJobOnceSync(name string) error {
|
|
|
|
if job, ok := j.FetchJob(name); ok {
|
2021-12-01 15:40:23 +03:00
|
|
|
job.runOnceSync(name, j.Logger())
|
2019-12-17 10:39:48 +03:00
|
|
|
return nil
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:39:48 +03:00
|
|
|
return fmt.Errorf("cannot find job `%s`", name)
|
2019-12-16 17:24:40 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Start all jobs in the manager.
|
2019-12-17 14:11:29 +03:00
|
|
|
func (j *JobManager) Start() {
|
|
|
|
j.jobs.Range(func(key, value interface{}) bool {
|
|
|
|
name := key.(string)
|
|
|
|
job := value.(*Job)
|
2021-12-01 15:40:23 +03:00
|
|
|
job.run(name, j.Logger())
|
2019-12-17 14:11:29 +03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|