-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsyncJobsPresenter.php
More file actions
213 lines (191 loc) · 6.25 KB
/
AsyncJobsPresenter.php
File metadata and controls
213 lines (191 loc) · 6.25 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace App\V1Module\Presenters;
use App\Helpers\MetaFormats\Attributes\Post;
use App\Helpers\MetaFormats\Attributes\Query;
use App\Helpers\MetaFormats\Attributes\Path;
use App\Helpers\MetaFormats\Validators\VBool;
use App\Helpers\MetaFormats\Validators\VInt;
use App\Helpers\MetaFormats\Validators\VUuid;
use App\Async\Dispatcher;
use App\Async\Handler\PingAsyncJobHandler;
use App\Model\Repository\Assignments;
use App\Model\Repository\AsyncJobs;
use App\Security\ACL\IAssignmentPermissions;
use App\Security\ACL\IAsyncJobPermissions;
use App\Exceptions\NotFoundException;
use App\Exceptions\ForbiddenRequestException;
use App\Exceptions\BadRequestException;
use Doctrine\Common\Collections\Criteria;
use Exception;
use DateTime;
/**
* Basic management of asynchronous jobs executed by core systemd service.
* Async jobs are jobs that might take a long time, so they cannot be executed in request handler;
* however, they need to access functions of the core API module.
*/
class AsyncJobsPresenter extends BasePresenter
{
/**
* @var Dispatcher
* @inject
*/
public $dispatcher;
/**
* @var Assignments
* @inject
*/
public $assignments;
/**
* @var AsyncJobs
* @inject
*/
public $asyncJobs;
/**
* @var IAsyncJobPermissions
* @inject
*/
public $asyncJobsAcl;
/**
* @var IAssignmentPermissions
* @inject
*/
public $assignmentsAcl;
public function checkDefault(string $id)
{
$asyncJob = $this->asyncJobs->findOrThrow($id);
if (!$this->asyncJobsAcl->canViewDetail($asyncJob)) {
throw new ForbiddenRequestException("You cannot see details of given async job");
}
}
/**
* Retrieves details about particular async job.
* @GET
* @throws NotFoundException
*/
#[Path("id", new VUuid(), "job identifier", required: true)]
public function actionDefault(string $id)
{
$asyncJob = $this->asyncJobs->findOrThrow($id);
$this->sendSuccessResponse($asyncJob);
}
public function checkList()
{
if (!$this->asyncJobsAcl->canList()) {
throw new ForbiddenRequestException("You cannot list async jobs");
}
}
/**
* Retrieves details about async jobs that are either pending or were recently completed.
* @GET
* @throws BadRequestException
*/
#[Query(
"ageThreshold",
new VInt(),
"Maximal time since completion (in seconds), null = only pending operations",
required: false,
nullable: true,
)]
#[Query(
"includeScheduled",
new VBool(false),
"If true, pending scheduled events will be listed as well",
required: false,
nullable: true,
)]
public function actionList(?int $ageThreshold, ?bool $includeScheduled)
{
if ($ageThreshold && $ageThreshold < 0) {
throw new BadRequestException("Age threshold must not be negative.");
}
// criteria for termination (either pending or within threshold)
$finishedAt = Criteria::expr()->eq('finishedAt', null);
if ($ageThreshold) {
$thresholdDate = new DateTime();
$thresholdDate->modify("-$ageThreshold seconds");
$finishedAt = Criteria::expr()->orX(
$finishedAt,
Criteria::expr()->gte('finishedAt', $thresholdDate)
);
}
$criteria = Criteria::create()->where(
$includeScheduled
? $finishedAt
: Criteria::expr()->andX(
$finishedAt,
Criteria::expr()->eq('scheduledAt', null)
)
);
$criteria->orderBy(['createdAt' => 'ASC']);
$jobs = $this->asyncJobs->matching($criteria)->toArray();
$jobs = array_filter($jobs, function ($job) {
return $this->asyncJobsAcl->canViewDetail($job);
});
$this->sendSuccessResponse($jobs);
}
public function checkAbort(string $id)
{
$asyncJob = $this->asyncJobs->findOrThrow($id);
if (!$this->asyncJobsAcl->canAbort($asyncJob)) {
throw new ForbiddenRequestException("You cannot abort selected async job");
}
}
/**
* Retrieves details about particular async job.
* @POST
* @throws NotFoundException
*/
#[Path("id", new VUuid(), "job identifier", required: true)]
public function actionAbort(string $id)
{
$this->asyncJobs->beginTransaction();
try {
$asyncJob = $this->asyncJobs->findOrThrow($id);
if ($asyncJob->getStartedAt() === null && $asyncJob->getFinishedAt() === null) {
// if the job has not been started yet, it can be aborted
$asyncJob->setFinishedNow();
$asyncJob->appendError("ABORTED");
$this->asyncJobs->persist($asyncJob);
$this->asyncJobs->commit();
} else {
$this->asyncJobs->rollback();
}
} catch (Exception $e) {
$this->asyncJobs->rollback();
throw $e;
}
$this->sendSuccessResponse($asyncJob);
}
public function checkPing()
{
if (!$this->asyncJobsAcl->canPing()) {
throw new ForbiddenRequestException("You cannot ping async job worker");
}
}
/**
* Initiates ping job. An empty job designed to verify the async handler is running.
* @POST
*/
public function actionPing()
{
$asyncJob = PingAsyncJobHandler::dispatchAsyncJob($this->dispatcher, $this->getCurrentUser());
$this->sendSuccessResponse($asyncJob);
}
public function checkAssignmentJobs($id)
{
$assignment = $this->assignments->findOrThrow($id);
if (!$this->assignmentsAcl->canViewAssignmentAsyncJobs($assignment)) {
throw new ForbiddenRequestException("You cannot list async jobs of given assignment");
}
}
/**
* Get all pending async jobs related to a particular assignment.
* @GET
*/
#[Path("id", new VUuid(), required: true)]
public function actionAssignmentJobs($id)
{
$asyncJobs = $this->asyncJobs->findAssignmentJobs($id);
$this->sendSuccessResponse($asyncJobs);
}
}