Changeset 7246
- Timestamp:
- 01/31/08 13:26:52 (10 months ago)
- Files:
-
- plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfJobHandler.class.php (modified) (2 diffs)
- plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfJobHandlerInterface.interface.php (deleted)
- plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfMailJobHandler.class.php (modified) (3 diffs)
- plugins/sfJobQueuePlugin/trunk/lib/model/sfJob.php (modified) (9 diffs)
- plugins/sfJobQueuePlugin/trunk/modules/sfJob/actions/actions.class.php (modified) (1 diff)
- plugins/sfJobQueuePlugin/trunk/modules/sfJob/config/generator.yml (modified) (2 diffs)
- plugins/sfJobQueuePlugin/trunk/modules/sfJob/templates/_status.php (modified) (1 diff)
- plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/actions/actions.class.php (modified) (2 diffs)
- plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/config/generator.yml (modified) (1 diff)
- plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/templates/_scheduler.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfJobHandler.class.php
r7231 r7246 3 3 * This file is part of the sfJobQueuePlugin package. 4 4 */ 5 class sfJobHandler 5 6 /** 7 * Interface for job handlers. 8 * 9 * @author Xavier Lacot <xavier@lacot.org> 10 * @author Tristan Rivoallan <tristan@rivoallan.net> 11 * @see http://trac.symfony-project.com/wiki/sfJobQueuePlugin#Creatinganewjobtype 12 */ 13 abstract class sfJobHandler 6 14 { 7 15 protected $logger; … … 17 25 } 18 26 19 public function run($params) 27 /** 28 * Returns the lists of parameters accepted by the job. 29 * 30 * The array must have the form : 31 * 32 * <code> 33 * array('param1', 'param2', 'param3', etc.) 34 * </code> 35 * 36 * @return array 37 */ 38 public function getParamFields() 20 39 { 21 return sfJobQueue::SUCCESS;40 return array(); 22 41 } 42 43 /** 44 * Executes the job. 45 * 46 * @param array $params An array of valued job parameters. The key is the 47 * parameter name, as 48 * what returns the 49 * getParamFields() method and the value is parameter's 50 * value. 51 * 52 * @throws Exception When job execution does not succeed. 53 * @return sfJobQueue::SUCCESS if job execution succeeded. 54 */ 55 abstract public function run(array $params); 23 56 24 57 /** 25 58 * Called after a job creation 26 59 * 27 * @param objectthe related job60 * @param sfJob the related job 28 61 * @param array array of params of the job 29 62 */ 30 public static function postSave( $job,$params)63 public static function postSave(sfJob $job, array $params) 31 64 { 32 65 } plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfMailJobHandler.class.php
r5658 r7246 1 1 <?php 2 2 3 class sfMailJobHandler extends sfJobHandler implements sfJobHandlerInterface3 class sfMailJobHandler extends sfJobHandler 4 4 { 5 5 public function getParamFields() … … 8 8 } 9 9 10 public function run( $params)10 public function run(array $params) 11 11 { 12 if ( false && mail($params['to'],13 $params['subject'], 14 $params['message'], 12 if (mail($params['to'], 13 $params['subject'], 14 $params['message'], 15 15 'From: '.$params['from'])) 16 16 { … … 20 20 { 21 21 throw new Exception('There was an error while sending the mail.'); 22 return sfJob::ERROR;23 22 } 24 23 } plugins/sfJobQueuePlugin/trunk/lib/model/sfJob.php
r5658 r7246 2 2 /* 3 3 * This file is part of the sfJobQueuePlugin package. 4 * 4 * 5 5 * (c) 2007 Xavier Lacot <xavier@lacot.org> 6 * 6 * 7 7 * For the full copyright and license information, please view the LICENSE 8 8 * file that was distributed with this source code. … … 10 10 11 11 /** 12 * This plugins enables job queues into Symfony. It includes all the common job 13 * queues tasks (start, stop, scheduling through job election strategies, etc.), 14 * command line tasks, and a graphical interface for managing queues and jobs. 15 * Using a job queue can be useful when asynchronised server-side operations 16 * have to be performed (periodically grabbing a RSS feed, automatically sending 12 * This plugins enables job queues into Symfony. It includes all the common job 13 * queues tasks (start, stop, scheduling through job election strategies, etc.), 14 * command line tasks, and a graphical interface for managing queues and jobs. 15 * Using a job queue can be useful when asynchronised server-side operations 16 * have to be performed (periodically grabbing a RSS feed, automatically sending 17 17 * emails, etc.) or in environments without a cron access. 18 * 18 * 19 19 * @author Xavier Lacot <xavier@lacot.org> 20 20 * @see http://www.symfony-project.com/trac/wiki/sfJobQueuePlugin … … 29 29 const SUCCESS = 9; 30 30 31 public static $status_text = array(sfJob::ERROR => 'error', 32 sfJob::CANCELLED => 'cancelled', 33 sfJob::STOPPED => 'stopped', 34 sfJob::RUNNING => 'running', 35 sfJob::IDLE => 'idle', 31 public static $status_text = array(sfJob::ERROR => 'error', 32 sfJob::CANCELLED => 'cancelled', 33 sfJob::STOPPED => 'stopped', 34 sfJob::RUNNING => 'running', 35 sfJob::IDLE => 'idle', 36 36 sfJob::SUCCESS => 'success'); 37 37 … … 90 90 /** 91 91 * Returns the status of the job, as a text 92 * 92 * 93 93 * @return string 94 94 */ … … 105 105 { 106 106 set_error_handler(array($this, 'handleRuntimeError')); 107 $status_text = ''; 107 108 108 109 // increment number of tries … … 119 120 { 120 121 $status = $jobhandler->run($params); 121 122 if ($status == = '')122 123 if ($status == '' || $status == null) 123 124 { 124 125 $status = self::SUCCESS; 125 126 } 127 128 $this->setMessage(''); 126 129 } 127 130 catch (Exception $e) … … 130 133 { 131 134 $status = self::ERROR; 132 $this->setMessage($e->getMessage()); 133 } 134 else 135 { 136 // messages should be logged here 137 } 135 } 136 137 // messages should be logged here 138 $status_text = $e->getMessage(); 139 $this->setMessage($status_text); 138 140 } 139 141 140 142 if (!$this->getIsRecuring()) 141 143 { 142 if (($this->getTries() >= $this->getMaxTries()) 143 && ($status != self::SUCCESS) 144 if (($this->getTries() >= $this->getMaxTries()) 145 && ($status != self::SUCCESS) 144 146 && ($status != self::ERROR)) 145 147 { … … 158 160 $this->save(); 159 161 restore_error_handler(); 162 return $status_text; 160 163 } 161 164 … … 205 208 206 209 // Max number of tries reached, change status to "error" 207 if (!$this->getIsRecuring() 210 if (!$this->getIsRecuring() 208 211 && ($this->getTries() >= $this->getMaxTries())) 209 212 { plugins/sfJobQueuePlugin/trunk/modules/sfJob/actions/actions.class.php
r7231 r7246 136 136 $message = $sf_job->run(); 137 137 138 if ($message == '' )138 if ($message == '' || $message == null) 139 139 { 140 140 if ($sf_job->getMessage() == '') 141 141 { 142 $message = 'Job completed';142 $message = sfConfig::get('sf_i18n') ? $this->getContext()->getI18n()->__('Job completed') : 'Job completed'; 143 143 } 144 144 else plugins/sfJobQueuePlugin/trunk/modules/sfJob/config/generator.yml
r7231 r7246 6 6 7 7 fields: 8 name: { name: Name } 8 9 tries: { name: Number of tries } 9 created_at: { name: Created at, params: date_format=MM/dd HH:mm } 10 scheduled_at: { name: Scheduled at, params: date_format=MM/dd HH:mm } 11 last_tried_at: { name: Last tried at, params: date_format=MM/dd HH:mm } 12 completed_at: { name: Completed at, params: date_format=MM/dd HH:mm } 10 retry_delay: { name: Retry delay (in seconds) } 11 created_at: { name: Created at, params: date_format=MM/dd HH:mm:ss } 12 scheduled_at: { name: Scheduled at, params: date_format=MM/dd HH:mm:ss } 13 last_tried_at: { name: Last tried at, params: date_format=MM/dd HH:mm:ss } 14 completed_at: { name: Completed at, params: date_format=MM/dd HH:mm:ss } 13 15 14 16 list: … … 28 30 title: Job edition 29 31 display: 30 "Job informations": [sf_job_queue_id, _type, max_tries, is_recuring, retry_delay, scheduled_at]32 "Job informations": [sf_job_queue_id, _type, name, max_tries, is_recuring, retry_delay, scheduled_at] 31 33 "Job parameters": [_params] 32 34 "Job status": [status, tries, created_at, last_tried_at, completed_at] 33 35 fields: 36 name: { name: Name of the job (informational only) } 34 37 status: { type: plain } 35 38 tries: { type: plain } plugins/sfJobQueuePlugin/trunk/modules/sfJob/templates/_status.php
r5658 r7246 1 1 <?php echo $sf_job->getStatusText(); ?> 2 <?php if (($sf_job->getStatus() == sfJob::ERROR) 3 && ($sf_job->getMessage() != '')): ?> 2 <?php if ($sf_job->getMessage() != ''): ?> 4 3 <?php 5 echo image_tag(sfConfig::get('sf_admin_web_dir').'/images/help.png', 6 array('align' => 'absmiddle', 7 'alt' => $sf_job->getMessage(), 4 echo image_tag(sfConfig::get('sf_admin_web_dir').'/images/help.png', 5 array('align' => 'absmiddle', 6 'alt' => $sf_job->getMessage(), 8 7 'title' => $sf_job->getMessage())); 9 8 ?> plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/actions/actions.class.php
r5658 r7246 9 9 class sfJobQueueActions extends autosfJobQueueActions 10 10 { 11 public function executeEditJob()12 {13 $sf_job_queue_id = $this->getRequestParameter('sf_job_queue_id', $this->getRequestParameter('id'));14 $this->sf_job_queue = sfJobQueuePeer::retrieveByPk($sf_job_queue_id);15 $this->forward404Unless($this->sf_job_queue);16 17 $this->sf_job = $this->getsfJobOrCreate();18 19 if ($this->getRequest()->getMethod() == sfRequest::POST)20 {21 $this->updatesfJobFromRequest();22 $this->sf_job->save();23 $this->setFlash('notice', 'Your modifications have been saved');24 25 if ($this->getRequestParameter('save_and_add'))26 {27 return $this->redirect('sfJobQueue/createJob?sf_job_queue_id='.$sf_job_queue_id);28 }29 else if ($this->getRequestParameter('save_and_list'))30 {31 return $this->redirect('sfJobQueue/listJob?sf_job_queue_id='.$sf_job_queue_id);32 }33 else34 {35 return $this->redirect('sfJobQueue/editJob?sf_job_queue_id='.$this->sf_job->getSfJobQueue()->getId().'&sf_job_id='.$this->sf_job->getId());36 }37 }38 else39 {40 $this->sf_job_queues = sfJobQueuePeer::doSelect(new Criteria());41 }42 }43 44 public function executeSaveJob()45 {46 return $this->forward('sfJobQueue', 'editJob');47 }48 49 11 public function executeSwitchStatus() 50 12 { … … 70 32 $this->forward404Unless($this->job_queue); 71 33 } 72 73 private function getsfJobOrCreate()74 {75 $sf_job_id = $this->getRequestParameter('sf_job_id');76 77 if ($sf_job_id)78 {79 $sf_job = sfJobPeer::retrieveByPk($sf_job_id);80 $this->forward404Unless($sf_job);81 }82 else83 {84 $sf_job = new sfJob();85 $sf_job->setSfJobQueueId($this->sf_job_queue->getId());86 }87 88 return $sf_job;89 }90 91 private function updatesfJobFromRequest()92 {93 $sf_job = $this->getRequestParameter('sf_job');94 $sf_job_queue_id = $this->getRequestParameter('sf_job_queue_id');95 96 if ($sf_job_queue_id)97 {98 $this->sf_job->setSfJobQueueId($sf_job_queue_id);99 }100 101 if (isset($sf_job['type']))102 {103 $this->sf_job->setType($sf_job['type']);104 }105 106 if (isset($sf_job['max_tries']))107 {108 $this->sf_job->setMaxTries($sf_job['max_tries']);109 }110 111 if (isset($sf_job['retry_delay']))112 {113 $this->sf_job->setRetryDelay($sf_job['retry_delay']);114 }115 116 if (isset($sf_job['message']))117 {118 $this->sf_job->setMessage($sf_job['message']);119 }120 121 if (isset($sf_job['priority']))122 {123 $this->sf_job->setPriority($sf_job['priority']);124 }125 126 if (isset($sf_job['scheduled_at']))127 {128 if ($sf_job['scheduled_at'])129 {130 try131 {132 $dateFormat = new sfDateFormat($this->getUser()->getCulture());133 134 if (!is_array($sf_job['scheduled_at']))135 {136 $value = $dateFormat->format($sf_job['scheduled_at'], 'I', $dateFormat->getInputPattern('g'));137 }138 else139 {140 $value_array = $sf_job['scheduled_at'];141 $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');142 }143 144 $this->sf_job->setScheduledAt($value);145 }146 catch (sfException $e)147 {148 // not a date149 }150 }151 else152 {153 $this->sf_job->setScheduledAt(null);154 }155 }156 157 if (isset($sf_job['status']))158 {159 $this->sf_job->setStatus($sf_job['status']);160 }161 162 if (isset($sf_job['params']))163 {164 $this->sf_job->setParams($sf_job['params']);165 }166 }167 34 } plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/config/generator.yml
r7191 r7246 8 8 name: { name: Name } 9 9 scheduler_name: { name: Scheduler } 10 polling_delay: { name: Polling delay }10 polling_delay: { name: Polling delay (in seconds) } 11 11 _is_running: { name: Status } 12 12 _nb_active_ready_jobs: { name: ready, help: number of jobs ready to be executed } plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/templates/_scheduler.php
r7191 r7246 4 4 ?> 5 5 <select name="sf_jobqueue[scheduler_name]" id="sf_jobqueue_scheduler_name"> 6 <option value=""> Scheduling strategy</option>6 <option value=""><?php echo __('Scheduling strategy') ?></option> 7 7 <?php foreach ($job_schedulers as $job_scheduler): ?> 8 8 <?php preg_match('`^sf(.+)Scheduler`', $job_scheduler, $name); ?>