go-cron

command module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2021 License: MIT Imports: 11 Imported by: 0

README

go-cron

A tiny cron scheduler for go

Features

  • Run multiple jobs concurrently
  • Specify Job frequency declaratively. i.e 1s, 3m, 5h, 300ms, etc.
  • Prometheus compatible metrics
  • Job Timeout
  • Jobs Cancellation and Scheduler cleaning up

Installation

go get github.com/adhaamehab/go-cron

Usage

// main.go
package main

import (
	"fmt"
	"time"
  "github.com/adhaamehab/go-cron"
)

func main() {
  jobs := []*Job{
    NewJob("Tick1", "1s", "1s", func() {
      time.Sleep(500 * time.Millisecond)
      fmt.Println("Tick 1") }, make([]interface{}, 0)
    ),
		NewJob("Tick2", "3s", "1s", func() { fmt.Println("Tick 2") }, make([]interface{}, 0)),
	}
	s, err := NewScheduler(C{MetricsPort: 9092})
	if err != nil {
    panic(err)
	}
	s.AddMany(jobs)

  // This will block the execution
  // You can use s.StartAsync() to run the scheduler in the background
	s.Start()

}

API Docs

Components

Scheduler

Scheduler is the core component in go-cron. It gives the user a configurable api to manage the jobs and how they run.It is composed of Executor and Jobs. User has full control over both components (explained later).

Executor

The idea of the executor is to allow scheduler to work different type of execution algorithms. This will enable users to extend the package to run as a distributed scheduler.

Job

A Job to a function is the same idea in K8s Pod & container. A Job is just a wrapper for a go function with some additional meta data about how to run this function in the background.

The downside to the current Job implementation is the ability to cancel a singe Job. Also handling the Job overlapping might be tricky.

Monitoring

Go-Cron expose a MetricsPort which expose prometheus metrics. This metrics tracks the number of running jobs and executors in addition to the execution time for each individual Job.

User can shut down the MetricsServer using StopMetricsServer() method. But it's yet a limitation to stop that. Using configuration. This should be enhanced later.

Logging

Road Map

  • Ability to cancel individual jobs.
  • Ability to configure metrics server before initializing the Scheduler.
  • More default executors. (i.e) Distributed executor, etc.
  • User provided logger and log mode.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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