@@ -41,3 +41,64 @@ to see the live progress of the job.
41
41
42
42
![ demo-web] ( demo-web.png )
43
43
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
+ ```
0 commit comments