Issues (31)

src/Jobs/CheckForUpdatesJob.php (2 issues)

Severity
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Jobs;
4
5
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
6
use DateTime;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use Symbiote\QueuedJobs\Services\QueuedJob;
10
use SilverStripe\Core\Injector\Injector;
11
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
14
/**
15
 * Refresh report job. Runs as a queued job.
16
 *
17
 */
18
class CheckForUpdatesJob extends AbstractQueuedJob implements QueuedJob
19
{
20
    /**
21
     * Whether or not to reschedule a new job when one completes
22
     *
23
     * @config
24
     * @var bool
25
     */
26
    private static $reschedule = true;
0 ignored issues
show
The private property $reschedule is not used, and could be removed.
Loading history...
27
28
    /**
29
     * The PHP time difference to reschedule a job for after one completes
30
     *
31
     * @config
32
     * @var string
33
     */
34
    private static $reschedule_delay = '+1 day';
0 ignored issues
show
The private property $reschedule_delay is not used, and could be removed.
Loading history...
35
36
    /**
37
     * Define the title
38
     *
39
     * @return string
40
     */
41
    public function getTitle()
42
    {
43
        return _t(__CLASS__ . '.TITLE', 'Check for updates to installed modules');
44
    }
45
46
    /**
47
     * Define the type.
48
     */
49
    public function getJobType()
50
    {
51
        $this->totalSteps = 1;
52
        return QueuedJob::QUEUED;
53
    }
54
55
    /**
56
     * Processes the task as a job
57
     */
58
    public function process()
59
    {
60
        // Run the UpdatePackageInfo task
61
        $updateTask = Injector::inst()->create(UpdatePackageInfoTask::class);
62
        $updateTask->run(null);
63
64
        // mark job as completed
65
        $this->isComplete = true;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function afterComplete()
72
    {
73
        // Gather config options
74
        $reschedule = Config::inst()->get(__CLASS__, 'reschedule');
75
        $rescheduleDelay = Config::inst()->get(__CLASS__, 'reschedule_delay');
76
77
        if ($reschedule === false) {
78
            return;
79
        }
80
81
        // Queue a new job to run in the future
82
        $injector = Injector::inst();
83
        $queuedJobService = $injector->get(QueuedJobService::class);
84
85
        $startAfter = new DateTime(DBDatetime::now()->getValue());
86
        $startAfter->modify($rescheduleDelay);
87
        $queuedJobService->queueJob(
88
            $injector->create(CheckForUpdatesJob::class),
89
            $startAfter->format(DateTime::ISO8601)
90
        );
91
    }
92
}
93