Categorygithub.com/michaels11/go-scheduler
modulepackage
0.1.0
Repository: https://github.com/michaels11/go-scheduler.git
Documentation: pkg.go.dev

# README

Go Cron Job Scheduler

Golang Cron Job Scheduler

GoDoc Reference Build Status Coverage Go Report Card

Get

go get github.com/MichaelS11/go-scheduler

Simple Example

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/MichaelS11/go-scheduler"
)

func main() {
	myFunction := func(dataInterface interface{}) {
		data := dataInterface.(string)
		fmt.Println(data)
	}

	// Create new scheduler
	s := scheduler.NewScheduler()

	// Make a new job that runs myFunction passing it "myData"
	// A cron of * * * * * * * will run every second.
	// The scheduler uses UTC time
	err := s.Make("jobName", "* * * * * * *", myFunction, "myData")
	if err != nil {
		log.Fatalln("Make error:", err)
	}

	// Starts the job schedule. Job will run at it's next run time.
	s.Start("jobName")

	// Wait a second to make sure job was run
	time.Sleep(time.Second)

	// Stop all the jobs and waits for then to all stop. In the below case it waits for at least a second.
	// Note this does not kill any running jobs, only stops them from running again.
	s.StopAllWait(time.Second)

	// Output:
	// myData
}

Important note about Cron format

The Cron format is in the form of:

Seconds, Minutes, Hours, Day of month, Month, Day of week, Year

Field name     Mandatory?   Allowed values    Allowed special characters
----------     ----------   --------------    --------------------------
Seconds        No           0-59              * / , -
Minutes        Yes          0-59              * / , -
Hours          Yes          0-23              * / , -
Day of month   Yes          1-31              * / , - L W
Month          Yes          1-12 or JAN-DEC   * / , -
Day of week    Yes          0-6 or SUN-SAT    * / , - L #
Year           No           1970-2099         * / , -

The Cron parser used is:

https://github.com/gorhill/cronexpr

# Functions

NewScheduler creates a new Scheduler.

# Constants

StateDeleting when job is scheduled to be deleted after run.
StateRunning when job is running.
StateScheduled when job pending schedule to run.
StateStopped when job is stopped.
StateStopping when job is scheduled to stop after run.

# Variables

ErrJobAlreadyExists is returned when a job name already exists.
ErrJobIsRunning is returned when a job is running.
ErrJobMustBeStopped is returned when a job is not stopped first.
ErrJobNotFound is returned when job has not been found.

# Structs

Scheduler is used to create and run jobs.

# Type aliases

State what state the job is in.