schedule

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 8 Imported by: 0

README

Schedule inspired laravel

A simple scheduler for golang, inspired with laravel's task scheduling, use it with crontab.

CI Coverage Go Report Card license PkgGoDev

Run with crontab

After you build your project, you can use it with crontab like below.

* * * * * /path/to/your/schedule >> /dev/null 2>&1
Features
  • You can integrate to your self project;
  • Multiple frequency options;
  • Day and time constraints;
  • Custom logger;
  • context.Context integration;
  • Panic recover;
  • Run task in go routine;
  • Custom timezone;
  • 100% code coverage;
Installation
go get -u github.com/iflamed/schedule

Getting Started

Custom logger

Logger interface

type Logger interface {
	Error(msg string, e any)
	Debugf(msg string, n int32)
	Debug(msg string)
}

The default logger

type DefaultLogger struct {
}

func (d *DefaultLogger) Error(msg string, r any) {
	log.Println(msg, r)
}

func (d *DefaultLogger) Debug(msg string) {
	log.Println(msg)
}

func (d *DefaultLogger) Debugf(msg string, i int32) {
	log.Printf(msg, i)
}
Create scheduler instance
s := NewScheduler(context.Background(), time.UTC)
s.Daily().CallFunc(func(ctx context.Context) {
    log.Println("Task finished.")
    return
})
s.DailyAt("09:00").Call(NewDefaultTask(func(ctx context.Context) {
    log.Println("Task finished at 09:00")
}))
s.Start()

⚠️You should set frequency first, then call the Call and CallFunc method to run task.

⚠️s.Start() method must call at last, it will wait all task finished when process exit.

Schedule Frequency Options

There are many more task schedule frequencies that you may assign to a task:

Method Description
EveryMinute() Run the task every minute
EveryTwoMinutes() Run the task every two minutes
EveryThreeMinutes() Run the task every three minutes
EveryFourMinutes() Run the task every four minutes
EveryFiveMinutes() Run the task every five minutes
EveryTenMinutes() Run the task every ten minutes
EveryFifteenMinutes() Run the task every fifteen minutes
EveryThirtyMinutes() Run the task every thirty minutes
Hourly() Run the task every hour
HourlyAt(17) Run the task every hour at 17 minutes past the hour
EveryOddHour() Run the task every odd hour
EveryTwoHours() Run the task every two hours
EveryThreeHours() Run the task every three hours
EveryFourHours() Run the task every four hours
EverySixHours() Run the task every six hours
Daily() Run the task every day at midnight
DailyAt("13:00") Run the task every day at 13:00
At("13:00") Run the task every day at 13:00, method alias of dailyAt
TwiceDaily(1, 13) Run the task daily at 1:00 & 13:00
TwiceDailyAt(1, 13, 15) Run the task daily at 1:15 & 13:15
Weekly() Run the task every Sunday at 00:00
WeeklyOn(1, "8:00") Run the task every week on Monday at 8:00
Monthly() Run the task on the first day of every month at 00:00
MonthlyOn(4, "15:00") Run the task every month on the 4th at 15:00
TwiceMonthly(1, 16, "13:00") Run the task monthly on the 1st and 16th at 13:00
LastDayOfMonth("15:00") Run the task on the last day of the month at 15:00
Quarterly() Run the task on the first day of every quarter at 00:00
Yearly() Run the task on the first day of every year at 00:00
YearlyOn(6, 1, "17:00") Run the task every year on June 1st at 17:00
Timezone(time.UTC) Set the timezone for the task
Schedule constraints
Method Description
Weekdays() Limit the task to weekdays
Weekends() Limit the task to weekends
Sundays() Limit the task to Sunday
Mondays() Limit the task to Monday
Tuesdays() Limit the task to Tuesday
Wednesdays() Limit the task to Wednesday
Thursdays() Limit the task to Thursday
Fridays() Limit the task to Friday
Saturdays() Limit the task to Saturday
Days(d ...time.Weekday) Limit the task to specific days
Between(start, end string) Limit the task to run between start and end time
UnlessBetween(start, end string) Limit the task to not run between start and end time
When(when WhenFunc) Limit the task based on a truth test
Schedule example
package main

import (
	"context"
	"github.com/iflamed/schedule"
	"log"
	"time"
)

func main() {
	s := schedule.NewScheduler(context.Background(), time.UTC)
	s.Daily().CallFunc(func(ctx context.Context) {
		log.Println("Task finished.")
	})
	s.DailyAt("09:00").Call(schedule.NewDefaultTask(func(ctx context.Context) {
		log.Println("Task finished at 09:00")
	}))
	s.EveryMinute().Sundays().Call(schedule.NewDefaultTask(func(ctx context.Context) {
		log.Println("Task finished at 09:00")
	}))
	s.EveryMinute().Sundays().Between("12:00", "20:00").Call(schedule.NewDefaultTask(func(ctx context.Context) {
		log.Println("Task finished at 09:00")
	}))
	s.Start()
}

