| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\CronTask\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cron\CronExpression; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\CronTask\CronTaskStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\CronTask\Interfaces\CronTask; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SilverStripe\ORM\FieldType\DBDatetime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SilverStripe\Security\Permission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SilverStripe\Security\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * This is the controller that finds, checks and process all crontasks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * The default route to this controller is 'dev/cron' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class CronTaskController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * If this controller is in quiet mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @deprecated Use $verbosity instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $quiet = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Tell the controller how noisy it may be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var int A number from 0 to 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected $verbosity = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Tell the controller how noisy it may be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @deprecated Use setVerbosity instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param bool $quiet If set to true this controller will not emit debug noise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function setQuiet($quiet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->setVerbosity($quiet ? 0 : 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->quiet = (bool) $quiet; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Tell the controller how noisy it may be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param int $verbosity An integer from 0 to 2. 0 = no output, 1 = normal, 2 = debug | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public function setVerbosity($verbosity) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->verbosity = (int) $verbosity; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Checks for cli or admin permissions and include the library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         // Unless called from the command line, we need ADMIN privileges | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!Director::is_cli() && !Permission::check('ADMIN')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             Security::permissionFailure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // set quiet flag based on CLI parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($this->getRequest()->getVar('quiet')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->setVerbosity(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if ($this->getRequest()->getVar('debug')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->setVerbosity(2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Determine if a task should be run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param CronTask $task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param CronExpression $cron | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function isTaskDue(CronTask $task, CronExpression $cron) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         // Get last run status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $status = CronTaskStatus::get_status(get_class($task)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         // If the cron is due immediately, then run it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $now = new DateTime(DBDatetime::now()->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if ($cron->isDue($now)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if (empty($status) || empty($status->LastRun)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             // In case this process is invoked twice in one minute, supress subsequent executions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $lastRun = new DateTime($status->LastRun); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return $lastRun->format('Y-m-d H:i') != $now->format('Y-m-d H:i'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // If this is the first time this task is ever checked, no way to detect postponed execution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (empty($status) || empty($status->LastChecked)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         // Determine if we have passed the last expected run time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $nextExpectedDate = $cron->getNextRunDate($status->LastChecked); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $nextExpectedDate <= $now; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Default controller action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function index(HTTPRequest $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         // Show more debug info with ?debug=1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $isDebug = (bool)$request->getVar('debug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         // Check each task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $tasks = ClassInfo::implementorsOf(CronTask::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if (empty($tasks)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $this->output("There are no implementators of CronTask to run", 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         foreach ($tasks as $subclass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $task = Injector::inst()->create($subclass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $this->runTask($task, $isDebug); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * Checks and runs a single CronTask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param CronTask $task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     public function runTask(CronTask $task) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $cron = CronExpression::factory($task->getSchedule()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $isDue = $this->isTaskDue($task, $cron); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         // Update status of this task prior to execution in case of interruption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         CronTaskStatus::update_status(get_class($task), $isDue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         if ($isDue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $this->output(get_class($task) . ' will start now.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $task->process(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $this->output(get_class($task) . ' will run at ' . $cron->getNextRunDate()->format('Y-m-d H:i:s') . '.', 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * Output a message to the browser or CLI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function output($message, $minVerbosity = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($this->verbosity < $minVerbosity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         $timestamp = DBDatetime::now()->Format('Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             echo $timestamp . ' - ' . $message . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             echo Convert::raw2xml($timestamp . ' - ' . $message) . '<br />' . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  | 
            
                        
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.