/[drupal]/contributions/modules/admintools/cron.module
ViewVC logotype

Diff of /contributions/modules/admintools/cron.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.6, Mon Jun 8 13:41:07 2009 UTC revision 1.7, Mon Jun 15 10:43:17 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: cron.module,v 1.5 2009/05/13 17:07:05 denraf Exp $  // $Id: cron.module,v 1.6 2009/06/08 13:41:07 denraf Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 67  function cron_list_form() { Line 67  function cron_list_form() {
67      array('data' => t('Checks'), 'field' => 'checks'),      array('data' => t('Checks'), 'field' => 'checks'),
68      t('Last check'),      t('Last check'),
69      array('data' => t('Errors'), 'field' => 'errors'),      array('data' => t('Errors'), 'field' => 'errors'),
70      t('Last error')      t('Last error'),
71        array('data' => t('Disabled'), 'field' => 'disabled')
72    );    );
73    
74    $sql = 'SELECT DISTINCT sid, url, cron_key, description, checks, errors, last_check, last_error FROM {cron}';    $sql = 'SELECT DISTINCT sid, url, cron_key, description, checks, errors, last_check, last_error, disabled FROM {cron}';
75    $sql .= tablesort_sql($header);    $sql .= tablesort_sql($header);
76    $query_count = 'SELECT COUNT(DISTINCT url) FROM {cron}';    $query_count = 'SELECT COUNT(DISTINCT url) FROM {cron}';
77    $result = pager_query($sql, 50, 0, $query_count);    $result = pager_query($sql, 50, 0, $query_count);
# Line 85  function cron_list_form() { Line 86  function cron_list_form() {
86      $form['last_check'][$site->sid] = array('#value' => format_interval(time() - $site->last_check));      $form['last_check'][$site->sid] = array('#value' => format_interval(time() - $site->last_check));
87      $form['errors'][$site->sid] =  array('#value' => $site->errors);      $form['errors'][$site->sid] =  array('#value' => $site->errors);
88      $form['last_error'][$site->sid] = array('#value' => ($site->last_error == 0) ? 'N/A' : format_interval(time() - $site->last_error));      $form['last_error'][$site->sid] = array('#value' => ($site->last_error == 0) ? 'N/A' : format_interval(time() - $site->last_error));
89        $form['disabled'][$site->sid] = array('#type' => 'checkbox', '#default_value' => $site->disabled);
90      $form['operations'][$site->sid] = array('#value' => l(t('edit'), "admin/settings/tools/cron/edit/$site->sid") .' '. l(t('delete'), "admin/settings/tools/cron/delete/$site->sid"));      $form['operations'][$site->sid] = array('#value' => l(t('edit'), "admin/settings/tools/cron/edit/$site->sid") .' '. l(t('delete'), "admin/settings/tools/cron/delete/$site->sid"));
91    }    }
92    $form['sites'] = array(    $form['sites'] = array(
# Line 114  function cron_add_form() { Line 116  function cron_add_form() {
116      '#type' => 'textfield',      '#type' => 'textfield',
117      '#size' => 20,      '#size' => 20,
118    );    );
119      $form['interval'] =  array(
120        '#type' => 'textfield',
121        '#size' => 3,
122      );
123    $form['operations']['add'] = array(    $form['operations']['add'] = array(
124      '#type' => 'submit',      '#type' => 'submit',
125      '#value' => t('Add'),      '#value' => t('Add'),
# Line 156  function cron_add_form_validate($form, & Line 162  function cron_add_form_validate($form, &
162        if(isset($form_state['values']['cron_key'])) {        if(isset($form_state['values']['cron_key'])) {
163          $url .= '?cron_key='.$form_state['values']['cron_key'];          $url .= '?cron_key='.$form_state['values']['cron_key'];
164        }        }
165    
166        $ch = curl_init($url);        $ch = curl_init($url);
167        curl_setopt($ch, CURLOPT_HEADER, TRUE);        curl_setopt($ch, CURLOPT_HEADER, TRUE);
168        curl_setopt($ch, CURLOPT_NOBODY, TRUE);        curl_setopt($ch, CURLOPT_NOBODY, TRUE);
# Line 165  function cron_add_form_validate($form, & Line 171  function cron_add_form_validate($form, &
171        curl_exec($ch);        curl_exec($ch);
172        $result = curl_getinfo($ch);        $result = curl_getinfo($ch);
173        if ($result['http_code'] != '200') {        if ($result['http_code'] != '200') {
174          form_set_error('site', t('Your cron is not reacheable at the given address: @url', array('@url' => $url)));          form_set_error('url', t('Your cron is not reacheable at the given address: @url', array('@url' => $url)));
175          }
176    
177          if ( (!empty($form_state['values']['interval'])) && (!is_numeric($form_state['values']['interval']))) {
178            form_set_error('interval', t('The interval setting has to be a number.'));
179        }        }
180    }    }
181    
# Line 173  function cron_add_form_validate($form, & Line 183  function cron_add_form_validate($form, &
183  }  }
184    
185  function cron_add_form_submit($form, $form_state) {  function cron_add_form_submit($form, $form_state) {
186    db_query("INSERT INTO {cron} (url, cron_key, description, checks, last_check) VALUES ('%s', '%s', '%s', 1, '%d')", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], time());    db_query("INSERT INTO {cron} (url, cron_key, description, checks, last_check, interval, count) VALUES ('%s', '%s', '%s', 1, '%d', '%d', '%d')", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], time(), $form_state['values']['interval'], $form_state['values']['interval']);
187    drupal_goto('admin/settings/tools/cron');    drupal_goto('admin/settings/tools/cron');
188  }  }
189    
190  function cron_edit_form($form_state, $sid = 0) {  function cron_edit_form($form_state, $sid = 0) {
191    $sql = db_query("SELECT url, cron_key, description, checks, errors FROM {cron} WHERE sid = '%d'", $sid);    $sql = db_query("SELECT url, cron_key, description, checks, errors, interval FROM {cron} WHERE sid = '%d'", $sid);
192    $row = db_fetch_object($sql);    $row = db_fetch_object($sql);
193    $form['sid'] = array(    $form['sid'] = array(
194      '#type' => 'hidden',      '#type' => 'hidden',
# Line 203  function cron_edit_form($form_state, $si Line 213  function cron_edit_form($form_state, $si
213      '#size' => 20,      '#size' => 20,
214      '#default_value' => $row->description,      '#default_value' => $row->description,
215    );    );
216      $form['interval'] =  array(
217        '#type' => 'textfield',
218        '#size' => 3,
219        '#default_value' => $row->interval,
220      );
221    $form['operations']['save'] = array(    $form['operations']['save'] = array(
222      '#type' => 'submit',      '#type' => 'submit',
223      '#value' => t('Save'),      '#value' => t('Save'),
# Line 250  function cron_edit_form_validate($form, Line 265  function cron_edit_form_validate($form,
265        curl_exec($ch);        curl_exec($ch);
266        $result = curl_getinfo($ch);        $result = curl_getinfo($ch);
267        if ($result['http_code'] != '200') {        if ($result['http_code'] != '200') {
268          form_set_error('site', t('Your cron is not reacheable at the given address: @url', array('@url' => $url)));          form_set_error('url', t('Your cron is not reacheable at the given address: @url', array('@url' => $url)));
269          }
270    
271          if ( (!empty($form_state['values']['interval'])) && (!is_numeric($form_state['values']['interval']))) {
272            form_set_error('interval', t('The interval setting has to be a number.'));
273        }        }
274    }    }
275    
# Line 258  function cron_edit_form_validate($form, Line 277  function cron_edit_form_validate($form,
277  }  }
278    
279  function cron_edit_form_submit($form, $form_state) {  function cron_edit_form_submit($form, $form_state) {
280    db_query("UPDATE {cron} SET url = '%s', cron_key = '%s', description = '%s', checks = '%d', last_check = '%d' WHERE sid = '%d'", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], $form_state['values']['checks'] + 1, time(), $form_state['values']['sid']);    db_query("UPDATE {cron} SET url = '%s', cron_key = '%s', description = '%s', checks = '%d', last_check = '%d', disabled = 'FALSE', interval = '%d', count = '%d' WHERE sid = '%d'", $form_state['values']['url'], $form_state['values']['cron_key'], $form_state['values']['description'], $form_state['values']['checks'] + 1, time(), $form_state['values']['sid'], $form_state['values']['interval'], $form_state['values']['interval']);
281    drupal_goto('admin/settings/tools/cron');    drupal_goto('admin/settings/tools/cron');
282  }  }
283    
# Line 300  function theme_cron_list_form($form) { Line 319  function theme_cron_list_form($form) {
319      t('Last check'),      t('Last check'),
320      array('data' => t('Errors'), 'field' => 'errors'),      array('data' => t('Errors'), 'field' => 'errors'),
321      t('Last error'),      t('Last error'),
322        array('data' => t('Disabled'), 'field' => 'disabled'),
323      t('Operations')      t('Operations')
324    );    );
325    if (isset($form['url']) && is_array($form['url'])) {    if (isset($form['url']) && is_array($form['url'])) {
# Line 313  function theme_cron_list_form($form) { Line 333  function theme_cron_list_form($form) {
333          drupal_render($form['last_check'][$key]),          drupal_render($form['last_check'][$key]),
334          drupal_render($form['errors'][$key]),          drupal_render($form['errors'][$key]),
335          drupal_render($form['last_error'][$key]),          drupal_render($form['last_error'][$key]),
336            drupal_render($form['disabled'][$key]),
337          drupal_render($form['operations'][$key]),          drupal_render($form['operations'][$key]),
338        );        );
339      }      }
340    }    }
341    else {    else {
342      $rows[] = array(array('data' => t('No sites available.'), 'colspan' => '7'));      $rows[] = array(array('data' => t('No sites available.'), 'colspan' => '10'));
343    }    }
344    
345    $output .= theme('table', $header, $rows);    $output .= theme('table', $header, $rows);
# Line 373  function theme_cron_edit_form($form) { Line 394  function theme_cron_edit_form($form) {
394      t('Site'),      t('Site'),
395      t('Cron key (Drupal 7)'),      t('Cron key (Drupal 7)'),
396      t('Description'),      t('Description'),
397        t('Interval'),
398      t('Operations')      t('Operations')
399    );    );
400    if (isset($form['url']) && is_array($form['url'])) {    if (isset($form['url']) && is_array($form['url'])) {
# Line 380  function theme_cron_edit_form($form) { Line 402  function theme_cron_edit_form($form) {
402        drupal_render($form['url']),        drupal_render($form['url']),
403        drupal_render($form['cron_key']),        drupal_render($form['cron_key']),
404        drupal_render($form['description']),        drupal_render($form['description']),
405          drupal_render($form['interval']),
406        drupal_render($form['operations']),        drupal_render($form['operations']),
407      );      );
408    }    }
# Line 397  function theme_cron_edit_form($form) { Line 420  function theme_cron_edit_form($form) {
420   */   */
421  function cron_cron() {  function cron_cron() {
422    
423    $sql = db_query("SELECT sid, url, cron_key, checks, errors, last_error FROM {cron}");    $sql = db_query("SELECT sid, url, cron_key, checks, errors, last_check, last_error, interval, count FROM {cron} WHERE disabled = '%d'", FALSE);
424    while ($row = db_fetch_object($sql)) {    while ($row = db_fetch_object($sql)) {
425        if ($row->count == 0) {
     $checks = $row->checks + 1;  
     $errors = $row->errors ;  
     $last_error = $row->last_error;  
   
     if (function_exists('curl_init')) {  
   
       $url = $row->url .'/cron.php';  
       if (isset($row->cron_key)) {  
         $url .= '?cron_key=' . $row->cron_key;  
       }  
   
       $options = array(  
         CURLOPT_RETURNTRANSFER => TRUE,         // return web page  
         CURLOPT_HEADER         => TRUE,        // don't return headers  
         CURLOPT_NOBODY         => TRUE,  
         CURLOPT_FOLLOWLOCATION => FALSE,         // don't follow redirects  
         CURLOPT_USERAGENT      => 'spider',     // who am i  
         CURLOPT_AUTOREFERER    => TRUE,         // set referer on redirect  
         CURLOPT_CUSTOMREQUEST  => 'HEAD',  
         CURLOPT_CONNECTTIMEOUT => 60,          // timeout on connect  
         CURLOPT_TIMEOUT        => 60,          // timeout on response  
         CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects  
       );  
   
       $ch = curl_init($url);  
       curl_setopt_array($ch,$options);  
       curl_exec($ch);  
   
       $result = curl_getinfo($ch);  
426        $checks = $row->checks + 1;        $checks = $row->checks + 1;
427        $errors = $row->errors ;        $errors = $row->errors ;
428        $last_error = $row->last_error;        $last_error = $row->last_error;
429        if ($result['http_code'] != '200') {        $candidate = FALSE;
430          $errors += 1;        if ($row->last_check == $row->last_error) {
431          $last_error = time();          $candidate = TRUE;
432          }
433    
434          if (function_exists('curl_init')) {
435    
436            $url = $row->url .'/cron.php';
437            if (isset($row->cron_key)) {
438              $url .= '?cron_key=' . $row->cron_key;
439            }
440    
441            $options = array(
442              CURLOPT_RETURNTRANSFER => TRUE,         // return web page
443              CURLOPT_HEADER         => TRUE,        // don't return headers
444              CURLOPT_NOBODY         => TRUE,
445              CURLOPT_FOLLOWLOCATION => FALSE,         // don't follow redirects
446              CURLOPT_USERAGENT      => 'spider',     // who am i
447              CURLOPT_AUTOREFERER    => TRUE,         // set referer on redirect
448              CURLOPT_CUSTOMREQUEST  => 'HEAD',
449              CURLOPT_CONNECTTIMEOUT => 60,          // timeout on connect
450              CURLOPT_TIMEOUT        => 60,          // timeout on response
451              CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
452            );
453    
454            $ch = curl_init($url);
455            curl_setopt_array($ch,$options);
456            curl_exec($ch);
457    
458            $result = curl_getinfo($ch);
459            $checks = $row->checks + 1;
460            $errors = $row->errors ;
461            $last_error = $row->last_error;
462            if ($result['http_code'] != '200') {
463              $errors += 1;
464              $last_error = time();
465            }
466            curl_close($ch);
467          }
468          else {
469    
470            if (strstr($url, 'https://')) {
471              $port = 443;
472              $url = substr($row->url, 8);
473            }
474            else {
475              $port = 80;
476              $url = substr($row->url, 7);
477            }
478            if (strpos($url, '/')) {
479               $path = substr($url, strpos($url, '/'));
480              $url = substr($url, 0, strpos($url, '/'));
481            }
482            $fp = fsockopen($url, $port, $errno, $errstr, 30);
483            $page = $path ."/cron.php";
484            $out="GET $page HTTP/1.1\r\n";
485            $out.="Host: $url\r\n";
486            $out.="Connection: Close\r\n\r\n";
487            fwrite($fp,$out);
488            $content=fgets($fp);
489            $result = explode(' ', $content);
490            if ($result[1] != '200') {
491              $errors += 1;
492              $last_error = time();
493            }
494            fclose($fp);
495          }
496    
497          $disable = FALSE;
498          if (($last_error > $row->last_error) && ($candidate == TRUE) && ( $errors > (0.9 * $checks) )) {
499            // when 2 last checks failed and 90% of all checks failed, disable this site
500            $disable = TRUE;
501        }        }
502        curl_close($ch);        db_query("UPDATE {cron} SET checks = '%d', last_check = '%d', errors = '%d', last_error = '%d', disabled = '%d', interval = '%d', count = '%d' WHERE sid = '%d'", $checks, time(), $errors, $last_error, $disable, $row->interval, $row->interval, $row->sid);
503      }      }
504      else {      else {
505          $row->count--;
506        if (strstr($url, 'https://')) {        db_query("UPDATE {cron} SET count = '%d' WHERE sid = '%d'", $row->count, $row->sid);
         $port = 443;  
         $url = substr($row->url, 8);  
       }  
       else {  
         $port = 80;  
         $url = substr($row->url, 7);  
       }  
       if (strpos($url, '/')) {  
         $path = substr($url, strpos($url, '/'));  
         $url = substr($url, 0, strpos($url, '/'));  
       }  
       $fp = fsockopen($url, $port, $errno, $errstr, 30);  
       $page = $path ."/cron.php";  
       $out="GET $page HTTP/1.1\r\n";  
       $out.="Host: $url\r\n";  
       $out.="Connection: Close\r\n\r\n";  
       fwrite($fp,$out);  
       $content=fgets($fp);  
       $result = explode(' ', $content);  
       if ($result[1] != '200') {  
         $errors += 1;  
         $last_error = time();  
       }  
       fclose($fp);  
507      }      }
   
     db_query("UPDATE {cron} SET checks = '%d', last_check = '%d', errors = '%d', last_error = '%d' WHERE sid = '%d'", $checks, time(), $errors, $last_error, $row->sid);  
   
508    }    }
509  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.2