Documentation

Overview

Package schedule The core code of scheduler, contain frequency options and constraints.

Package schedule file contains all struct and interface define.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultLogger

type DefaultLogger struct {
}

func (*DefaultLogger) Debug

func (d *DefaultLogger) Debug(msg string)

func (*DefaultLogger) Debugf

func (d *DefaultLogger) Debugf(msg string, i int32)

func (*DefaultLogger) Error

func (d *DefaultLogger) Error(msg string, r any)

type DefaultTask

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

func NewDefaultTask

func NewDefaultTask(fn TaskFunc) *DefaultTask

func (*DefaultTask) Run

func (d *DefaultTask) Run(ctx context.Context)

type Limit

type Limit struct {
	DaysOfWeek []time.Weekday
	StartTime  string
	EndTime    string
	IsBetween  bool
	When       WhenFunc
}

type Logger

type Logger interface {
	Error(msg string, e any)
	Debugf(msg string, n int32)
	Debug(msg string)
}

Logger logger interface for scheduler logger

type NextTick

type NextTick struct {
	Year   int
	Month  int
	Day    int
	Hour   int
	Minute int
	Omit   bool
}

type Scheduler

type Scheduler struct {
	Next *NextTick
	// contains filtered or unexported fields
}

Scheduler The core scheduler struct

func NewScheduler

func NewScheduler(ctx context.Context, loc *time.Location) *Scheduler

NewScheduler create instance of scheduler with context and default time.location

func (*Scheduler) At

func (s *Scheduler) At(t ...string) *Scheduler

At run the task every day at some time (03:00 format), method alias of dailyAt

func (*Scheduler) Between

func (s *Scheduler) Between(start, end string) *Scheduler

Between limit the task to run between start and end time

func (*Scheduler) Call

func (s *Scheduler) Call(t Task)

Call call a task

func (*Scheduler) CallFunc

func (s *Scheduler) CallFunc(fn TaskFunc)

CallFunc call a task function

func (*Scheduler) Daily

func (s *Scheduler) Daily() *Scheduler

Daily run the task every day at midnight

func (*Scheduler) DailyAt

func (s *Scheduler) DailyAt(t ...string) *Scheduler

DailyAt run the task every day at some time (03:00 format)

func (*Scheduler) Days

func (s *Scheduler) Days(d ...time.Weekday) *Scheduler

Days limit the task to specific days

func (*Scheduler) EveryFifteenMinutes

func (s *Scheduler) EveryFifteenMinutes() *Scheduler

EveryFifteenMinutes run the task every fifteen minutes

func (*Scheduler) EveryFiveHours

func (s *Scheduler) EveryFiveHours() *Scheduler

EveryFiveHours run the task every five hours

func (*Scheduler) EveryFiveMinutes

func (s *Scheduler) EveryFiveMinutes() *Scheduler

EveryFiveMinutes run task every five minutes

func (*Scheduler) EveryFourHours

func (s *Scheduler) EveryFourHours() *Scheduler

EveryFourHours run the task every four hours

func (*Scheduler) EveryFourMinutes

func (s *Scheduler) EveryFourMinutes() *Scheduler

EveryFourMinutes run task every four minutes

func (*Scheduler) EveryMinute

func (s *Scheduler) EveryMinute() *Scheduler

EveryMinute run task every minutes

func (*Scheduler) EveryOddHour

func (s *Scheduler) EveryOddHour() *Scheduler

EveryOddHour run the task every odd hour

func (*Scheduler) EverySixHours

func (s *Scheduler) EverySixHours() *Scheduler

EverySixHours run the task every six hours

func (*Scheduler) EveryTenMinutes

func (s *Scheduler) EveryTenMinutes() *Scheduler

EveryTenMinutes run the task every ten minutes

func (*Scheduler) EveryThirtyMinutes

func (s *Scheduler) EveryThirtyMinutes() *Scheduler

EveryThirtyMinutes run the task every thirty minutes

func (*Scheduler) EveryThreeHours

func (s *Scheduler) EveryThreeHours() *Scheduler

EveryThreeHours run the task every three hours

func (*Scheduler) EveryThreeMinutes

func (s *Scheduler) EveryThreeMinutes() *Scheduler

EveryThreeMinutes run task every three minutes

func (*Scheduler) EveryTwoHours

func (s *Scheduler) EveryTwoHours() *Scheduler

EveryTwoHours run the task every two hours

func (*Scheduler) EveryTwoMinutes

func (s *Scheduler) EveryTwoMinutes() *Scheduler

EveryTwoMinutes run task every two minutes

func (*Scheduler) Fridays

func (s *Scheduler) Fridays() *Scheduler

Fridays limit the task to Friday

func (*Scheduler) Hourly

func (s *Scheduler) Hourly() *Scheduler

Hourly run the task every hour

func (*Scheduler) HourlyAt

func (s *Scheduler) HourlyAt(t ...int) *Scheduler

HourlyAt run the task every hour at some minutes past the hour

func (*Scheduler) LastDayOfMonth

func (s *Scheduler) LastDayOfMonth(t string) *Scheduler

LastDayOfMonth run the task on the last day of the month at a time LastDayOfMonth("15:00") run the task on the last day of the month at 15:00

func (*Scheduler) Mondays

func (s *Scheduler) Mondays() *Scheduler

Mondays limit the task to Monday

func (*Scheduler) Monthly

func (s *Scheduler) Monthly() *Scheduler

Monthly run the task on the first day of every month at 00:00

func (*Scheduler) MonthlyOn

func (s *Scheduler) MonthlyOn(d int, t string) *Scheduler

MonthlyOn run the task every month on a time MonthlyOn(4, "15:00") run the task every month on the 4th at 15:00

func (*Scheduler) Quarterly

func (s *Scheduler) Quarterly() *Scheduler

Quarterly Run the task on the first day of every quarter at 00:00

func (*Scheduler) Saturdays

func (s *Scheduler) Saturdays() *Scheduler

Saturdays limit the task to Saturday

func (*Scheduler) SetLogger

func (s *Scheduler) SetLogger(l Logger) *Scheduler

SetLogger set a new logger

func (*Scheduler) Start

func (s *Scheduler) Start()

Start wait all task to be finished

func (*Scheduler) Sundays

func (s *Scheduler) Sundays() *Scheduler

Sundays limit the task to Sunday

func (*Scheduler) Thursdays

func (s *Scheduler) Thursdays() *Scheduler

Thursdays limit the task to Thursday

func (*Scheduler) Timezone

func (s *Scheduler) Timezone(loc *time.Location) *Scheduler

Timezone set timezone with a new time.Location instance after `Call` and `CallFunc` method called, the current time will roll back to default location.

func (*Scheduler) Tuesdays

func (s *Scheduler) Tuesdays() *Scheduler

Tuesdays limit the task to Tuesday

func (*Scheduler) TwiceDaily

func (s *Scheduler) TwiceDaily(first, second int) *Scheduler

TwiceDaily run the task daily at first and second hour

func (*Scheduler) TwiceDailyAt

func (s *Scheduler) TwiceDailyAt(first, second, offset int) *Scheduler

TwiceDailyAt run the task daily at some time TwiceDailyAt(1, 13, 15) run the task daily at 1:15 & 13:15

func (*Scheduler) TwiceMonthly

func (s *Scheduler) TwiceMonthly(b, e int, t string) *Scheduler

TwiceMonthly run the task monthly on some time TwiceMonthly(1, 16, "13:00") run the task monthly on the 1st and 16th at 13:00

func (*Scheduler) UnlessBetween

func (s *Scheduler) UnlessBetween(start, end string) *Scheduler

UnlessBetween limit the task to not run between start and end time

func (*Scheduler) Wednesdays

func (s *Scheduler) Wednesdays() *Scheduler

Wednesdays limit the task to Wednesday

func (*Scheduler) Weekdays

func (s *Scheduler) Weekdays() *Scheduler

Weekdays limit the task to weekdays

func (*Scheduler) Weekends

func (s *Scheduler) Weekends() *Scheduler

Weekends limit the task to weekends

func (*Scheduler) Weekly

func (s *Scheduler) Weekly() *Scheduler

Weekly run the task every Sunday at 00:00

func (*Scheduler) WeeklyOn

func (s *Scheduler) WeeklyOn(d time.Weekday, t string) *Scheduler

WeeklyOn run the task every week on a time WeeklyOn(1, "8:00") run the task every week on Monday at 8:00

func (*Scheduler) When

func (s *Scheduler) When(when WhenFunc) *Scheduler

When limit the task based on a truth test

func (*Scheduler) Yearly

func (s *Scheduler) Yearly() *Scheduler

Yearly run the task on the first day of every year at 00:00

func (*Scheduler) YearlyOn

func (s *Scheduler) YearlyOn(m, d int, t string) *Scheduler

YearlyOn Run the task every year on a time YearlyOn(6, 1, "17:00") run the task every year on June 1st at 17:00

type Task

type Task interface {
	Run(ctx context.Context)
}

Task task interface for scheduler task

type TaskFunc

type TaskFunc func(ctx context.Context)

TaskFunc the function of task

type WhenFunc

type WhenFunc func(ctx context.Context) bool

WhenFunc the function define of task constraint

Jump to

Keyboard shortcuts

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