Skip to content

Instantly share code, notes, and snippets.

@chadhutchins
Created April 21, 2015 21:04
Show Gist options
  • Save chadhutchins/2fe3dd0bce97413b2c5b to your computer and use it in GitHub Desktop.
Save chadhutchins/2fe3dd0bce97413b2c5b to your computer and use it in GitHub Desktop.
Classes for creating your own custom queue table to run on Sugar's On-demand Scheduler
<?php
$job_strings[] = 'YourJobQueue';
function YourJobQueue()
{
$queues_to_process = array(
'FirstQueue',
'SecondQueue',
'ThirdQueue',
);
// try to go through each job that needs to be processed and start their work
foreach ($queues_to_process => $queue)
{
// forge the $job and get started
$result = YourSchedulerClass::forge($queue)->start();
// check result to see if we're out of allotted self-imposed scheduler time
if ($result['elapsed_time'] === false || !preg_match("/^[0-9]+$/",$result['elapsed_time']))
{
// there's no time left to keep going
// go ahead and return true so sugar scheduler keeps doing its thing
// it'll pick up from here next time
return true;
}
}
// if everything finishes
return true;
}
// worker class that simply creates and returns the SchedulerQueue class
class YourSchedulerClass
{
function __construct(){}
public static function forge(){}
}
// abstract driver class that all of your custom queues extend from
abstract class YourSchedulerQueue
{
function __construct(){}
public static function forge(){}
public function start(){
// start the timer
$this->start_timer();
}
// this gets implemented by the individual queue classes
public abstract function run(){}
public function end(){
// the job finished
$this->update_timer();
return;
}
public function start_timer(){}
public function update_timer(){}
public function check_timer(){}
}
// class that handles the work for this specific queue
// you may have several different custom queues
// they can all extend the YourSchedulerQueue driver class
class YourSchedulerQueue_FirstQueue extends YourSchedulerQueue
{
function __construct(){}
public function run(){
// get the next job name
$job_name = $this->get_next_job_name();
// get the next until time - this is used because
// we are able to batch calls by the time the come
// in and the specific task
$until_time = $this->get_next_until_time();
// get some jobs to run
while ($jobs = $this->get_jobs_to_process($job_name,$until_time))
{
// run the jobs
$this->process_jobs($jobs);
// remove the jobs from the queue
// you can handle what happens on error, maybe
// they don't need to be deleted on error
$this->delete($jobs);
// make sure we have time to do more work, if not, return
// we can pick up the work next time
$this->check_timer();
}
}
public function process_jobs(){}
public function delete_jobs(){}
public function get_next_job_name(){}
public function get_next_until_time(){}
public function get_jobs_to_process(){}
}
// abstract driver class that all of your custom jobs extend from
abstract class YourSchedulerJob
{
public static function forge(){}
// this method will be used throughout your addon/integration
// it is what is called to add jobs into your queue
public function queue(){}
// this gets implemented by the individual job classes
abstract function run(){}
}
// class that runs individual jobs that are in the queues/
// you'll have at least one of these for each individual queue
// you'll need one for each type of job the parent queue can process
class YourSchedulerJob_FirstJob extends YourSchedulerJob
{
public function __construct(){}
// whatever this job is suppose to do goes here
public function run(){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment