public function duration() { if ($this->startedAt && $this->endedAt && $this->startedAt <= $this->endedAt) { return T\seconds($this->endedAt->seconds() - $this->startedAt->seconds()); } return T\seconds(0); }
public function testShouldRescheduleInOneMinuteWhenWasCreatedLessThanFiveMinutesAgo() { $expectedToBeScheduledAt = T\minute(1)->fromNow()->toSecondPrecision(); $wasCreatedAt = T\seconds(10)->ago(); $job = $this->givenJobThat($wasCreatedAt); $job->expects($this->once())->method('scheduleAt')->with($this->equalTo($expectedToBeScheduledAt)); $this->scheduler->schedule($job); }
public function testBackingOffCannotIncreaseTheIntervalOverAMaximum() { $ws = new WaitStrategy(T\seconds(1), T\seconds(2), $this->howToWait); $ws->backOff(); $ws->backOff(); $ws->backOff(); $ws->backOff(); $ws->wait(); $this->assertEquals(T\seconds(2), $this->waited); }
public static function import($parameters) { return new self($parameters['retry_how_many_times'], T\seconds($parameters['seconds_to_initially_wait_before_retry'])); }
private function aJob() { $workable = $this->getMockBuilder('Recruiter\\Workable')->getMock(); return Job::around($workable, $this->repository)->scheduleAt(T\now()->before(T\seconds(5))); }
public function testCanBeCreatedByTargetingAMaximumInterval() { $this->assertEquals(ExponentialBackoff::forAnInterval(1025, T\seconds(1)), new ExponentialBackoff(10, 1)); }
#!/usr/bin/env php <?php require __DIR__ . '/../vendor/autoload.php'; use Timeless as T; use Recruiter\Recruiter; use Recruiter\Factory; use Recruiter\Workable\AlwaysFail; use Recruiter\RetryPolicy; use Recruiter\Worker; use Recruiter\Option\MemoryLimit; $factory = new Factory(); $db = $factory->getMongoDb($hosts = 'localhost:27017', $options = [], $dbName = 'recruiter'); $db->drop(); $recruiter = new Recruiter($db); (new AlwaysFail())->asJobOf($recruiter)->retryManyTimes(5, T\seconds(1), 'DomainException')->inBackground()->execute(); $memoryLimit = new MemoryLimit('memory-limit', '64MB'); $worker = $recruiter->hire($memoryLimit); while (true) { printf("Try to do my work\n"); $assignments = $recruiter->assignJobsToWorkers(); if ($assignments === 0) { break; } $worker->work(); usleep(1200 * 1000); }