cron

package module
v0.0.0-...-a499989 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cron contains cron jobs and other logging functions

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearFutureJobs

func ClearFutureJobs()

ClearFutureJobs clears all future jobs waiting to be executed

func ClearRecurringJobs

func ClearRecurringJobs()

ClearRecurringJobs clears all recurring jobs. You would do this in a case of some event reconfiguring possibly a dynamic event of RegisterRecurring() where an end user is in control of the execution of the cron jobs Then call the function which reads your static configuration or dynamic configuration to setup your cron jobs and there is no need to call Start again if its already been called

func ExecuteOneTimeJob

func ExecuteOneTimeJob(jobName string, callback OneTimeEvent)

ExecuteOneTimeJob executes a one time job.

Example

ExampleExecuteOneTimeJob is an example of how to execute a one time event.

package main

import (
	"github.com/DanielRenne/GoCore/core/cron"
	"github.com/DanielRenne/GoCore/core/zip"
)

func main() {
	cb := func() bool {
		err := zip.Unzip("test", "test", []string{})
		return err == nil
	}
	cron.ExecuteOneTimeJob("run me and i will save a key of this string and write the boolean of success or not to /usr/local/goCore/jobs/jobs.json for historical purposes", cb)
}
Output:

func RegisterFutureEvent

func RegisterFutureEvent(t time.Time, callback FutureEvent)

RegisterFutureEvent provides a method to register for a callback that is called at at some time you specify in the future

Example

ExampleRegisterFutureEvent is an example of how to execute a one time event.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func() {
		log.Println("asdfasdf")
	}
	t := time.Now().Add(time.Second * 5)
	cron.RegisterFutureEvent(t, cb)
}
Output:

func RegisterRecurring

func RegisterRecurring(t RecurringType, callback RecurringEvent)

RegisterRecurring provides a method to register for a callback that is called at the start of the cron job engine and 5 seconds before each day occures.

Example

You may have a situation that upon initializing, you have a user defined configuration to control the interval of the job. This is how you would do that.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	// In this situation, you have a user defined configuration to control the interval of the job.  This is how you would do that:
	var runCronAt cron.RecurringType
	var someConfigValue string
	if someConfigValue == "top-of-hour" {
		runCronAt = cron.CronTopOfHour
	}
	if someConfigValue == "" {
		runCronAt = cron.CronTopOfSecond
	}
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on top of second (because it wasnt top-of-hour): " + currentTime.String())
	}
	cron.RegisterRecurring(runCronAt, cb)
	c := make(chan string)
	go func() {
		time.Sleep(10 * time.Second)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)

}
Output:

func RegisterTopOf30SecondsJob

func RegisterTopOf30SecondsJob(callback RecurringEvent)

RegisterTopOf30SecondsJob executes top of 30 seconds job.

Example

top of 30 seconds event

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on: " + currentTime.String())
	}
	cron.RegisterTopOf30SecondsJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(2 * time.Minute)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func RegisterTopOfDayJob

func RegisterTopOfDayJob(callback RecurringEvent)

RegisterTopOfDayJob executes top of 30 seconds job.

Example

executes top of day.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on: " + currentTime.String())
	}
	cron.RegisterTopOfDayJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(24 * time.Hour)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func RegisterTopOfHourJob

func RegisterTopOfHourJob(callback RecurringEvent)

RegisterTopOfHourJob executes top of 30 seconds job.

Example

executes top of the hour.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on: " + currentTime.String())
	}
	cron.RegisterTopOfHourJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(1 * time.Hour)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func RegisterTopOfMinuteJob

func RegisterTopOfMinuteJob(callback RecurringEvent)

RegisterTopOfMinuteJob executes top of 30 seconds job.

Example

executes top of minute job.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on: " + currentTime.String())
	}
	cron.RegisterTopOfMinuteJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(2 * time.Minute)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func RegisterTopOfSecondJob

func RegisterTopOfSecondJob(callback RecurringEvent)

RegisterTopOfSecondJob executes top of 30 seconds job.

Example

executes top of second job.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		core.Dump("Job Executed on: " + currentTime.String())
	}
	cron.RegisterTopOfSecondJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(10 * time.Second)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func ShouldRunEvery15Minutes

func ShouldRunEvery15Minutes(x time.Time) (run bool)

ShouldRunEvery15Minutes is a helper function that returns true if the time is the top of every 15 minutes.

func ShouldRunEveryFiveMinutes

func ShouldRunEveryFiveMinutes(x time.Time) (run bool)

ShouldRunEveryFiveMinutes is a helper function that returns true if the time is the top of every 5 minutes.

Example

execute job every 5 minutes

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/cron"
)

func main() {
	cb := func(currentTime time.Time) {
		// Here you wrap the current time with the helper function so that minutes 1, 2, 3, 4 are skipped, but minutes 0 and 5 are executed.
		if cron.ShouldRunEveryFiveMinutes(currentTime) {
			core.Dump("Job Executed on top of 5 minutes: " + currentTime.String())
		}
	}
	cron.RegisterTopOfMinuteJob(cb)
	c := make(chan string)
	go func() {
		time.Sleep(11 * time.Minute)
		c <- "done"
	}()
	exiting := <-c
	log.Println(exiting)
}
Output:

func ShouldRunEveryTenMinutes

func ShouldRunEveryTenMinutes(x time.Time) (run bool)

ShouldRunEveryTenMinutes is a helper function that returns true if the time is the top of every 10 minutes. Note, please call cron.()

Types

type FutureEvent

type FutureEvent func()

type OneTimeEvent

type OneTimeEvent func() bool

OneTimeEvent is used to schedule a one time event to be executed

type RecurringEvent

type RecurringEvent func(eventDate time.Time)

RecurringEvent is a callback function called by the cron job engine.

type RecurringType

type RecurringType int

RecurringType is a type of recurring event (int)

const (
	//CronTopOfMinute is a cron job type that is called at the top of every minute.
	CronTopOfMinute RecurringType = iota
	//CronTopOfHour is a cron job type that is called at the top of every hour.
	CronTopOfHour
	//CronTopOfDay is a cron job type that is called at the top of every day.
	CronTopOfDay
	//CronTopOf30Seconds is a cron job type that is called at the top of every 30 seconds.
	CronTopOf30Seconds
	//CronTopOfSecond is a cron job type that is called at the top of every second.
	CronTopOfSecond
)

Jump to

Keyboard shortcuts

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