Giter Site home page Giter Site logo

goscheduler's Introduction

goscheduler

go program that takes a "task" function and a time.Duration interval and will call the task continuously until the program exists or Done() method is called on the scheduler object.

example

package main

import (
  "fmt"
  "time"
  "github.com/swys/goscheduler"
)

func task() {
  fmt.Printf("I am a task\n")
}

func main() {
  // setup interval
  interval := time.Duration(1) * time.Second
  // get scheduler object and schedulerFunc (go routine used to run the task based on interval)
  scheduler := goscheduler.NewScheduler(interval, task)
  // start the interval to run the tasks
  go scheduler.schedulerFunc()
  // sleep for 2 secs to simulate a later time when you want to stop the scheduler
  time.Sleep(2 * time.Second)
  // stop the scheduler
  scheduler.Done()
}

functions

NewScheduler(interval time.Duration, task func()) (*Scheduler, func())

this creates a new scheduler object. The object contains a schedulerFunc method which is the func() you passed into NewScheduler. You must start the scheduler by running this schedulerFunc method as a go routine like below :

// create scheduler object
scheduler := NewScheduler(interval, task)
// start the scheduler
go scheduler.schedulerFunc()
arguments:
argument type example description
interval time.Duration time.Duration(1)*time.Second how often should your task run?
task func() func task() {...} function to run
return:
return type description
scheduler *Scheduler scheduler object with Done() method for stopping scheduler

methods

schedulerFunc()

This method runs the function that was originally passed into NewScheduler to create the scheduler object. You must call this method to start the scheduler interval.

Done()

call this method on the Scheduler object when you want to stop the scheduler from running. This method is safe to call as many time as you want, weather the Scheduler go routine is running or not or even if the scheduler has not been initialized yet with an interval and a task function.

scheduler := NewScheduler(interval, task)
go scheduler.schedulerFunc()
// some time in the future when you want to stop the scheduler
scheduler.Done()
// from this point on, the task will not be called anymore

IsRunning() int64

call this method when you want to find out if the Scheduler's go routine is running or not. This will return an int64 and a value of 0 means the scheduler is NOT running. A value of 1 means the scheduler is running.

scheduler := NewScheduler(interval, task)
go scheduler.schedulerFunc()
// find out if scheduler is running
isRunning := scheduler.IsRunning() // returns 1

test

make test

license

MIT

goscheduler's People

Contributors

swys avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.