public function getRunnableUnit(Project $project = null)
{
$whereProject = '';
if ($project) {
$whereProject = 'AND R.project_name = :project_name';
}
$this->connection->beginTransaction();
$stmt = $this->connection->prepare('
SELECT
U.id AS id,
U.feature AS feature,
U.created_at AS created_at,
U.finished_at AS finished_at,
U.return_code AS return_code,
U.output_files AS output_files,
R.id AS run_id,
R.title AS run_title,
R.project_name AS run_project_name,
R.properties AS run_properties,
R.created_at AS run_created_at
FROM
bl_run R
INNER JOIN bl_run_unit U ON R.id = U.run_id
WHERE U.started_at IS NULL ' . $whereProject . '
ORDER BY U.created_at ASC
LIMIT 1
');
if ($project) {
$stmt->bindValue('project_name', $project->getName());
}
$stmt->execute();
if (!($row = $stmt->fetch())) {
$this->connection->commit();
return;
// nothing to process
}
$stmt = $this->connection->prepare('UPDATE bl_run_unit SET started_at = NOW() WHERE id = :id');
$stmt->bindValue('id', $row['id']);
$stmt->execute();
$this->connection->commit();
$run = new Run();
$run->setId($row['run_id'])->setTitle($row['run_title'])->setProjectName($row['run_project_name'])->setProperties(json_decode($row['run_properties'], true))->setProjectName($row['run_project_name'])->setCreatedAt(new \DateTime($row['created_at']));
$unit = new RunUnit();
$unit->setRun($run)->setId($row['id'])->setFeature($row['feature'])->setCreatedAt(new \DateTime($row['created_at']))->setStartedAt(new \DateTime())->setFinishedAt($row['finished_at'] !== null ? new \DateTime($row['finished_at']) : null)->setReturnCode($row['return_code']);
$this->loadOutputFiles($unit->getOutputFiles(), json_decode($row['output_files'], true));
return $unit;
}