Development

Changeset 7246

You must first sign up to be able to contribute.

Changeset 7246

Show
Ignore:
Timestamp:
01/31/08 13:26:52 (10 months ago)
Author:
xavier
Message:

sfJobQueuePlugin:

  • removed the useless sfJobhandlerInterface
  • improved jobs status visualisation from the graphical interface
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfJobHandler.class.php

    r7231 r7246  
    33 * This file is part of the sfJobQueuePlugin package. 
    44 */ 
    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 */ 
     13abstract class sfJobHandler 
    614{ 
    715  protected $logger; 
     
    1725  } 
    1826 
    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() 
    2039  { 
    21     return sfJobQueue::SUCCESS
     40    return array()
    2241  } 
     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); 
    2356 
    2457  /** 
    2558   * Called after a job creation 
    2659   * 
    27    * @param    object     the related job 
     60   * @param    sfJob      the related job 
    2861   * @param    array      array of params of the job 
    2962   */ 
    30   public static function postSave($job, $params) 
     63  public static function postSave(sfJob $job, array $params) 
    3164  { 
    3265  } 
  • plugins/sfJobQueuePlugin/trunk/lib/jobHandlers/sfMailJobHandler.class.php

    r5658 r7246  
    11<?php 
    22 
    3 class sfMailJobHandler extends sfJobHandler implements sfJobHandlerInterface 
     3class sfMailJobHandler extends sfJobHandler 
    44{ 
    55  public function getParamFields() 
     
    88  } 
    99 
    10   public function run($params) 
     10  public function run(array $params) 
    1111  { 
    12     if (false && mail($params['to'],  
    13              $params['subject'],  
    14              $params['message'],  
     12    if (mail($params['to'], 
     13             $params['subject'], 
     14             $params['message'], 
    1515             'From: '.$params['from'])) 
    1616    { 
     
    2020    { 
    2121      throw new Exception('There was an error while sending the mail.'); 
    22       return sfJob::ERROR; 
    2322    } 
    2423  } 
  • plugins/sfJobQueuePlugin/trunk/lib/model/sfJob.php

    r5658 r7246  
    22/* 
    33 * This file is part of the sfJobQueuePlugin package. 
    4  *  
     4 * 
    55 * (c) 2007 Xavier Lacot <xavier@lacot.org> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
     
    1010 
    1111/** 
    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 
    1717 * emails, etc.) or in environments without a cron access. 
    18  *  
     18 * 
    1919 * @author   Xavier Lacot <xavier@lacot.org> 
    2020 * @see      http://www.symfony-project.com/trac/wiki/sfJobQueuePlugin 
     
    2929  const SUCCESS   = 9; 
    3030 
    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', 
    3636                                     sfJob::SUCCESS   => 'success'); 
    3737 
     
    9090  /** 
    9191   * Returns the status of the job, as a text 
    92    *  
     92   * 
    9393   * @return    string 
    9494   */ 
     
    105105  { 
    106106    set_error_handler(array($this, 'handleRuntimeError')); 
     107    $status_text = ''; 
    107108 
    108109    // increment number of tries 
     
    119120    { 
    120121      $status = $jobhandler->run($params); 
    121        
    122       if ($status === ''
     122 
     123      if ($status == '' || $status == null
    123124      { 
    124125        $status = self::SUCCESS; 
    125126      } 
     127 
     128      $this->setMessage(''); 
    126129    } 
    127130    catch (Exception $e) 
     
    130133      { 
    131134        $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); 
    138140    } 
    139141 
    140142    if (!$this->getIsRecuring()) 
    141143    { 
    142       if (($this->getTries() >= $this->getMaxTries())  
    143           && ($status != self::SUCCESS)  
     144      if (($this->getTries() >= $this->getMaxTries()) 
     145          && ($status != self::SUCCESS) 
    144146          && ($status != self::ERROR)) 
    145147      { 
     
    158160    $this->save(); 
    159161    restore_error_handler(); 
     162    return $status_text; 
    160163  } 
    161164 
     
    205208 
    206209        // Max number of tries reached, change status to "error" 
    207         if (!$this->getIsRecuring()  
     210        if (!$this->getIsRecuring() 
    208211            && ($this->getTries() >= $this->getMaxTries())) 
    209212        { 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJob/actions/actions.class.php

    r7231 r7246  
    136136    $message = $sf_job->run(); 
    137137 
    138     if ($message == ''
     138    if ($message == '' || $message == null
    139139    { 
    140140      if ($sf_job->getMessage() == '') 
    141141      { 
    142         $message = 'Job completed'; 
     142        $message = sfConfig::get('sf_i18n') ? $this->getContext()->getI18n()->__('Job completed') : 'Job completed'; 
    143143      } 
    144144      else 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJob/config/generator.yml

    r7231 r7246  
    66 
    77    fields: 
     8      name:                { name: Name } 
    89      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 } 
    1315 
    1416    list: 
     
    2830      title:               Job edition 
    2931      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] 
    3133        "Job parameters":   [_params] 
    3234        "Job status":       [status, tries, created_at, last_tried_at, completed_at] 
    3335      fields: 
     36        name:                { name: Name of the job (informational only) } 
    3437        status:             { type: plain } 
    3538        tries:              { type: plain } 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJob/templates/_status.php

    r5658 r7246  
    11<?php echo $sf_job->getStatusText(); ?> 
    2 <?php if (($sf_job->getStatus() == sfJob::ERROR)  
    3           && ($sf_job->getMessage() != '')): ?> 
     2<?php if ($sf_job->getMessage() != ''): ?> 
    43  <?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(), 
    87                       'title' => $sf_job->getMessage())); 
    98  ?> 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/actions/actions.class.php

    r5658 r7246  
    99class sfJobQueueActions extends autosfJobQueueActions 
    1010{ 
    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       else 
    34       { 
    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     else 
    39     { 
    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  
    4911  public function executeSwitchStatus() 
    5012  { 
     
    7032    $this->forward404Unless($this->job_queue); 
    7133  } 
    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     else 
    83     { 
    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         try 
    131         { 
    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           else 
    139           { 
    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 date 
    149         } 
    150       } 
    151       else 
    152       { 
    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   } 
    16734} 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/config/generator.yml

    r7191 r7246  
    88      name:                          { name: Name } 
    99      scheduler_name:                { name: Scheduler } 
    10       polling_delay:                 { name: Polling delay
     10      polling_delay:                 { name: Polling delay (in seconds)
    1111      _is_running:                   { name: Status } 
    1212      _nb_active_ready_jobs:         { name: ready, help: number of jobs ready to be executed } 
  • plugins/sfJobQueuePlugin/trunk/modules/sfJobQueue/templates/_scheduler.php

    r7191 r7246  
    44?> 
    55<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> 
    77  <?php foreach ($job_schedulers as $job_scheduler): ?> 
    88    <?php preg_match('`^sf(.+)Scheduler`', $job_scheduler, $name); ?>