cron

package module
v0.0.0-...-9b0f09e Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: MIT Imports: 18 Imported by: 0

README

cron

Build Status | codecov | Go Report Card | GoDoc

A simple tool that allows you to cron your jobs.

If i miss something or you have something interesting, please be part of this project. Let me know! My contact is at the end.

With support for modes

  • from midnight
  • recurring

Settings

Mode Year Month Day_of_week Day Hour Minute Second

0 1 1 1 0 0 0

Modes
  • 0 - starts on midnight
  • 1 - starts from now

Dependency Management

Dependency

Project dependencies are managed using Dependency. Read more about Dependency.

  • Get dependency manager: go get github.com/joaosoft/dependency
Commands
  • Install dependencies: dependency get
  • Update dependencies: dependency update
  • Reset dependencies: dependency reset
  • Add dependencies: dependency add <dependency>
  • Remove dependencies: dependency remove <dependency>
Go
go get github.com/joaosoft/cron
Configuration
{
  "cron": {
    "log": {
      "level": "debug"
    },
    "migration": {
      "path": {
        "database": "schema/db/postgres"
      },
      "db": {
        "schema": "cron",
        "driver": "postgres",
        "datasource": "postgres://user:password@localhost:7000/postgres?sslmode=disable&cron_path=cron"
      },
      "log": {
        "level": "info"
      }
    },
    "dbr": {
      "read_db": {
        "driver": "postgres",
        "datasource": "postgres://user:password@localhost:7000/postgres?sslmode=disable&cron_path=cron"
      },
      "write_db": {
        "driver": "postgres",
        "datasource": "postgres://user:password@localhost:7100/postgres?sslmode=disable&cron_path=cron"
      },
      "log": {
        "level": "debug"
      }
    }
  },
  "manager": {
    "log": {
      "level": "debug"
    }
  }
}

Usage

This examples are available in the project at cron/examples

func main() {
	// create cron
	myCron, err := cron.New()
	if err != nil {
		panic(err)
	}

	// add tasks to jobs
	myCron.
		AddJobTaskWithFuncs("teste1_key", canExecute, before, middle, after).
		AddJobTask("teste2_key", cron.NewTaskGeneric(canExecute, before, middle, after)).
		AddJobTaskWithFuncs("teste3_key", canExecute, before, middle, after)

	// wait
	myCron.Wait()
}

func canExecute() (bool, error) {
	logger.Info("executing can execute method")
	return true, nil
}

func before() error {
	logger.Info("executing before method")
	return nil
}

func middle() error {
	logger.Info("executing middle method")
	return nil
}

func after() error {
	logger.Info("executing after method")
	return nil
}
Result:
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"config config_app added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"database db_postgres added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"database db-Read added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"database db-Write added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"scheduling job with name: teste1","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"process teste1_key added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"scheduling job with name: teste2","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"process teste2_key added","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"starting...","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"started [ process: teste1_key ]","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"started [ process: teste2_key ]","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"job teste2_key waiting next scheduled time 1m0s]","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"job teste1_key waiting next scheduled time 23h41m30.132184s]","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"manager"},"message":"started","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"cron service started!","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:29:19"},"tags":{"service":"cron"},"message":"waiting for quit signal...","sufixes":{"ip":"192.168.1.67"}}
{"prefixes":{"level":"info","timestamp":"2019-06-14 19:04:31:19"},"tags":{"service":"cron"},"message":"quitting cron","sufixes":{"ip":"192.168.1.67"}}

Known issues

Follow me at

Facebook: https://www.facebook.com/joaosoft

LinkedIn: https://www.linkedin.com/in/jo%C3%A3o-ribeiro-b2775438/

If you have something to add, please let me know joaosoft@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorInvalidTask     = errors.New("invalid function")
	ErrorInvalidFunction = errors.New("invalid task")
)

Functions

func Exists

func Exists(file string) bool

func GetEnv

