cron

package
v3.15.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: Apache-2.0, Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

This package provides scheduled(by interval) service for jobs.

IntervalService

The main service used to manage/start/stop job you want to execute continuously.

Time Interval

As "IntervalConfig" defined, there are "duration"s for the period of job execution:

InitialDelay - The delay duration before the first execution
FixedDelay - The delay duration before the next execution(after the complete of previous execution)
ErrorDelay - The delay duration before the next execution(after the complete of previous execution which is failed)

By default(all of the durations are 0), your job would be executed one by another.

Job

A "job" is a object implements "Job" interface. You could use "ProcJob" as functional paradigm.

The "AutoJob()" is an out-of-box function to convert your partial-implementation object to a "Job" object.

newJob := AutoJob(new(YourStruct))
service.Add("worker-1", &IntervalConfig{ }, newJob)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLoggerLevel

func SetLoggerLevel(level string)

Types

type IntervalConfig

type IntervalConfig struct {
	// The delay time before first execution
	InitialDelay time.Duration
	// The delay time between executions(after the job is finished)
	FixedDelay time.Duration
	// The delay time for next job if the last execution gave error
	//
	// If the value <= 0(no duration), the value of "FixedDelay" would be used instead.
	ErrorDelay time.Duration
	// contains filtered or unexported fields
}

Configuration of interval jobs

type IntervalService

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

func NewIntervalService

func NewIntervalService() *IntervalService

func (*IntervalService) Add

func (s *IntervalService) Add(name string, config *IntervalConfig, targetJob Job)

Adds a job with worker name.

The same name of worker would be overrode.

func (*IntervalService) GetWorkerProfile

func (s *IntervalService) GetWorkerProfile(name string) *JobProfile

Gets the profiling information after a job has started.

Once the job is stopped, whatever by "Stop()" or "StopWorker()", the information of profiling would be abandoned.

This method gives "nil" the job is not running.

Example
newService := NewIntervalService()
newService.Add(
	"worker-33", &IntervalConfig{FixedDelay: 200 * time.Millisecond},
	ProcJob(func() error {
		return nil
	}),
)

newService.Start()
defer newService.Stop()

time.Sleep(time.Second)
profile := newService.GetWorkerProfile("worker-33")

match, _ := regexp.MatchString("Called: \\d+", fmt.Sprintf("Called: %d", profile.Called))

fmt.Printf("Match /Called: \\d+/: %v", match)
Output:

Match /Called: \d+/: true

func (*IntervalService) Start

func (s *IntervalService) Start()

Starts all of the added jobs

Example
var message = ""

newService := NewIntervalService()
newService.Add(
	"worker-1", &IntervalConfig{FixedDelay: 500 * time.Millisecond},
	ProcJob(func() error {
		message = "Get Called 1"
		return nil
	}),
)

newService.Start()
defer newService.Stop()

time.Sleep(time.Second)

fmt.Println("Get Called 1")
Output:

Get Called 1

func (*IntervalService) StartWorker

func (s *IntervalService) StartWorker(name string) bool

Starts a worker by name

This method returns "true" if the job is started effectively.

Example
var message = ""

newService := NewIntervalService()
newService.Add(
	"worker-2", &IntervalConfig{FixedDelay: 500 * time.Millisecond},
	ProcJob(func() error {
		message = "Get Called 2"
		return nil
	}),
)

newService.StartWorker("worker-2")
defer newService.StopWorker("worker-2")

time.Sleep(time.Second)

fmt.Println(message)
Output:

Get Called 2

func (*IntervalService) Stop

func (s *IntervalService) Stop()

Stops all of the added jobs

func (*IntervalService) StopWorker

func (s *IntervalService) StopWorker(name string) bool

Stops a worker by name

This method returns "true" if the job is started effectively.

type Job

type Job interface {
	JobLifecycle
	// Main work to be executed of a job
	Do() error
}

Defines all of the actions of a job

func AutoJob

func AutoJob(v interface{}) Job

Converts any object to Job object, this function would check the signature of "Job" interface.

This function "Do() error" is **mandatory**.

Other methods of life-cycle are optional.

func EmptyJob

func EmptyJob() Job

Constructs a job, which does nothing.

type JobInstance

type JobInstance struct {
	BeforeJobStartImpl  func()
	BeforeJobStopImpl   func()
	AfterJobStoppedImpl func()
	DoImpl              func() error
}

As struct-typed of a "Job"

func (*JobInstance) AfterJobStopped

func (j *JobInstance) AfterJobStopped()

Delegates to "obj.AfterJobStoppedImpl". This is optional.

func (*JobInstance) BeforeJobStart

func (j *JobInstance) BeforeJobStart()

Delegates to "obj.BeforeJobStartImpl". This is optional.

func (*JobInstance) BeforeJobStop

func (j *JobInstance) BeforeJobStop()

Delegates to "obj.BeforeJobStopImpl". This is optional.

func (*JobInstance) Do

func (j *JobInstance) Do() error

Delegates to "obj.DoImpl". This is **mandatory**.

type JobLifecycle

type JobLifecycle interface {
	// Gets called before the job starts
	BeforeJobStart()
	// Gets called before the job is going to be stopped
	//
	// This function get called asynchronously.
	BeforeJobStop()
	// Gets called after the job has been stopped
	AfterJobStopped()
}

Defines the events of life cycle on a job

type JobProfile

type JobProfile struct {
	// The times of the job get executed
	Called int
	// The times of the job's executions are failed
	Failed int
}

Represents the profile of job.

type ProcJob

type ProcJob func() error

As function-typed of a "job"

func (ProcJob) AfterJobStopped

func (p ProcJob) AfterJobStopped()

Implementation of "Job" interface. Do nothing.

func (ProcJob) BeforeJobStart

func (p ProcJob) BeforeJobStart()

Implementation of "Job" interface. Do nothing.

func (ProcJob) BeforeJobStop

func (p ProcJob) BeforeJobStop()

Implementation of "Job" interface. Do nothing.

func (ProcJob) Do

func (p ProcJob) Do() error

Delegates to the body of this function type.

Jump to

Keyboard shortcuts

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