Skip to content

Commit 5fbb7d8

Browse files
committed
Job: explain functionality of the job helper in the readme
1 parent 68e6846 commit 5fbb7d8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,21 @@ $job = $client->jobs()->show($jobId);
558558
Returns the job.
559559

560560
##### 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
561562
```php
562-
$numberOfMinutesToWait = 3;
563+
$numberOfSecondsToWait = 30;
563564
$jobHelper = new \PrivatePackagist\ApiClient\JobHelper($client);
564-
$job = $jobHelper->waitForJob($jobId, $numberOfMinutesToWait);
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+
565576
```
566577
Returns the job.
567578

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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PrivatePackagist\ApiClient;
44

55
use PrivatePackagist\ApiClient\Exception\JobErrorException;
6+
use PrivatePackagist\ApiClient\Exception\JobTimeoutException;
67

78
class JobHelper
89
{
@@ -14,9 +15,15 @@ public function __construct(Client $packagistClient)
1415
$this->packagistClient = $packagistClient;
1516
}
1617

17-
public function waitForJob($jobId, $maxWaitMinutes = 3)
18+
/**
19+
* @param string $jobId
20+
* @param int $maxWaitSeconds
21+
* @param int $waitInterval
22+
* @return array
23+
*/
24+
public function waitForJob($jobId, $maxWaitSeconds = 30, $waitInterval = 5)
1825
{
19-
$maxWaitTime = new \DateTimeImmutable(sprintf('+%s minutes', $maxWaitMinutes));
26+
$maxWaitTime = new \DateTimeImmutable(sprintf('+%s seconds', $maxWaitSeconds));
2027
while ($maxWaitTime> new \DateTimeImmutable()) {
2128
$job = $this->packagistClient->jobs()->show($jobId);
2229

@@ -28,9 +35,9 @@ public function waitForJob($jobId, $maxWaitMinutes = 3)
2835
throw new JobErrorException($job);
2936
}
3037

31-
sleep(1);
38+
sleep($waitInterval);
3239
}
3340

34-
throw new \Exception(sprintf('Job has not finish after %s minutes', $maxWaitMinutes));
41+
throw new JobTimeoutException(sprintf('Job has not finish after %s seconds', $maxWaitSeconds));
3542
}
3643
}

0 commit comments

Comments
 (0)