metre

package module
v0.0.0-...-0c25e90 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2016 License: MIT Imports: 10 Imported by: 0

README

metre

Golang cron framework. Scheduling through ØMQ push-pull and state managed through redis

Requirements

Installation

go get github.com/johnhof/metre

Documentation

Example

// Package main demo's metre
package main

import (
  "github.com/johnhof/metre"
)


func main () {

  // Create
  m, _ := metre.New("127.0.0.1:5555", "127.0.0.1:6379") // (zmq, redis)

  // Add tasks
  m.add(metre.Task{
    ID: "F", // used to prevent collision across tasks
    Interval: "* * * * * *", // Cron expression
    Schedule: func(t metre.TaskConfig, s metre.Scheduler, c metre.cache, q metre.queue)  {
      t.UID = "TestID" // overwrite the generated UID with a static namespace
      s.Schedule(t) // only schedules if "TestID" is not being processed ("F-TestId" not cached in a processing state)
      s.SetExpire(t, 15000) // set expire in milliseconds
    },
    Process: func(t metre.TaskConfig, s metre.Scheduler, c metre.Cache, q metre.Queue)  {
      log.Info("Processing: " + t.class + "-" + t.uid)
  })

  m.add(metre.Task{
    ID: "B", // used to prevent collision across tasks
    Interval: "@every minute", // Cron Schedule
    Schedule: func(s metre.Scheduler, c metre.Cache, q metre.Queue)  {
      t.UID = "TestID" // overwrite the generated UID with a static namespace
      s.ForceSchedule(t) // schedule regardless of task state
    },
    Process: func(t metre.TaskInstance, s metre.Scheduler, c metre.Cache, q metre.Queue)  {
      log.Info("Processing: " + t.class + "-" + t.uid)
    },
  })

  go m.RunSlave() // run slave in background
  m.RunMaster() // run master

  // this approach means we can run master and slave
  // on separate servers from the same code base
  // allowing us to scale them separately
}

Authors

Documentation

Overview

Package metre is used to schedule end execute cron jobs in a simplified fashion

Index

Constants

View Source
const CACHEPORT string = "6379" // Default port for cache
View Source
const LOCALHOST string = "127.0.0.1" // Default host for cache and queue
View Source
const QUEUEPORT string = "5555" // Default port for queue

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	ConnPool *redis.Pool
}

func NewCache

func NewCache(uri string) (Cache, error)

New acts as a queue constructor

func (Cache) Delete

func (c Cache) Delete(key string) (interface{}, error)

Delete clears the cache value for the provided key

func (Cache) Expire

func (c Cache) Expire(key string, mSeconds int)

Expire sets the cache value for the provided key

func (Cache) Get

func (c Cache) Get(key string) (string, error)

Get gets the cache value for the provided key

func (Cache) Set

func (c Cache) Set(key string, val string) (interface{}, error)

Set sets the cache value for the provided key

type Metre

type Metre struct {
	Cron      cron.Cron
	Queue     Queue
	Cache     Cache
	Scheduler Scheduler
	TaskMap   map[string]Task
}

func New

func New(queueUri string, cacheUri string) (Metre, error)

New creates a new scheduler to manage task scheduling and states

func (*Metre) Add

func (m *Metre) Add(t Task)

Add adds a cron job task to schedule and process

func (*Metre) Process

func (m *Metre) Process(ID string) (string, error)

Scheduler processes a singular cron task

func (*Metre) Schedule

func (m *Metre) Schedule(ID string) (string, error)

Schedule schedules a singular cron task

func (*Metre) StartMaster

func (m *Metre) StartMaster()

func (*Metre) StartSlave

func (m *Metre) StartSlave()

type Queue

type Queue struct {
	URI        string
	PushSocket *zmq.Socket // Socket to push messages to
	PullSocket *zmq.Socket // SOcket to pull mesasges form
}

func NewQueue

func NewQueue(uri string) (Queue, error)

New acts as a queue constructor

func (Queue) BindPush

func (q Queue) BindPush() error

BindPush binds to the socket to push

func (Queue) ConnectPull

func (q Queue) ConnectPull() error

ConnectPull connects to the socket to listen for messages

func (Queue) Pop

func (q Queue) Pop() string

Pop pulls off the last element from the queue

func (Queue) Push

func (q Queue) Push(msg string) (int, error)

Push pushes and element onto the queue

type Scheduler

type Scheduler struct {
	Queue Queue
	Cache Cache
}

func NewScheduler

func NewScheduler(q Queue, c Cache) Scheduler

func (Scheduler) ForceSchedule

func (s Scheduler) ForceSchedule(t TaskRecord) (string, error)

ForceSchedule schedules a task in the cache and queue regardless of tasks actively waiting to be processed

func (Scheduler) Schedule

func (s Scheduler) Schedule(t TaskRecord) (string, error)

Schedule schedules a task in the cache and queue if no task is actively waiting to be processed

func (Scheduler) SetExpire

func (s Scheduler) SetExpire(t TaskRecord, time int)

SetExpire set teh expiration for a task

type Task

type Task struct {
	ID       string // Type Type of task (user as class prefix in cache)
	Interval string // Schedule String in cron notation
	Schedule func(t TaskRecord, s Scheduler, c Cache, q Queue)
	Process  func(t TaskRecord, s Scheduler, c Cache, q Queue)
}

type TaskRecord

type TaskRecord struct {
	ID        string
	UID       string
	UpdatedAt string
	CreatedAt string
	State     string
}

func NewTaskRecord

func NewTaskRecord(id string) TaskRecord

NewTaskRecord takes seed data and returns a full TaskRecord instance

func NewTaskRecordWithID

func NewTaskRecordWithID(id string, uid string) TaskRecord

NewTaskRecordWithID takes seed data and returns a full TaskRecord instance

func ParseTask

func ParseTask(s string) (TaskRecord, error)

ParseTask returns a task instance from a serialized json string

func (*TaskRecord) CanReschedule

func (t *TaskRecord) CanReschedule() bool

CanReschedule returns whether or not the task can be rescheduled

func (*TaskRecord) SetProcessing

func (t *TaskRecord) SetProcessing()

SetProcessing set the state of the task to "PROCESSING"

func (*TaskRecord) SetScheduled

func (t *TaskRecord) SetScheduled()

SetProcessing set the state of the task to "SCHEDULED"

func (*TaskRecord) SetUnscheduled

func (t *TaskRecord) SetUnscheduled()

SetProcessing set the state of the task to "UNSCHEDULED"

func (TaskRecord) ToString

func (t TaskRecord) ToString() (string, error)

ToString returns a serialized json string representation of the task

func (*TaskRecord) TriggerUpdate

func (t *TaskRecord) TriggerUpdate()

TriggerUpdate sets the UpdatedAt property to teh current time

Jump to

Keyboard shortcuts

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