public function testConvertsBackAndForthMongoDatesWithoutLosingMillisecondPrecision() { $this->forAll(Generator\choose(0, 1500 * 1000 * 1000))->then(function ($milliseconds) { $moment = new Moment($milliseconds); $this->assertEquals($moment, MongoDate::toMoment(MongoDate::from($moment))); }); }
public function export() { $exported = []; if ($this->scheduledAt) { $exported['scheduled_at'] = T\MongoDate::from($this->scheduledAt); } if ($this->startedAt) { $exported['started_at'] = T\MongoDate::from($this->startedAt); } if ($this->endedAt) { $exported['ended_at'] = T\MongoDate::from($this->endedAt); } if ($this->failedWith) { $exported['class'] = get_class($this->failedWith); $exported['message'] = $this->failedWith->getMessage(); $exported['trace'] = $this->traceOf($this->failedWith); } if ($this->completedWith) { $exported['trace'] = $this->traceOf($this->completedWith); } if ($exported) { return ['last_execution' => $exported]; } else { return []; } }
public static function pickReadyJobsForWorkers(MongoCollection $collection, $worksOn, $workers) { $jobs = Onebip\array_pluck(iterator_to_array($collection->find(Worker::canWorkOnAnyJobs($worksOn) ? ['scheduled_at' => ['$lt' => T\MongoDate::now()], 'locked' => false] : ['scheduled_at' => ['$lt' => T\MongoDate::now()], 'locked' => false, 'group' => $worksOn], ['_id' => 1])->sort(['scheduled_at' => 1])->limit(count($workers))), '_id'); if (count($jobs) > 0) { return [$worksOn, $workers, $jobs]; } }
public static function tryToAssignJobsToWorkers(MongoCollection $collection, $jobs, $workers) { $assignment = array_combine(Onebip\array_map($workers, function ($id) { return (string) $id; }), $jobs); $result = $collection->update($where = ['_id' => ['$in' => array_values($workers)]], $update = ['$set' => ['available' => false, 'assigned_to' => $assignment, 'assigned_since' => T\MongoDate::now()]], ['multiple' => true]); return [$assignment, $result['n']]; }
public function recentHistory($group = null, T\Moment $at = null, array $query = []) { if ($at === null) { $at = T\now(); } $lastMinute = array_merge($query, ['last_execution.ended_at' => ['$gt' => T\MongoDate::from($at->before(T\minute(1))), '$lte' => T\MongoDate::from($at)]]); if ($group !== null) { $lastMinute['group'] = $group; } $document = $this->archived->aggregate($pipeline = [['$match' => $lastMinute], ['$project' => ['latency' => ['$subtract' => ['$last_execution.started_at', '$last_execution.scheduled_at']], 'execution_time' => ['$subtract' => ['$last_execution.ended_at', '$last_execution.started_at']]]], ['$group' => ['_id' => 1, 'throughput' => ['$sum' => 1], 'latency' => ['$avg' => '$latency'], 'execution_time' => ['$avg' => '$execution_time']]]]); if (!$document['ok']) { throw new RuntimeException("Pipeline failed: " . var_export($pipeline, true)); } if (count($document['result']) === 0) { $throughputPerMinute = 0.0; $averageLatency = 0.0; $averageExecutionTime = 0; } else { if (count($document['result']) === 1) { $throughputPerMinute = (double) $document['result'][0]['throughput']; $averageLatency = $document['result'][0]['latency'] / 1000; $averageExecutionTime = $document['result'][0]['execution_time'] / 1000; } else { throw new RuntimeException("Result was not ok: " . var_export($document, true)); } } return ['throughput' => ['value' => $throughputPerMinute, 'value_per_second' => $throughputPerMinute / 60.0], 'latency' => ['average' => $averageLatency], 'execution_time' => ['average' => $averageExecutionTime]]; }