Created
April 29, 2016 20:14
-
-
Save sameg14/778022d9124357bd92f411a7c035fcdd to your computer and use it in GitHub Desktop.
Schedule jobs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tib\CoreBundle\Job\Scheduler; | |
use \Resque; | |
use \Resque_Worker; | |
use \Resque_Job_Status; | |
use Tib\CoreBundle\Job\Data\Broker as DataBroker; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Responsible for scheduling jobs | |
* Class JobScheduler | |
* @package Job\Scheduler | |
*/ | |
class JobScheduler | |
{ | |
/** | |
* Name of queue | |
* @var string | |
*/ | |
protected $queue = 'default'; | |
/** | |
* Fully namespaced job class | |
* @var string | |
*/ | |
protected $jobClass; | |
/** | |
* Array of any data you want to send to the job | |
* @var array | |
*/ | |
protected $jobData = array(); | |
/** | |
* JobId from a newly created job | |
* @var int | |
*/ | |
protected $jobId; | |
/** | |
* URI of redis server | |
* @var string | |
*/ | |
protected $redisServer; | |
/** | |
* Port number | |
* @var int | |
*/ | |
protected $redisPort; | |
/** | |
* Symfony service container | |
* @var ContainerInterface | |
*/ | |
protected $container; | |
/** | |
* JobScheduler constructor. | |
* @param string $redisServer | |
* @param int $redisPort | |
*/ | |
public function __construct($redisServer = 'job.theillbeat.com', $redisPort = 6379) | |
{ | |
$this->redisServer = $redisServer; | |
$this->redisPort = $redisPort; | |
Resque::setBackend($redisServer . ':' . $redisPort); | |
} | |
/** | |
* Schedule a job i.e. add it to the redis backed queue | |
* @return int jobId from the newly created job | |
*/ | |
public function schedule() | |
{ | |
return $this->jobId = Resque::enqueue($this->queue, $this->jobClass, $this->jobData, $trackStatus = true); | |
} | |
/** | |
* Get status of a job. | |
* @return string | |
*/ | |
public function getStatus() | |
{ | |
$job = new Resque_Job_Status($this->getJobId()); | |
$jobStatusCode = $job->get(); | |
switch ($jobStatusCode) { | |
case Resque_Job_Status::STATUS_WAITING: | |
return 'Waiting'; | |
break; | |
case Resque_Job_Status::STATUS_COMPLETE: | |
return 'Complete'; | |
break; | |
case Resque_Job_Status::STATUS_FAILED: | |
return "Failed"; | |
break; | |
case Resque_Job_Status::STATUS_RUNNING: | |
return 'Running'; | |
break; | |
default: | |
return 'Unknown! Code: ' . $jobStatusCode; | |
} | |
} | |
/** | |
* Get any data that the worker set using DataBroker | |
* @return mixed | |
*/ | |
public function getReturnedData() | |
{ | |
$broker = new DataBroker(); | |
$broker->setJobId($this->getJobId()); | |
return $broker->get(); | |
} | |
/** | |
* Get a list of all queues from redis. | |
* @return array Array of queues. | |
*/ | |
public function getQueues() | |
{ | |
return Resque::queues(); | |
} | |
/** | |
* How many jobs are currently in a queue? | |
* @return int how many jobs are in the queue | |
*/ | |
public function size() | |
{ | |
return Resque::size($this->queue); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getWorkers() | |
{ | |
return Resque_Worker::all(); | |
} | |
/** | |
* @param string $jobClass just the non namespaced worker clss | |
*/ | |
public function setJobClass($jobClass) | |
{ | |
$this->jobClass = 'Tib\\CoreBundle\\Job\\Worker\\' . $jobClass; | |
} | |
/** | |
* @return string | |
*/ | |
public function getJobClass() | |
{ | |
return $this->jobClass; | |
} | |
/** | |
* @param array $jobData | |
*/ | |
public function setJobData($jobData) | |
{ | |
$this->jobData = $jobData; | |
} | |
/** | |
* @return array | |
*/ | |
public function getJobData() | |
{ | |
return $this->jobData; | |
} | |
/** | |
* @param int $jobId | |
*/ | |
public function setJobId($jobId) | |
{ | |
$this->jobId = $jobId; | |
} | |
/** | |
* @return int | |
*/ | |
public function getJobId() | |
{ | |
return $this->jobId; | |
} | |
/** | |
* @param string $queue | |
*/ | |
public function setQueue($queue) | |
{ | |
$this->queue = $queue; | |
} | |
/** | |
* @return string | |
*/ | |
public function getQueue() | |
{ | |
return $this->queue; | |
} | |
/** | |
* @return ContainerInterface | |
*/ | |
public function getContainer() | |
{ | |
return $this->container; | |
} | |
/** | |
* @param ContainerInterface $container | |
*/ | |
public function setContainer($container) | |
{ | |
$this->container = $container; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment