Skip to content

Commit 0811293

Browse files
authored
Merge pull request #34 from packagist/t/job-wait-helper
Jobs: add helper service to wait for a job to finish
2 parents 10295aa + 6c0f58f commit 0811293

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,25 @@ $client->mirroredRepositories()->removePackages($mirroredRepositoryId);
554554
##### Show a job
555555
```php
556556
$job = $client->jobs()->show($jobId);
557+
```
558+
Returns the job.
559+
560+
##### Wait for a job to finish
561+
This will periodically poll the job status until the job either finished or the maximum wait time was reached
562+
```php
563+
$numberOfSecondsToWait = 180;
564+
$jobHelper = new \PrivatePackagist\ApiClient\JobHelper($client);
565+
try {
566+
$job = $jobHelper->waitForJob($jobId, $numberOfSecondsToWait);
567+
} catch (\PrivatePackagist\ApiClient\Exception\JobTimeoutException $e) {
568+
// Job didn't finish within the specified time
569+
} catch (\PrivatePackagist\ApiClient\Exception\JobErrorException $e) {
570+
// Job finished with an error. See the message for more information
571+
echo $e->getMessage();
572+
}
573+
574+
575+
557576
```
558577
Returns the job.
559578

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
class JobErrorException extends RuntimeException
6+
{
7+
/** @var array */
8+
private $job;
9+
10+
public function __construct(array $job)
11+
{
12+
$this->job = $job;
13+
14+
parent::__construct($job['message']);
15+
}
16+
17+
/**
18+
* @return array
19+
*/
20+
public function getJob()
21+
{
22+
return $this->job;
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient\Exception;
4+
5+
class JobTimeoutException extends RuntimeException
6+
{
7+
8+
}

src/JobHelper.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PrivatePackagist\ApiClient;
4+
5+
use PrivatePackagist\ApiClient\Exception\JobErrorException;
6+
use PrivatePackagist\ApiClient\Exception\JobTimeoutException;
7+
8+
class JobHelper
9+
{
10+
/** @var Client */
11+
private $packagistClient;
12+
13+
public function __construct(Client $packagistClient)
14+
{
15+
$this->packagistClient = $packagistClient;
16+
}
17+
18+
/**
19+
* @param string $jobId
20+
* @param int $maxWaitSeconds
21+
* @param int $waitInterval
22+
* @return array
23+
*/
24+
public function waitForJob($jobId, $maxWaitSeconds = 180, $waitInterval = 5)
25+
{
26+
$maxWaitTime = new \DateTimeImmutable(sprintf('+%s seconds', $maxWaitSeconds));
27+
while ($maxWaitTime> new \DateTimeImmutable()) {
28+
$job = $this->packagistClient->jobs()->show($jobId);
29+
30+
if ($job['status'] === 'success') {
31+
return $job;
32+
}
33+
34+
if ($job['status'] === 'error') {
35+
throw new JobErrorException($job);
36+
}
37+
38+
sleep($waitInterval);
39+
}
40+
41+
throw new JobTimeoutException(sprintf('Job has not finish after %s seconds', $maxWaitSeconds));
42+
}
43+
}

0 commit comments

Comments
 (0)