Skip to content

Commit 25035c4

Browse files
committed
TASK: improve APIs
1 parent c36c31e commit 25035c4

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

Classes/Dto/Jobs.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Flowpack\Prunner\Dto;
44

5+
use Flowpack\Prunner\ValueObject\PipelineName;
56
use Neos\Flow\Annotations as Flow;
67

78
/**
@@ -29,11 +30,11 @@ public function fromJsonArray(array $in): self
2930
return new self($converted);
3031
}
3132

32-
public function forPipeline(string $pipeline): Jobs
33+
public function forPipeline(PipelineName $pipeline): Jobs
3334
{
3435
$filteredJobs = [];
3536
foreach ($this->jobs as $job) {
36-
if ($job->getPipeline() === $pipeline) {
37+
if ($job->getPipeline() === $pipeline->getName()) {
3738
$filteredJobs[] = $job;
3839
}
3940
}
@@ -42,7 +43,25 @@ public function forPipeline(string $pipeline): Jobs
4243
}
4344

4445
/**
45-
* @return \Iterator<Job>
46+
* Filter running jobs
47+
*
48+
* @return Jobs
49+
*/
50+
public function running(): Jobs
51+
{
52+
$filteredJobs = [];
53+
foreach ($this->jobs as $job) {
54+
// running = started jobs which have not finished.
55+
if ($job->getStart() !== null && !$job->getEnd()) {
56+
$filteredJobs[] = $job;
57+
}
58+
}
59+
60+
return new self($filteredJobs);
61+
}
62+
63+
/**
64+
* @return \Iterator<Job>|Job[]
4665
*/
4766
public function getIterator()
4867
{

Classes/Dto/PipelinesAndJobsResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function __construct(Pipelines $pipelines, Jobs $jobs)
1919
$this->jobs = $jobs;
2020
}
2121

22-
public function fromJsonArray(array $in): self
22+
public static function fromJsonArray(array $in): self
2323
{
2424
$pipelines = Pipelines::fromJsonArray($in['pipelines']);
2525
$jobs = Jobs::fromJsonArray($in['jobs']);

Classes/PrunnerApiService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Flowpack\Prunner;
44

55
use Firebase\JWT\JWT;
6+
use Flowpack\Prunner\Dto\Job;
67
use Flowpack\Prunner\Dto\PipelinesAndJobsResponse;
78
use Flowpack\Prunner\ValueObject\JobId;
89
use Flowpack\Prunner\ValueObject\PipelineName;
@@ -50,6 +51,13 @@ public function loadPipelinesAndJobs(): PipelinesAndJobsResponse
5051
return PipelinesAndJobsResponse::fromJsonArray($result);
5152
}
5253

54+
public function loadJobDetail(JobId $jobId): Job
55+
{
56+
$resultString = $this->apiCall('GET', 'job/detail?' . http_build_query(['id' => $jobId->getId()]), null)->getBody()->getContents();
57+
$result = json_decode($resultString, true);
58+
return Job::fromJsonArray($result);
59+
}
60+
5361
public function schedulePipeline(PipelineName $pipeline, array $variables): JobId
5462
{
5563
$response = $this->apiCall('POST', 'pipelines/schedule', json_encode([
@@ -64,6 +72,14 @@ public function schedulePipeline(PipelineName $pipeline, array $variables): JobI
6472
return JobId::create($tmp['jobId']);
6573
}
6674

75+
public function cancelJob(Job $job): void
76+
{
77+
$response = $this->apiCall('POST', 'job/cancel?' . http_build_query(['id' => $job->getId()]), '');
78+
if ($response->getStatusCode() !== 200) {
79+
throw new \RuntimeException('Cancelling a job should have returned status code 200, but got: ' . $response->getStatusCode());
80+
}
81+
}
82+
6783
/**
6884
* Low-Level method, handling only the authentication.
6985
*

0 commit comments

Comments
 (0)