Example #1
0
 public function lock($status = BACKGROUND_PROCESS_STATUS_LOCKED)
 {
     // Preliminary select to avoid unnecessary write-attempt
     if (background_process_get_process($this->handle)) {
         // watchdog('bg_process', 'Will not attempt to lock handle %handle, already exists', array('%handle' => $this->handle), WATCHDOG_NOTICE);
         return FALSE;
     }
     // "Lock" handle
     $this->start_stamp = $this->start = microtime(TRUE);
     if (!background_process_lock_process($this->handle, $status)) {
         // If this happens, we might have a race condition or an md5 clash
         watchdog('bg_process', 'Could not lock handle %handle', array('%handle' => $this->handle), WATCHDOG_ERROR);
         return FALSE;
     }
     $this->exec_status = $this->status = BACKGROUND_PROCESS_STATUS_LOCKED;
     $this->sendMessage('locked');
     return TRUE;
 }
 /**
  * Background Process legacy callback for running cron jobs.
  *
  * @param string $name
  *   The name of the job.
  * @param string $lock_id
  *   The lock id.
  */
 public static function job_callback($name, $lock_id, $recheck = FALSE)
 {
     $job = _ultimate_cron_job_load($name);
     $log_entry = $job->resumeLog($lock_id);
     // If set, $recheck contains the timestamp of the last schedule check.
     if ($recheck) {
         // Simulate schedule check by setting a mock log entry object with the
         // recheck timestamp.
         $job->log_entry = $job->getPlugin('logger')->factoryLogEntry($job->name);
         $job->log_entry->start_time = $recheck;
         // Now we can check the scheduler.
         if (!$job->getPlugin('scheduler')->isScheduled($job)) {
             watchdog('bgpl_launcher', 'Recheck failed at @time', array('@time' => format_date(time(), 'custom', 'Y-m-d H:i:s')), WATCHDOG_ERROR);
             $job->sendSignal('background_process_legacy_dont_log');
             $log_entry->finish();
             $job->unlock($lock_id);
             return;
         }
         unset($job->log_entry);
     }
     // Run job.
     try {
         if ($job->getPlugin('launcher')->name != 'background_process_legacy') {
             // Launcher has changed, end job/daemon.
             $log_entry->finish();
             $job->unlock($lock_id);
             return;
         }
         $settings = $job->getSettings('launcher');
         if ($settings['daemonize']) {
             $keepalive = TRUE;
             $expire = microtime(TRUE) + (double) $settings['daemonize_interval'];
             do {
                 $job->run();
                 if ($settings['daemonize_delay']) {
                     usleep((double) $settings['daemonize_delay'] * 1000000);
                 }
                 if ($job->getSignal('end_daemonize')) {
                     watchdog('bgpl_launcher', 'end daemonize signal received', array(), WATCHDOG_NOTICE);
                     $keepalive = FALSE;
                     break;
                 }
             } while (microtime(TRUE) < $expire);
             // Refresh disabled value.
             $job = _ultimate_cron_job_load($name, TRUE);
             $settings = $job->getSettings('launcher');
             $keepalive &= empty($job->disabled);
             $keepalive &= !empty($settings['daemonize']);
             $keepalive &= !$job->getSignal('end_daemonize');
             if ($keepalive) {
                 // Make sure recheck isn't kept alive, as this does not make
                 // any sense.
                 background_process_keepalive($name, $lock_id);
                 // Save a copy of the log.
                 $log_entry->lid = $lock_id . '-' . uniqid('', TRUE);
                 $job->sendSignal('background_process_legacy_dont_log');
                 $log_entry->finish();
                 // Restart log for keepalive.
                 $log_entry->lid = $lock_id;
                 $handle = background_process_current_handle();
                 $process = background_process_get_process($handle);
                 $log_entry->init_message = t('Re-launched at service host @name', array('@name' => $process->service_host));
                 $log_entry->message = '';
                 $log_entry->end_time = 0;
                 $log_entry->start_time = microtime(TRUE);
                 $log_entry->save();
             } else {
                 $job->sendSignal('background_process_legacy_dont_log');
                 $log_entry->finish();
                 $job->unlock($lock_id);
             }
         } else {
             $job->run();
             $job->sendSignal('background_process_legacy_dont_log');
             $log_entry->finish();
             $job->unlock($lock_id);
         }
     } catch (Throwable $e) {
         watchdog('bgpl_launcher', 'Error executing %job: @error', array('%job' => $job->name, '@error' => (string) $e), WATCHDOG_ERROR);
         $job->sendSignal('background_process_legacy_dont_log');
         $log_entry->finish();
         $job->unlock($lock_id);
     } catch (Exception $e) {
         watchdog('bgpl_launcher', 'Error executing %job: @error', array('%job' => $job->name, '@error' => (string) $e), WATCHDOG_ERROR);
         $job->sendSignal('background_process_legacy_dont_log');
         $log_entry->finish();
         $job->unlock($lock_id);
     }
 }