/**
  * Save log entry.
  */
 public function save()
 {
     if (!$this->lid) {
         return;
     }
     if ($this->log_type != ULTIMATE_CRON_LOG_TYPE_NORMAL) {
         return;
     }
     $job = _ultimate_cron_job_load($this->name);
     $settings = $job->getSettings('logger');
     $expire = $settings['timeout'] > 0 ? time() + $settings['timeout'] : $settings['timeout'];
     cache_set('uc-name:' . $this->name, $this->lid, $settings['bin'], $expire);
     cache_set('uc-lid:' . $this->lid, $this->getData(), $settings['bin'], $expire);
 }
 /**
  * Throttle queues.
  *
  * Enables or disables queue threads depending on remaining items in queue.
  */
 public function throttle($job)
 {
     if (!empty($job->hook['settings']['queue']['master'])) {
         // We always base the threads on the master.
         $master_job = _ultimate_cron_job_load($job->hook['settings']['queue']['master']);
         $settings = $master_job->getSettings('settings');
     } else {
         return;
     }
     if ($settings['queue']['throttle']) {
         $queue = DrupalQueue::get($settings['queue']['name']);
         $items = $queue->numberOfItems();
         $thread = $job->hook['settings']['queue']['thread'];
         $name = $master_job->name . '_' . $thread;
         $status = empty($master_job->disabled) && $items >= ($thread - 1) * $settings['queue']['threshold'];
         $new_status = !$status ? TRUE : FALSE;
         $old_status = ultimate_cron_job_get_status($name) ? TRUE : FALSE;
         if ($old_status !== $new_status) {
             $log_entry = $job->startLog(uniqid($job->name, TRUE), 'throttling', ULTIMATE_CRON_LOG_TYPE_ADMIN);
             $log_entry->log($job->name, 'Job @status by queue throttling (items:@items, boundary:@boundary, threshold:@threshold)', array('@status' => $new_status ? t('disabled') : t('enabled'), '@items' => $items, '@boundary' => ($thread - 1) * $settings['queue']['threshold'], '@threshold' => $settings['queue']['threshold']), WATCHDOG_DEBUG);
             $log_entry->finish();
             $job->dont_log = TRUE;
             $job->setStatus($new_status);
         }
     }
 }
 /**
  * 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);
     }
 }