Skip to content

Commit 00b1d91

Browse files
committed
Add documentation note about long running process workaround
1 parent 2facfa7 commit 00b1d91

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,64 @@ to see the live progress of the job.
4141

4242
![demo-web](demo-web.png)
4343

44+
## Long Running Single Progress Jobs
45+
46+
Due to the design of queued jobs, the progress indicator (currentStep) is only
47+
modified in the database at the end of a `process` call. Sometimes with long
48+
running single process jobs we need to display progress more verbosely.
49+
`QueuedJobProgressService` is designed as a drop-in replacement for
50+
`QueuedJobService`. The service allows your job to update the job descriptor
51+
more frequently.
52+
53+
Example Job
54+
55+
```
56+
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
57+
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
58+
use FullscreenInteractive\QueuedJobProgressField\QueuedJobProgressService;
59+
use SilverStripe\Core\Injector\Injector;
60+
61+
class MyAwesomeJob extends AbstractQueuedJob
62+
{
63+
protected $descriptor;
64+
65+
/**
66+
* By default the job descriptor is only ever updated when process() is
67+
* finished, so for long running single tasks the user see's no process.
68+
*
69+
* This method manually updates the count values on the QueuedJobDescriptor
70+
*/
71+
public function updateJobDescriptor()
72+
{
73+
if (!$this->descriptor && $this->jobDescriptorId) {
74+
$this->descriptor = QueuedJobDescriptor::get()->byId($this->jobDescriptorId);
75+
}
76+
77+
// rate limit the updater to only 1 query every sec, our front end only
78+
// updates every 1s as well.
79+
if ($this->descriptor && (!$this->lastUpdatedDescriptor || $this->lastUpdatedDescriptor < (strtotime('-1 SECOND')))) {
80+
Injector::inst()->get(QueuedJobProgressService::class)
81+
->copyJobToDescriptor($this, $this->descriptor);
82+
83+
$this->lastUpdatedDescriptor = time();
84+
}
85+
}
86+
87+
public function process()
88+
{
89+
$tasks = [
90+
// ..
91+
];
92+
93+
foreach ($tasks as $task) {
94+
$this->currentStep++;
95+
96+
// sends feedback to the database in the middle of process() allowing
97+
// long single processes to continue.
98+
$this->updateJobDescriptor();
99+
}
100+
101+
$this->isComplete = true;
102+
}
103+
}
104+
```

_config/queuedjobprogress.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
name: queuedjobprogress
3+
---
4+
SilverStripe\Core\Injector\Injector:
5+
Symbiote\QueuedJobs\Services\QueuedJobService:
6+
class: FullscreenInteractive\QueuedJobProgressField\QueuedJobProgressService

src/QueuedJobProgressService.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace FullscreenInteractive\QueuedJobProgressField;
4+
5+
use Symbiote\QueuedJobs\Services\QueuedJobService;
6+
7+
class QueuedJobProgressService extends QueuedJobService
8+
{
9+
/**
10+
* Make public
11+
*
12+
* {@inheritDoc}
13+
*/
14+
public function copyJobToDescriptor($job, $jobDescriptor)
15+
{
16+
return parent::copyJobToDescriptor($job, $jobDescriptor);
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function copyDescriptorToJob($jobDescriptor, $job)
23+
{
24+
parent::copyDescriptorToJob($jobDescriptor, $job);
25+
26+
$job->jobDescriptorId = $jobDescriptor->ID;
27+
}
28+
}

0 commit comments

Comments
 (0)