protected function _executeJob(Zend_Db_Table_Row_Abstract $job) { // Get job info $jobTypeTable = Engine_Api::_()->getDbtable('jobtypes', 'core'); $jobType = $jobTypeTable->find($job->jobtype_id)->current(); // Prepare data $data = array(); $where = array('job_id = ?' => $job->job_id, 'state = ?' => $job->state); if ($job->state == 'pending') { $data['state'] = 'active'; $data['started_date'] = new Zend_Db_Expr('NOW()'); $data['modified_date'] = new Zend_Db_Expr('NOW()'); } else { if ($job->state == 'sleeping') { $data['state'] = 'active'; $data['modified_date'] = new Zend_Db_Expr('NOW()'); } else { // wth is this? $this->getLog()->log('Job Execution Duplicate: ' . $jobType->title . ' ' . $job->state, Zend_Log::NOTICE); return; } } // Attempt lock $table = $job->getTable(); $affected = $table->update($data, $where); if (1 !== $affected) { $this->getLog()->log('Job Execution Failed Lock: ' . $jobType->title, Zend_Log::NOTICE); return; } // Refresh $job->refresh(); // Register fatal error handler if (!$this->_isShutdownRegistered) { register_shutdown_function(array($this, 'handleShutdown')); $this->_isShutdownRegistered = true; } // Signal execution $this->_isExecuting = true; $this->_executingJob = $job; $this->_executingJobType = $jobType; // Log if (APPLICATION_ENV == 'development') { $this->getLog()->log('Job Execution Start: ' . $jobType->title, Zend_Log::NOTICE); } // Initialize $isComplete = true; $wasIdle = false; $messages = array(); $progress = null; try { // Check job type if (!$jobType || !$jobType->plugin) { throw new Engine_Exception(sprintf('Missing job type with ID "%1$d"', $job->jobtype_id)); } // Get plugin $plugin = $jobTypeTable->getJobPlugin($job, $jobType); // Execute $plugin->execute(); // Cleanup $isComplete = (bool) $plugin->isComplete(); //$progress = $plugin->getProgress(); $wasIdle = $plugin->wasIdle(); // If job set itself to failed, it failed. Otherwise, job may have not // set a status if ($job->state == 'failed' || $job->state == 'cancelled') { $status = false; } else { $status = true; } } catch (Exception $e) { $messages[] = $e->getMessage(); $this->getLog()->log(sprintf('Job Execution Error: [%d] [%s] %s %s', $job->job_id, $jobType->type, $jobType->title, $e->__toString()), Zend_Log::ERR); $status = false; } // Log if (APPLICATION_ENV == 'development') { if ($status) { $this->getLog()->log(sprintf('Job Execution Complete: [%d] [%s] %s', $job->job_id, $jobType->type, $jobType->title), Zend_Log::NOTICE); } else { $this->getLog()->log(sprintf('Job Execution Complete (with errors): [%d] [%s] %s', $job->job_id, $jobType->type, $jobType->title), Zend_Log::ERR); } } // Update job $job->messages .= ltrim(join("\n", $messages) . "\n", "\n"); $job->modified_date = new Zend_Db_Expr('NOW()'); if (!$isComplete) { $job->is_complete = false; $job->state = 'sleeping'; } else { $job->is_complete = true; $job->state = $status ? 'completed' : 'failed'; $job->completion_date = new Zend_Db_Expr('NOW()'); } $job->save(); // Cleanup $this->_executingJobType = null; $this->_executingJob = null; $this->_isExecuting = false; }