/**
  * Poorman launcher background process callback.
  *
  * @param string $lock_id
  *   The lock id used for this process.
  */
 public static function poormanLauncher($lock_id)
 {
     $class = _ultimate_cron_get_class('lock');
     // Bail out if someone stole our lock.
     if (!$class::reLock($lock_id, 60)) {
         return;
     }
     ultimate_cron_poorman_page_flush();
     // Wait until it's our turn (0 seconds at next minute).
     $cron_last = variable_get('cron_last', 0);
     $cron_next = floor(($cron_last + 60) / 60) * 60;
     $time = time();
     if ($time < $cron_next) {
         $sleep = $cron_next - $time;
         sleep($sleep);
     }
     // Get settings, so we can determine the poormans cron service group.
     $plugin = _ultimate_cron_plugin_load('launcher', 'background_process_legacy');
     if (!$plugin) {
         $class::unlock($lock_id);
         throw new Exception(t('Failed to load launcher plugin?!?!?!?!'));
     }
     $settings = $plugin->getDefaultSettings();
     // In case launchers fail, we don't want to relaunch this process
     // immediately.
     _ultimate_cron_variable_save('cron_last', time());
     // It's our turn!
     $launchers = array();
     foreach (_ultimate_cron_job_load_all() as $job) {
         $launcher = $job->getPlugin('launcher');
         $launchers[$launcher->name] = $launcher->name;
     }
     foreach ($launchers as $name) {
         $process = new BackgroundProcess('_ultimate_cron_poorman_' . $name);
         $process->uid = 0;
         $process->service_group = $settings['poorman_service_group'];
         $process->start('ultimate_cron_run_launchers', array(array($name)));
     }
     // Bail out if someone stole our lock.
     if (!$class::reLock($lock_id, 60)) {
         return;
     }
     // Wait until it's our turn (0 seconds at next minute).
     $cron_last = _ultimate_cron_variable_load('cron_last', 0);
     $cron_next = floor(($cron_last + 60) / 60) * 60;
     $time = time();
     if ($time < $cron_next) {
         $sleep = $cron_next - $time;
         sleep($sleep);
     }
     // Check poorman settings. If launcher has changed, we don't want
     // to keepalive.
     $poorman = _ultimate_cron_plugin_load('settings', 'poorman');
     if (!$poorman) {
         return;
     }
     $settings = $poorman->getDefaultSettings();
     if (!$settings['launcher'] || $settings['launcher'] !== 'background_process_legacy') {
         return;
     }
     background_process_keepalive();
 }
Exemplo n.º 2
0
 function testProcesses()
 {
     SweteDb::q('delete from background_processes');
     require_once 'inc/BackgroundProcess.php';
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     $process->start();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertTrue(isset($prec));
     if ($prec) {
         $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
         $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
         $this->assertEquals(0, intval($prec->val('running')));
         $this->assertEquals(1, intval($prec->val('complete')));
         $this->assertEquals(0, intval($prec->val('error')));
     }
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     BackgroundProcess::runProcess($process->getProcessId());
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertTrue(isset($prec));
     if ($prec) {
         $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
         $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
         $this->assertEquals(0, intval($prec->val('running')));
         $this->assertEquals(1, intval($prec->val('complete')));
         $this->assertEquals(0, intval($prec->val('error')));
     }
     $process = new BackgroundProcess();
     $this->assertEquals(null, $process->getProcessId());
     $this->assertEquals(false, $process->isClean());
     $process->save();
     $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
     $this->assertTrue($prec instanceof Dataface_Record);
     $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
     $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
     $this->assertEquals(0, intval($prec->val('running')));
     $this->assertEquals(0, intval($prec->val('complete')));
     $this->assertEquals(0, intval($prec->val('error')));
     $res = BackgroundProcess::runProcesses();
     $this->assertTrue($res);
     if ($res) {
         $prec = df_get_record('background_processes', array('process_id' => '=' . $process->getProcessId()));
         $this->assertTrue($prec instanceof Dataface_Record);
         $this->assertTrue(isset($prec));
         if ($prec) {
             $this->assertEquals($process->getProcessId(), $prec->val('process_id'));
             $this->assertEquals('BackgroundProcess', $prec->val('process_class'));
             $this->assertEquals(0, intval($prec->val('running')));
             $this->assertEquals(1, intval($prec->val('complete')));
             $this->assertEquals(0, intval($prec->val('error')));
         }
     }
 }