taskmanager

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 16 Imported by: 0

README

Go TaskManager

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

type Cron struct {
	// contains filtered or unexported fields
}

Cron A Timer that fires at according to a cron expression. All expresion supported by `https://github.com/gorhill/cronexpr` are supported.

func NewCron

func NewCron(cronExpression string) (*Cron, error)

NewCron returns a Timer that fires at according to a cron expression. All expresion supported by `https://github.com/gorhill/cronexpr` are supported.

func (*Cron) Next

func (c *Cron) Next() (time.Time, bool)

Next Return Next fire time.

func (*Cron) Reschedule

func (c *Cron) Reschedule(d time.Duration)

type ExecutionMiddleWare

type ExecutionMiddleWare interface {
	PreHandler(s *Task) (MWResult, error)
	PostHandler(s *Task, err error) MWResult
	Reset(s *Task)
	Initilize(s *Task)
}

PreExecutionMiddleWare Interface for developing new Middleware Pre Executation Middleware is run before executing a job.

type Fixed

type Fixed struct {
	// contains filtered or unexported fields
}

Fixed A Timer that fires at a fixed duration intervals

func NewFixed

func NewFixed(duration time.Duration) (*Fixed, error)

NewFixed Returns Fixed Timer; A Timer that fires at a fixed duration intervals.

func (*Fixed) Next

func (f *Fixed) Next() (time.Time, bool)

Next Return Next fire time.

func (*Fixed) Reschedule

func (f *Fixed) Reschedule(t time.Duration)

type MWResult

type MWResult struct {
	Result MWResult_Op
}

type MWResult_Op

type MWResult_Op int
const (
	MWResult_Cancel MWResult_Op = iota
	MWResult_Defer
	MWResult_NextMW
)

type Once

type Once struct {
	// contains filtered or unexported fields
}

Once A timer that run ONCE after an optional specific delay.

func NewOnce

func NewOnce(d time.Duration) (*Once, error)

NewOnce Return a timer that trigger ONCE after `d` delay as soon as Timer is inquired for the next Run. Delay = 0 means the Timer return now(), aka as soon as time is inquired.

func NewOnceTime

func NewOnceTime(t time.Time) (*Once, error)

NewOnceTime Return a timer that trigger ONCE at `t` time.Time. If `t` is in the past at inquery time, timer will NOT run.

func (*Once) Next

func (o *Once) Next() (time.Time, bool)

Next Return Next Time OR a boolean indicating no more Next()(s)

func (*Once) Reschedule

func (o *Once) Reschedule(d time.Duration)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option to customize schedule behavior, check the sched.With*() functions that implement Option interface for the available options

func WithExecutationMiddleWare

func WithExecutationMiddleWare(handler ExecutionMiddleWare) Option

func WithLogger

func WithLogger(logger logr.Logger) Option

WithLogger Use the supplied Logger as the logger.

func WithRetryMiddleWare

func WithRetryMiddleWare(handler RetryMiddleware) Option

type RetryMiddleware

type RetryMiddleware interface {
	Handler(s *Task, prerun bool, e error) (retry RetryResult, err error)
	Reset(s *Task) (ok bool)
	Initilize(s *Task)
}

type RetryResult

type RetryResult struct {
	Result RetryResult_Op
	Delay  time.Duration
}

type RetryResult_Op

type RetryResult_Op int
const (
	RetryResult_Retry RetryResult_Op = iota
	RetryResult_NoRetry
	RetryResult_NextMW
)

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler manage one or more Schedule creating them using common options, enforcing unique IDs, and supply methods to Start / Stop all schedule(s).

func NewScheduler

func NewScheduler(opts ...Option) *Scheduler

NewScheduler Creates new Scheduler, opt Options are applied to *every* schedule added and created by this scheduler.

func (*Scheduler) Add

func (s *Scheduler) Add(ctx context.Context, id string, timer Timer, job func(context.Context), extraOpts ...Option) error

Add Create a new Task for` jobFunc func()` that will run according to `timer Timer` with the []Options of the Scheduler.

func (*Scheduler) GetAllSchedules

func (s *Scheduler) GetAllSchedules() (map[string]*Task, error)

GetAllSchedules Returns all Schedule's in the Scheduler

func (*Scheduler) GetSchedule

func (s *Scheduler) GetSchedule(id string) (*Task, error)

GetSchedule Returns a Schedule by ID from the Scheduler

func (*Scheduler) Start

func (s *Scheduler) Start(id string) error

Start Start the Schedule with the given id. Return error if no Schedule with the given id exist.

func (*Scheduler) StartAll

func (s *Scheduler) StartAll()

StartAll Start All Schedules managed by the Scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop(id string) error

Stop Stop the Schedule with the given id. Return error if no Schedule with the given id exist.

func (*Scheduler) StopAll

func (s *Scheduler) StopAll()

StopAll Stops All Schedules managed by the Scheduler concurrently, but will block until ALL of them have stopped.

type Task

type Task struct {

	// Logging Interface
	Logger logr.Logger

	// Context for Jobs
	Ctx context.Context
	// contains filtered or unexported fields
}

Task A Task is an object that wraps a Job (func(){}) and runs it on a schedule according to the supplied Timer; With the the ability to expose metrics, and write logs to indicate job health, state, and stats.

func NewSchedule

func NewSchedule(ctx context.Context, id string, timer Timer, jobFunc func(context.Context), opts ...Option) *Task

NewSchedule Create a new schedule for` jobFunc func()` that will run according to `timer Timer` with the supplied []Options

func (*Task) GetID

func (s *Task) GetID() string

func (*Task) GetNextRun

func (s *Task) GetNextRun() time.Time

func (*Task) Run

func (s *Task) Run()

func (*Task) Start

func (s *Task) Start()

Start Start the scheduler. Method is concurrent safe. Calling Start() have the following effects according to the

scheduler state:
	1. NEW: Start the Schedule; running the defined Job on the first Timer's Next() time.
	2. QUEUED: No Effect (and prints warning)
	3. STOPPED: Restart the schedule
	4. FINISHED: No Effect (and prints warning)

func (*Task) Stop

func (s *Task) Stop()

Stop stops the scheduler. Method is **Blocking** and concurrent safe. When called:

  1. Schedule will cancel all waiting scheduled jobs.
  2. Schedule will wait for all running jobs to finish. Calling Stop() has the following effects depending on the state of the schedule:
  3. NEW: No Effect
  4. QUEUED: Stop Schedule
  5. STOPPED: No Effect
  6. FINISHED: No Effect

type Timer

type Timer interface {
	Next() (next time.Time, done bool)
	Reschedule(delay time.Duration)
}

Timer is an Interface for a Timer object that is used by a Schedule to determine when to run the next run of a job. Timer need to implement the Next() method returning the time of the next Job run. Timer indicates that no jobs shall be scheduled anymore by returning done == true. The `next time.Time` returned with `done bool` == true IS IGNORED. Next() shall not return time in the past. Time in the past is reset to time.Now() at evaluation time in the scheduler.

type UpdateSignalOp_Type

type UpdateSignalOp_Type int

Directories

Path Synopsis
examples
middleware

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL