Skip to content

mysticpants/Scheduler

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scheduler

A simple class to manage jobs with one-off and interval timers all of which can be cancelled. Can be used to create multiple jobs that actually share a single timer which may be helpful on the agent where the number of active timers is limited. This class also allows the user to pass parameters to the callbacks they provide for each job.

To add this library to your model, add the following lines to the top of your agent code:

#require "Scheduler.lib.nut:1.0.0"

Scheduler Usage

This class managers all the jobs and is used mostly for creating new jobs. Each method for creating a job will return the new Job class instance.

Constructor: Scheduler

The constructor for Scheduler takes no parameters.

Example
sch <- Scheduler();

Scheduler Methods

set(_duration, _callback, [...])

Start a new timer to execute the callback after the specified duration. Returns the new job.

Parameter Type Required Default Description
_duration float Yes N/A The duration of the timer in seconds
_callback function Yes N/A The function to run when the timer finishes
... any No N/A Optional parameters that will be passed to the callback

at(_time, _callback, [...])

Create a new job with a callback to execute at a specified time. The time can either be provided as an integer (do NOT provide a float) representing the number of seconds that have elapsed since midnight on 1 January 1970 OR as a string in the following format: "January 01, 2017 12:30 PM". Returns the new job.

Parameter Type Required Default Description
_time integer/string Yes N/A The time when the timer should end
_callback function Yes N/A The function to run when the timer finishes
... any No N/A Optional parameters that will be passed to the callback

repeat(_interval, _callback, [...])

Create a new job with a callback that will repeat at the specified interval. Returns the new job.

Parameter Type Required Default Description
_interval float Yes N/A The interval between executions of the timer in seconds
_callback function Yes N/A The function to run when the timer finishes
... any No N/A Optional parameters that will be passed to the callback

repeat_from(_time, _interval, _callback, [...])

Create a new job with a callback to execute at the specified time as an integer or string (do NOT provide a float) and then repeat at the specified interval after that. Returns the new job.

Parameter Type Required Default Description
_time integer/string Yes N/A The time when the timer should end
_interval float Yes N/A The interval between executions of the timer in seconds
_callback function Yes N/A The function to run when the timer finishes
... any No N/A Optional parameters that will be passed to the callback

tzoffset(offset)

Set the offset for the timezone.

Parameter Type Required Default Description
offset integer No null The time offset in hours

Scheduler.Job

An instance of this class is created for each new job (timer) created. Any action performed on an existing job will be done using a method on this class.

Example
function myFunc(msg) {
    server.log(msg);
}
sch <- Scheduler();
job1 <- sch.set(5, myFunc, "Timer fired");
job1.cancel();

now()

Immediately execute this job. Returns this.

pause()

Pause the execution of the job's timer. Returns this.

unpause()

Unpause the execution of the job's timer. Returns this.

cancel()

Cancel this job. Returns this.

reset([rstDur])

Reset this job (i.e. restart the timer). Optionally, a different duration to the original can be passed to this method. This can't be used for jobs created with the Scheduler.at() method or during the first timer of jobs created with the Scheduler.repeatFrom() method (can be used for Scheduler.repeatFrom() jobs after they've fired the first time). Returns this.

Parameter Type Required Default Description
rstDur float No original duration The optional new timer duration

License

The Scheduler library is licensed under the MIT License.

About

Scheduler Utility Library for device/agent sides

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Squirrel 100.0%