/** * Called before the Controller::beforeFilter(). * * @param Controller $Controller A reference to the controller * @return void * @access public * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components */ public function initialize(Controller $Controller) { $connection = ConnectionManager::getDataSource($this->settings['connection']); if ($this->settings['type'] == 'mysql') { DJJob::setConnection($connection->getConnection()); } else { DJJob::configure(implode(';', array("{$this->settings['type']}:host={$connection->config['host']}", "dbname={$connection->config['database']}", "port={$connection->config['port']}", "user={$connection->config['login']}", "password={$connection->config['password']}"))); } }
/** * Initiate CakeDjjob Behavior * * @param Model $Model Model using the behavior * @param array $config * @return void * @access public */ public function setup(Model $model, $config = array()) { $this->settings = array_merge($this->settings, $config); $connection = ConnectionManager::getDataSource($this->settings['connection']); if ($this->settings['type'] == 'mysql') { DJJob::setConnection($connection->getConnection()); } else { DJJob::configure(implode(';', array("{$this->settings['type']}:host={$connection->config['host']}", "dbname={$connection->config['database']}", "port={$connection->config['port']}", "user={$connection->config['login']}", "password={$connection->config['password']}"))); } }
/** * 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); } }
<?php function assert_handler($file, $line, $code, $desc = null) { printf("Assertion failed at %s:%s: %s: %s\n", $file, $line, $code, $desc); } assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_QUIET_EVAL, 1); assert_options(ASSERT_CALLBACK, 'assert_handler'); date_default_timezone_set('America/New_York'); require dirname(__FILE__) . "/../DJJob.php"; DJJob::configure(['driver' => 'mysql', 'host' => '127.0.0.1', 'dbname' => 'djjob', 'user' => 'root', 'password' => 'root']); 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);
<?php function assert_handler($file, $line, $code, $desc = null) { printf("Assertion failed at %s:%s: %s: %s\n", $file, $line, $code, $desc); } assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_QUIET_EVAL, 1); assert_options(ASSERT_CALLBACK, 'assert_handler'); date_default_timezone_set('America/New_York'); require dirname(__FILE__) . "/../DJJob.php"; DJJob::configure("mysql:host=127.0.0.1;dbname=djjob;", array("mysql_user" => "root", "mysql_pass" => "root")); 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);