crontab

package module
Version: v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 9 Imported by: 8

README

Go/Golang package for Crontab tickers GoDoc

This package provides crontab tickers to golang apps, supporting crontab-like syntax like * * * * * or */2 * * * * etc.

Installation

go get github.com/mileusna/crontab

Example

package main

import (
    "fmt"
    "log"

    "github.com/mileusna/crontab"
)

func main() {

    ctab := crontab.New() // create cron table

    // AddJob and test the errors
    err := ctab.AddJob("0 12 1 * *", myFunc) // on 1st day of month
    if err != nil {
        log.Println(err)
        return
    }    

    // MustAddJob is like AddJob but panics on wrong syntax or problems with func/args
    // This aproach is similar to regexp.Compile and regexp.MustCompile from go's standard library,  used for easier initialization on startup
    ctab.MustAddJob("* * * * *", myFunc) // every minute
    ctab.MustAddJob("0 12 * * *", myFunc3) // noon lauch

    // fn with args
    ctab.MustAddJob("0 0 * * 1,2", myFunc2, "Monday and Tuesday midnight", 123) 
    ctab.MustAddJob("*/5 * * * *", myFunc2, "every five min", 0)

    // all your other app code as usual, or put sleep timer for demo
    // time.Sleep(10 * time.Minute)
}

func myFunc() {
    fmt.Println("Helo, world")
}

func myFunc3() {
    fmt.Println("Noon!")
}

func myFunc2(s string, n int) {
    fmt.Println("We have params here, string", s, "and number", n)
}

Crontab syntax

If you are not faimiliar with crontab syntax you might be better off with other packages for scheduling tasks. But if you are familiar with Linux and crontab, this package might be right for you.

Here are the few quick references about crontab simple but powerful syntax.

*     *     *     *     *        

^     ^     ^     ^     ^
|     |     |     |     |
|     |     |     |     +----- day of week (0-6) (Sunday=0)
|     |     |     +------- month (1-12)
|     |     +--------- day of month (1-31)
|     +----------- hour (0-23)
+------------- min (0-59)
Examples
  • * * * * * run on every minute
  • 10 * * * * run at 0:10, 1:10 etc
  • 10 15 * * * run at 15:10 every day
  • * * 1 * * run on every minute on 1st day of month
  • 0 0 1 1 * Happy new year schedule
  • 0 0 * * 1 Run at midnight on every Monday
Lists
  • * 10,15,19 * * * run at 10:00, 15:00 and 19:00
  • 1-15 * * * * run at 1, 2, 3...15 minute of each hour
  • 0 0-5,10 * * * run on every hour from 0-5 and in 10 oclock
Steps
  • */2 * * * * run every two minutes
  • 10 */3 * * * run every 3 hours on 10th min
  • 0 12 */2 * * run at noon on every two days
  • 1-59/2 * * * * run every two minutes, but on odd minutes

Notice

There is no way to reschedule or to remove single job from crontab during runtime with crontab package. (Re)create new instance of crontab or use crontab.Clear() function and then add jobs again to reschedule during runtime.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crontab

type Crontab struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Crontab struct representing cron table

Example
package main

import (
	"fmt"
	"log"

	"github.com/mileusna/crontab"
)

func main() {

	ctab := crontab.New() // create cron table

	// MustAddJob panics on wrong syntax or problem with func and args for easier initialization
	ctab.MustAddJob("0 12 * * *", myFunc3)
	ctab.MustAddJob("* * * * *", myFunc2, "on every minute", 123) // fn with args
	ctab.MustAddJob("*/2 * * * *", myFunc2, "every two min", 18)

	// or use AddJob if you want to test the error
	err := ctab.AddJob("* * * * *", myFunc)
	if err != nil {
		log.Println(err)
		return
	}

	// all your other app code as usual, or put sleep timer for example
	// time.Sleep(5 * time.Minute)
}

func myFunc() {
	fmt.Println("Helo, world")
}

func myFunc3() {
	fmt.Println("Noon!")
}

func myFunc2(s string, n int) {
	fmt.Printf("We have params here, string `%s` and nymber %d\n", s, n)
}

type MyTypeInterface struct {
	ID   int
	Name string
}

func (m MyTypeInterface) Bar() string {
	return "OK"
}

type MyTypeNoInterface struct {
	ID   int
	Name string
}

func myFuncStruct(m MyTypeInterface) {
	fmt.Println("Custom type as param")
}

func myFuncInterface(i Foo) {
	i.Bar()
}

type Foo interface {
	Bar() string
}
Output:

func New

func New() *Crontab

New initializes and returns new cron table

func (*Crontab) AddJob

func (c *Crontab) AddJob(schedule string, fn interface{}, args ...interface{}) error

AddJob to cron table

Returns error if:

* Cron syntax can't be parsed or out of bounds

* fn is not function

* Provided args don't match the number and/or the type of fn args

func (*Crontab) Clear

func (c *Crontab) Clear()

Clear all jobs from cron table

func (*Crontab) MustAddJob

func (c *Crontab) MustAddJob(schedule string, fn interface{}, args ...interface{})

MustAddJob is like AddJob but panics if there is an problem with job

It simplifies initialization, since we usually add jobs at the beggining so you won't have to check for errors (it will panic when program starts). It is a similar aproach as go's std lib package `regexp` and `regexp.Compile()` `regexp.MustCompile()` MustAddJob will panic if:

* Cron syntax can't be parsed or out of bounds

* fn is not function

* Provided args don't match the number and/or the type of fn args

func (*Crontab) RunAll

func (c *Crontab) RunAll()

RunAll jobs in cron table, shcheduled or not

func (*Crontab) Shutdown

func (c *Crontab) Shutdown()

Shutdown the cron table schedule

Once stopped, it can't be restarted. This function is pre-shuttdown helper for your app, there is no Start/Stop functionallity with crontab package.

Source Files

Jump to

Keyboard shortcuts

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