func GetEnv() string

func ReadFile

func ReadFile(file string, obj interface{}) ([]byte, error)

func ReadFileLines

func ReadFileLines(file string) ([]string, error)

func WriteFile

func WriteFile(file string, obj interface{}) error

Types

type AppConfig

type AppConfig struct {
	Cron *CronConfig `json:"cron"`
}

AppConfig ...

func NewConfig

func NewConfig() (*AppConfig, manager.IConfig, error)

NewConfig ...

type CheckExecuteFunc

type CheckExecuteFunc func() (bool, error)

type Cron

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

func New

func New(options ...CronOption) (*Cron, error)

Start ...

func (*Cron) AddJob

func (cron *Cron) AddJob(job *Job) *Cron

func (*Cron) AddJobTask

func (cron *Cron) AddJobTask(key string, t ITask, ts ...ITask) *Cron

func (*Cron) AddJobTaskWithFuncs

func (cron *Cron) AddJobTaskWithFuncs(key string, canExecuteFunc CheckExecuteFunc, f ExecuteFunc, fs ...ExecuteFunc) *Cron

func (*Cron) NewSchedule

func (cron *Cron) NewSchedule(job *Job) *Schedule

func (*Cron) Reconfigure

func (cron *Cron) Reconfigure(options ...CronOption)

Reconfigure ...

func (*Cron) Wait

func (cron *Cron) Wait() (err error)

type CronConfig

type CronConfig struct {
	Migration *migration.MigrationConfig `json:"migration"`
	Dbr       *dbr.DbrConfig             `json:"dbr"`
	Log       struct {
		Level string `json:"level"`
	} `json:"log"`
}

CronConfig ...

type CronOption

type CronOption func(cron *Cron)

CronOption ...

func WithConfiguration

func WithConfiguration(config *CronConfig) CronOption

WithConfiguration ...

func WithLocation

func WithLocation(location *time.Location) CronOption

WithLocation ...

func WithLogLevel

func WithLogLevel(level logger.Level) CronOption

WithLogLevel ...

func WithLogger

func WithLogger(logger logger.ILogger) CronOption

WithLogger ...

func WithManager

func WithManager(mgr *manager.Manager) CronOption

WithManager ...

type ExecuteFunc

type ExecuteFunc func() error

type ITask

type ITask interface {
	CanExecute() (bool, error)
	Execute(breakOnError bool) error
}

type Job

type Job struct {
	Name     string `json:"name" db.read:"name"`
	Key      string `json:"key" db.read:"key"`
	Running  bool   `json:"running" db.read:"running"`
	Settings string `json:"settings" db.read:"settings"`
	Active   bool   `json:"active" db.read:"active"`
	Tasks    ListTasks
}

func NewJob

func NewJob(name string, key string, settings string, tasks ...ITask) *Job

type ListJobs

type ListJobs []*Job

type ListTasks

type ListTasks []ITask

func (ListTasks) Execute

func (tasks ListTasks) Execute() (err error)

type MapJobs

type MapJobs map[string]*Job

type MapSchedule

type MapSchedule map[string]*Schedule

type Schedule

type Schedule struct {
	Job *Job
	// contains filtered or unexported fields
}

func (*Schedule) Start

func (schedule *Schedule) Start(waitGroup ...*sync.WaitGroup) error

func (*Schedule) Started

func (schedule *Schedule) Started() bool

func (*Schedule) Stop

func (schedule *Schedule) Stop(waitGroup ...*sync.WaitGroup) error

type TaskGeneric

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

func NewTaskGeneric

func NewTaskGeneric(canExecuteFunc CheckExecuteFunc, f ExecuteFunc, fs ...ExecuteFunc) *TaskGeneric

func (*TaskGeneric) CanExecute

func (task *TaskGeneric) CanExecute() (bool, error)

func (*TaskGeneric) Execute

func (task *TaskGeneric) Execute(breakOnError bool) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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