scheduler

package
Version: v0.0.0-...-cae3ad5 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2017 License: MIT, MIT Imports: 5 Imported by: 0

README

scheduler

GoDoc Build Status Coverage Status

Job scheduling made easy.

Scheduler allows you to schedule recurrent jobs with an easy-to-read syntax.

Inspired by the article Rethinking Cron and the schedule python module.

How to use?

package main

import (
	"fmt"
	"runtime"
	"time"

	"github.com/carlescere/scheduler"
)

func main() {
	job := func() {
		t := time.Now()
		fmt.Println("Time's up! @", t.UTC())
	}
      // Run every 2 seconds but not now.
	scheduler.Every(2).Seconds().NotImmediately().Run(job)
      
      // Run now and every X.
	scheduler.Every(5).Minutes().Run(job)
	scheduler.Every().Day().Run(job)
	scheduler.Every().Monday().At("08:30").Run(job)
      
      // Keep the program from not exiting.
	runtime.Goexit()
}

How it works?

By specifying the chain of calls, a Job struct is instantiated and a goroutine is starts observing the Job.

The goroutine will be on pause until:

  • The next run scheduled is due. This will cause to execute the job.
  • The SkipWait channel is activated. This will cause to execute the job.
  • The Quit channel is activated. This will cause to finish the job.

Not immediate recurrent jobs

By default the behaviour of the recurrent jobs (Every(N) seconds, minutes, hours) is to start executing the job right away and then wait the required amount of time. By calling specifically .NotImmediately() you can override that behaviour and not execute it directly when the function Run() is called.

scheduler.Every(5).Minutes().NotImmediately().Run(job)

License

Distributed under MIT license. See LICENSE for more information.

Documentation

Overview

Package scheduler is a cron replacement based on:

http://adam.herokuapp.com/past/2010/4/13/rethinking_cron/

and

https://github.com/dbader/schedule

Uses include:

 func main() {
   job := func() {
	fmt.Println("Time's up!")
   }
   scheduler.Every(5).Seconds().Run(function)
   scheduler.Every().Day().Run(function)
   scheduler.Every().Sunday().At("08:30").Run(function)
 }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Quit     chan bool
	SkipWait chan bool

	sync.RWMutex
	// contains filtered or unexported fields
}

Job defines a running job and allows to stop a scheduled job or run it.

func Every

func Every(times ...int) *Job

Every defines when to run a job. For a recurrent jobs (n seconds/minutes/hours) you should specify the unit and then call to the correspondent period method.

func (*Job) At

func (j *Job) At(hourTime string) *Job

At lets you define a specific time when the job would be run. Does not work with recurrent jobs. Time should be defined as a string separated by a colon. Could be used as "08:35:30", "08:35" or "8" for only the hours.

func (*Job) Day

func (j *Job) Day() *Job

Day sets the job to run every day.

func (*Job) Friday

func (j *Job) Friday() *Job

Friday sets the job to run every Friday.

func (*Job) Hours

func (j *Job) Hours() *Job

Hours sets the job to run every n Hours where n was defined in the Every function.

func (*Job) IsRunning

func (j *Job) IsRunning() bool

IsRunning returns if the job is currently running

func (*Job) Minutes

func (j *Job) Minutes() *Job

Minutes sets the job to run every n Minutes where n was defined in the Every function.

func (*Job) Monday

func (j *Job) Monday() *Job

Monday sets the job to run every Monday.

func (*Job) NotImmediately

func (j *Job) NotImmediately() *Job

NotImmediately allows recurrent jobs not to be executed immediatelly after definition. If a job is declared hourly won't start executing until the first hour passed.

func (*Job) Run

func (j *Job) Run(f func()) (*Job, error)

Run sets the job to the schedule and returns the pointer to the job so it may be stopped or executed without waiting or an error.

func (*Job) Saturday

func (j *Job) Saturday() *Job

Saturday sets the job to run every Saturday.

func (*Job) Seconds

func (j *Job) Seconds() *Job

Seconds sets the job to run every n Seconds where n was defined in the Every function.

func (*Job) Sunday

func (j *Job) Sunday() *Job

Sunday sets the job to run every Sunday.

func (*Job) Thursday

func (j *Job) Thursday() *Job

Thursday sets the job to run every Thursday.

func (*Job) Tuesday

func (j *Job) Tuesday() *Job

Tuesday sets the job to run every Tuesday.

func (*Job) Wednesday

func (j *Job) Wednesday() *Job

Wednesday sets the job to run every Wednesday.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL