/** * Execution method always used for tasks * * @return void */ public function execute() { Configure::write('debug', $this->params['debug']); if (empty($this->params['queue'])) { $this->cakeError('error', array(array('code' => '', 'name' => '', 'message' => 'No queue set'))); } $status = DJJob::status($this->params['queue']); foreach ($status as $name => $count) { $this->out(sprintf("%s Jobs: %d", Inflector::humanize($name), $count)); } }
/** * Override startup * * @access public */ public function startup() { parent::startup(); ini_set('unserialize_callback_func', 'unserialize_jobs'); $connection = ConnectionManager::getDataSource($this->params['connection']); if ($this->params['type'] == 'mysql') { DJJob::setConnection($connection->getConnection()); } else { DJJob::configure(implode(';', array("{$this->params['type']}:host={$connection->config['host']}", "dbname={$connection->config['database']}", "port={$connection->config['port']}", "user={$connection->config['login']}", "password={$connection->config['password']}"))); } }
protected function setupDJJob() { $config = Mage::getConfig()->getResourceConnectionConfig("default_setup"); $dsn = ""; if (strpos($config->host, '/') !== false) { $dsn = "mysql:unix_socket=" . $config->host . ";dbname=" . $config->dbname; } else { $dsn = "mysql:host=" . $config->host . ";dbname=" . $config->dbname . ";port=" . $config->port; } DJJob::configure($dsn, array('mysql_user' => $config->username, 'mysql_pass' => $config->password)); if (!empty($config->initStatements)) { DJJob::runQuery($config->initStatements); } }
/** * Returns an array containing the status of a given queue * * @param Model $Model Model using the behavior * @param string $queue * @return array **/ public function status(Model $Model, $queue = "default") { return DJJob::status($queue); }
echo "Hello {$this->name}!\n"; sleep(1); } } class FailingJob { public function perform() { sleep(1); throw new Exception("Uh oh"); } } $status = DJJob::status(); assert('$status["outstanding"] == 0', "Initial outstanding status is incorrect"); assert('$status["locked"] == 0', "Initial locked status is incorrect"); assert('$status["failed"] == 0', "Initial failed status is incorrect"); assert('$status["total"] == 0', "Initial total status is incorrect"); printf("=====================\nStarting run of DJJob\n=====================\n\n"); DJJob::enqueue(new HelloWorldJob("delayed_job")); DJJob::bulkEnqueue(array(new HelloWorldJob("shopify"), new HelloWorldJob("github"))); DJJob::enqueue(new FailingJob()); // Test unicode support using the classic, rails snowman: http://www.fileformat.info/info/unicode/char/2603/browsertest.htm DJJob::enqueue(new HelloWorldJob(html_entity_decode("☃", ENT_HTML5, "UTF-8"))); $worker = new DJWorker(array("count" => 5, "max_attempts" => 2, "sleep" => 10)); $worker->start(); printf("\n============\nRun complete\n============\n\n"); $status = DJJob::status(); assert('$status["outstanding"] == 0', "Final outstanding status is incorrect"); assert('$status["locked"] == 0', "Final locked status is incorrect"); assert('$status["failed"] == 1', "Final failed status is incorrect"); assert('$status["total"] == 1', "Final total status is incorrect");
/** * Returns a new job ordered by most recent first * why this? * run newest first, some jobs get left behind * run oldest first, all jobs get left behind * @return DJJob */ public function getNewJob() { # we can grab a locked job if we own the lock $rs = $this->runQuery("\n SELECT id\n FROM " . self::$jobsTable . "\n WHERE queue = ?\n AND (run_at IS NULL OR NOW() >= run_at)\n AND (locked_at IS NULL OR locked_by = ?)\n AND failed_at IS NULL\n AND attempts < ?\n ORDER BY created_at DESC\n LIMIT 10\n ", array($this->queue, $this->name, $this->max_attempts)); // randomly order the 10 to prevent lock contention among workers shuffle($rs); foreach ($rs as $r) { $job = new DJJob($this->name, $r["id"], array("max_attempts" => $this->max_attempts, "fail_on_output" => $this->fail_on_output)); if ($job->acquireLock()) { return $job; } } return false; }
/** * Enables requeing of an email * * @param datetime $sendAt MySQL-compatible datetime * @param string $queue Name of queue * @return void */ public function sendLater($sendAt = null, $queue = "email") { DJJob::enqueue($this, $queue, $sendAt); }
require dirname(__FILE__) . "/../DJJob.php"; DJJob::configure("mysql:host=127.0.0.1;dbname=djjob", array("mysql_user" => "root", "mysql_pass" => "")); DJJob::runQuery("\nDROP TABLE IF EXISTS `jobs`;\nCREATE TABLE `jobs` (\n`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,\n`handler` VARCHAR(255) NOT NULL,\n`queue` VARCHAR(255) NOT NULL DEFAULT 'default',\n`attempts` INT UNSIGNED NOT NULL DEFAULT 0,\n`run_at` DATETIME NULL,\n`locked_at` DATETIME NULL,\n`locked_by` VARCHAR(255) NULL,\n`failed_at` DATETIME NULL,\n`error` VARCHAR(255) NULL,\n`created_at` DATETIME NOT NULL\n) ENGINE = MEMORY;\n"); class HelloWorldJob { public function __construct($name) { $this->name = $name; } public function perform() { echo "Hello {$this->name}!\n"; sleep(1); } } class FailingJob { public function perform() { sleep(1); throw new Exception("Uh oh"); } } var_dump(DJJob::status()); DJJob::enqueue(new HelloWorldJob("delayed_job")); DJJob::bulkEnqueue(array(new HelloWorldJob("shopify"), new HelloWorldJob("github"))); DJJob::enqueue(new FailingJob()); $worker = new DJWorker(array("count" => 5, "max_attempts" => 2, "sleep" => 10)); $worker->start(); var_dump(DJJob::status());
/** * Returns an array containing the status of a given queue * * @param string $queue * @return array **/ function status($queue = "default") { return DJJob::status($queue); }