shigoto

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2021 License: MIT Imports: 11 Imported by: 0

README

Shigoto - Task Scheduling

GitHub tag (latest by date) GitHub Coverage Status

Shigoto is a task scheduling for Golang with friendly API. Shigoto used MongoDB for persistent storage and used Cron format to set the schedule.

Installation

go get github.com/KodepandaID/shigoto

Example Usage

Init
package main

import "github.com/KodepandaID/shigoto"

func main() {
    client, e := shigoto.New(&shigoto.Config{
		DB:      "mongodb://localhost:27017",
		DBName:  "jobs-scheduler",
        Timezone: "Asia/Jakarta",
	})
    if e != nil {
        log.Fatal(e)
    }

    client.Run()
}
Register a function to call
func main() {
    client, e := shigoto.New(&shigoto.Config{
		DB:      "mongodb://localhost:27017",
		DBName:  "jobs-scheduler",
        Timezone: "Asia/Jakarta",
	})
    if e != nil {
        log.Fatal(e)
    }
    
    client.Register("hello", hello)
    client.Register("hello-params", helloParams, "message here")
    client.Run()
}

func hello() error {
    return nil
}

func helloParams(message string) error {
    return nil
}
Set Job

For more scheduling information, you can read at Go References.

func main() {
    client, e := shigoto.New(&shigoto.Config{
		DB:      "mongodb://localhost:27017",
		DBName:  "jobs-scheduler",
        Timezone: "Asia/Jakarta",
	})
    if e != nil {
        log.Fatal(e)
    }
    
    client.Register("hello", hello)
    if _, e := client.
        Command("job-name-here", "hello").
        EveryMinute().Do(); e != nil {
        log.Fatal(e)
    }
    client.Run()
}

func hello() error {
    return nil
}
Remove a Job
func main() {
    client, e := shigoto.New(&shigoto.Config{
		DB:      "mongodb://localhost:27017",
		DBName:  "jobs-scheduler",
        Timezone: "Asia/Jakarta",
	})
    if e != nil {
        log.Fatal(e)
    }
    
    client.Register("hello", hello)
    client.Delete("hello")
    client.Run()
}

License

Copyright Yudha Pratama Wicaksana, Licensed under MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FuncStorage = make(map[string]interface{})
View Source
var ScheduleStorage = make(map[string]interface{})

Functions

func CallFunc

func CallFunc(funcName string) (e error)

func CallFuncWithParams

func CallFuncWithParams(funcName string, params []interface{}) (e error)

func HandleErrFunc

func HandleErrFunc(values []reflect.Value) (e error)

func LoadJobsFromPersistentStorage

func LoadJobsFromPersistentStorage(c *Config)

After creating a new instance, the system will be load task data from persistent storage and added to scheduled storage mapping.

Types

type Config

type Config struct {
	DB       string // The MongoDB uri
	DBName   string // Database name from MongoDB
	Timezone string
	Timeout  time.Duration
	// contains filtered or unexported fields
}

Config to set the configuration task scheduler

func New

func New(c *Config) (*Config, error)

New to create task scheduler instance

func (*Config) Command

func (c *Config) Command(jobName, funcName string, params ...interface{}) *Jobs

Command to create a new job process

func (*Config) Delete

func (c *Config) Delete(name string)

Delete to remove job from instance and persistent storage

func (*Config) Register

func (c *Config) Register(funcName string, jobFunc interface{})

Register to register a function to call with the name The funcName should be the same with funcName at Command function

func (*Config) Run

func (c *Config) Run()

Run n a background process to check the tasks

type Jobs

type Jobs struct {
	JobName   string
	FuncName  string
	JobParams []interface{}
	Cron      []string // Set run a jobs with periodic by second, minute and hour
	// contains filtered or unexported fields
}

Jobs instance

func (*Jobs) At added in v0.2.0

func (j *Jobs) At(time string) *Jobs

At to run a job at a time

func (*Jobs) CronFormat

func (j *Jobs) CronFormat(cron string) *Jobs

CronFormat to set a job with cron format

func (*Jobs) Daily

func (j *Jobs) Daily() *Jobs

Daily to run a job every day at midnight

func (*Jobs) DailyAt

func (j *Jobs) DailyAt(time string) *Jobs

DailyAt to run a job every day at a specific time

func (*Jobs) Do

func (j *Jobs) Do() (id primitive.ObjectID, e error)

Do to run a schedule command

func (*Jobs) EveryFifteenMinutes

func (j *Jobs) EveryFifteenMinutes() *Jobs

EveryFifteenMinutes to run a job every 15 minutes

func (*Jobs) EveryFiveMinutes

func (j *Jobs) EveryFiveMinutes() *Jobs

EveryFiveMinutes to run a jobs every 5 minutes

func (*Jobs) EveryMinute

func (j *Jobs) EveryMinute() *Jobs

EveryMinute to run a job every minutes

func (*Jobs) EverySixHours

func (j *Jobs) EverySixHours() *Jobs

EverySixHours to run a job every 6 hours

func (*Jobs) EveryTenMinutes

func (j *Jobs) EveryTenMinutes() *Jobs

EveryTenMinutes to run a job every 10 minutes

func (*Jobs) EveryThirtyMinutes

func (j *Jobs) EveryThirtyMinutes() *Jobs

EveryThirtyMinutes to run a job every 30 minutes

func (*Jobs) EveryThreeHours

func (j *Jobs) EveryThreeHours() *Jobs

EveryThreeHours to run a job every 3 hours

func (*Jobs) EveryTwelveHours

func (j *Jobs) EveryTwelveHours() *Jobs

EveryTwelveHours to run a job every 12 hours

func (*Jobs) Hourly

func (j *Jobs) Hourly() *Jobs

Hourly to run a job every hours

func (*Jobs) Monthly

func (j *Jobs) Monthly() *Jobs

Monthly to run a job every month

func (*Jobs) MonthlyOn

func (j *Jobs) MonthlyOn(time string) *Jobs

MonthlyOn to run a job every month at a specific time

func (*Jobs) Quarterly

func (j *Jobs) Quarterly() *Jobs

Quarterly to run a job every 6 month

func (*Jobs) Weekly

func (j *Jobs) Weekly() *Jobs

Weekly to run a job every week

func (*Jobs) WeeklyOn

func (j *Jobs) WeeklyOn(time string) *Jobs

WeeklyOn to run a job every week at a specific time

func (*Jobs) Yearly

func (j *Jobs) Yearly() *Jobs

Yearly to run a job every 1 year

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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