-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskRun.php
More file actions
177 lines (153 loc) · 3.27 KB
/
TaskRun.php
File metadata and controls
177 lines (153 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php declare(strict_types=1);
namespace Torr\TaskManager\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Torr\TaskManager\Exception\Log\InvalidLogActionException;
use function Symfony\Component\Clock\now;
/**
* @final
*/
#[ORM\Entity]
#[ORM\Table(name: "task_manager_runs")]
class TaskRun
{
// region Fields
/**
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "AUTO")]
#[ORM\Column(name: "id", type: Types::INTEGER)]
private ?int $id = null;
/**
*/
#[ORM\ManyToOne(targetEntity: TaskLog::class, inversedBy: "runs")]
#[ORM\JoinColumn(name: "task_log_id", referencedColumnName: "id", nullable: false)]
private TaskLog $taskLog;
/**
*
*/
#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private \DateTimeImmutable $timeStarted;
/**
*
*/
#[ORM\Column(type: Types::FLOAT, nullable: true)]
private ?float $duration = null;
/**
*/
#[ORM\Column(type: Types::BOOLEAN, nullable: true)]
private ?bool $success = null;
/**
*/
#[ORM\Column(type: Types::BOOLEAN, nullable: true)]
private ?bool $finishedProperly = null;
/**
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $output = null;
/**
*
*/
private ?float $start = null;
// endregion
/**
*/
public function __construct (TaskLog $taskLog)
{
$this->taskLog = $taskLog;
$this->timeStarted = now();
$this->start = hrtime(true);
}
// region Accessors
/**
*/
public function getTaskLog () : TaskLog
{
return $this->taskLog;
}
/**
*/
public function getTimeStarted () : \DateTimeImmutable
{
return $this->timeStarted;
}
/**
* Whether the task was finished successfully.
*
* @return bool|null Whether the task was finished successfully. Will return null if not yet finished.
*/
public function isSuccess () : ?bool
{
return $this->success;
}
/**
*/
public function getOutput () : ?string
{
return $this->output;
}
/**
*/
public function isFinished () : bool
{
return null !== $this->duration;
}
/**
* The duration of the run in nanoseconds (if the task is finished already)
*/
public function getDuration () : ?float
{
return $this->duration;
}
/**
* Whether the task was finished properly or was automatically finished.
*/
public function hasFinishedProperly () : bool
{
return true === $this->finishedProperly;
}
// endregion
/**
*/
public function finish (bool $success, ?string $output) : void
{
$this->finalizeRun(
success: $success,
finishedProperly: true,
output: $output,
);
}
/**
* Aborts the task, without finishing it properly
*/
public function abort (bool $success, ?string $output = null) : void
{
$this->finalizeRun(
success: $success,
finishedProperly: false,
output: $output,
);
}
/**
* Finalizes the run
*/
private function finalizeRun (
bool $success,
bool $finishedProperly,
?string $output = null,
) : void
{
if ($this->isFinished())
{
throw new InvalidLogActionException("Can't finalize task run #{$this->id} as it is already finished.");
}
if (null === $this->start)
{
throw new InvalidLogActionException("Can't finalize a task that wasn't started in this run.");
}
$this->success = $success;
$this->finishedProperly = $finishedProperly;
$this->output = $output;
$this->duration = hrtime(true) - $this->start;
}
}