-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathBaseJob.php
More file actions
112 lines (98 loc) · 2.97 KB
/
Copy pathBaseJob.php
File metadata and controls
112 lines (98 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\queue;
use yii\base\BaseObject;
/**
* Job is the base class for classes representing jobs in terms of objects.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
abstract class BaseJob extends BaseObject implements JobInterface
{
/**
* @var string|null The configured job description.
*
* ::: tip
* Run the description through [[\craft\i18n\Translation::prep()]] rather than [[\yii\BaseYii::t()|Craft::t()]]
* so it can be lazy-translated for users’ preferred languages rather that the current app language.
* :::
*/
public ?string $description = null;
/**
* @var int|float The current progress
*/
private int|float $_progress;
/**
* @var string|null The current progress label
*/
private ?string $_progressLabel = null;
public function __wakeup(): void
{
$this->_progress = 0;
$this->_progressLabel = null;
}
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
// Set the default progress
$this->_progress = 0;
}
/**
* @inheritdoc
*/
public function getDescription(): ?string
{
return $this->description ?? $this->defaultDescription();
}
/**
* Returns a default description for [[getDescription()]].
*
* ::: tip
* Run the description through [[\craft\i18n\Translation::prep()]] rather than [[\yii\BaseYii::t()|Craft::t()]]
* so it can be lazy-translated for users’ preferred languages rather that the current app language.
* :::
*
* @return string|null
*/
protected function defaultDescription(): ?string
{
return null;
}
/**
* Sets the job progress on the queue.
*
* ::: tip
* Run the label through [[\craft\i18n\Translation::prep()]] rather than [[\yii\BaseYii::t()|Craft::t()]]
* so it can be lazy-translated for users’ preferred languages rather that the current app language.
* :::
*
* @param \yii\queue\Queue|QueueInterface $queue
* @param float $progress A number between 0 and 1
* @param string|null $label The progress label
*/
protected function setProgress(\yii\queue\Queue|QueueInterface $queue, float $progress, ?string $label = null): void
{
$progress = round(100 * $progress);
if (
$progress !== $this->_progress ||
($label !== null && $label !== $this->_progressLabel)
) {
$this->_progress = $progress;
// If $label == null, leave the existing value alone
if ($label !== null) {
$this->_progressLabel = $label;
}
if ($queue instanceof QueueInterface) {
$queue->setProgress((int)$progress, $label);
}
}
}
}