private function finalize(TaskExecutionInterface $execution, $start)
    {
        // this find is necessary because the storage could be
        // invalid (clear in doctrine) after handling an execution.
        $execution = $this->taskExecutionRepository->findByUuid($execution->getUuid());

        if ($execution->getStatus() !== TaskStatus::PLANNED) {
            $execution->setEndTime(new \DateTime());
            $execution->setDuration(microtime(true) - $start);
        }

        $this->eventDispatcher->dispatch(
            Events::TASK_FINISHED,
            new TaskExecutionEvent($execution->getTask(), $execution)
        );

        $this->taskExecutionRepository->save($execution);
    }