Example #1
0
 /**
  * @param \Ingenerator\RunSingle\LockDriver    $driver
  * @param \Ingenerator\RunSingle\CommandRunner $runner
  */
 function it_runs_task_and_returns_exit_code_if_task_ran($driver, $runner)
 {
     $this->given_lock_is_available($driver, self::TASK_NAME, self::TIMEOUT, 1426828665);
     $runner->execute(self::COMMAND)->willReturn(99);
     $this->subject->execute(self::TASK_NAME, self::COMMAND, self::TIMEOUT, TRUE)->shouldBe(99);
     $runner->execute(self::COMMAND)->shouldHaveBeenCalled();
 }
    function it_runs_provided_command()
    {
        $script = __DIR__ . '/test-execution.sh';
        $file_content = <<<'EOF'
#! /bin/bash
# Call this like
# test-execution.sh path/to/tmpfile some argument "with args"
for arg in "$@"
  do
    echo $arg >> $1;
  done
EOF;
        file_put_contents($script, $file_content);
        chmod($script, 0755);
        $tmpfile = tempnam(sys_get_temp_dir(), 'command-test_');
        $cmd = $script . ' ' . escapeshellarg($tmpfile) . ' some "argument with" arguments';
        $this->subject->execute($cmd);
        $received_args = file_get_contents($tmpfile);
        $expected = <<<ARGS
{$tmpfile}
some
argument with
arguments

ARGS;
        expect($received_args)->toBe($expected);
        unlink($tmpfile);
        unlink($script);
    }
Example #3
0
 /**
  * @param string $task_name
  * @param string $command
  * @param string $timeout
  * @param string $garbage_collect
  *
  * @return integer
  */
 public function execute($task_name, $command, $timeout, $garbage_collect)
 {
     if ($garbage_collect === TRUE) {
         $this->logger->info('garbage collecting for task ' . $task_name);
         $this->driver->garbage_collect($task_name);
     }
     $this->logger->info('lock_holder id is ' . $this->lock_holder->get_lock_holder());
     $this->logger->info('trying to get lock for task ' . $task_name);
     $lock_id = $this->driver->get_lock($task_name, $timeout, $this->lock_holder->get_lock_holder());
     if ($lock_id !== FALSE) {
         $this->logger->info('executing task ' . $task_name . ' ...');
         $this->logger->info('<command output>');
         $start_time = time();
         $exit_code = $this->runner->execute($command);
         $end_time = time();
         $elapsed_time = $end_time - $start_time;
         $this->logger->info('</command output>');
         $this->logger->info('finished ' . $task_name . ' after ' . $elapsed_time . ' seconds with exit code ' . $exit_code);
         $this->driver->release_lock($task_name, $lock_id);
         return $exit_code;
     }
     $this->logger->info('no lock available for ' . $task_name);
     return 0;
 }