@@ -87,6 +87,11 @@ class Observer
87
87
*/
88
88
protected $ _shell ;
89
89
90
+ /**
91
+ * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
92
+ */
93
+ protected $ timezone ;
94
+
90
95
/**
91
96
* @param \Magento\Framework\ObjectManagerInterface $objectManager
92
97
* @param ScheduleFactory $scheduleFactory
@@ -95,6 +100,7 @@ class Observer
95
100
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
96
101
* @param \Magento\Framework\App\Console\Request $request
97
102
* @param \Magento\Framework\ShellInterface $shell
103
+ * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
98
104
*/
99
105
public function __construct (
100
106
\Magento \Framework \ObjectManagerInterface $ objectManager ,
@@ -103,7 +109,8 @@ public function __construct(
103
109
\Magento \Cron \Model \ConfigInterface $ config ,
104
110
\Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
105
111
\Magento \Framework \App \Console \Request $ request ,
106
- \Magento \Framework \ShellInterface $ shell
112
+ \Magento \Framework \ShellInterface $ shell ,
113
+ \Magento \Framework \Stdlib \DateTime \TimezoneInterface $ timezone
107
114
) {
108
115
$ this ->_objectManager = $ objectManager ;
109
116
$ this ->_scheduleFactory = $ scheduleFactory ;
@@ -112,6 +119,7 @@ public function __construct(
112
119
$ this ->_scopeConfig = $ scopeConfig ;
113
120
$ this ->_request = $ request ;
114
121
$ this ->_shell = $ shell ;
122
+ $ this ->timezone = $ timezone ;
115
123
}
116
124
117
125
/**
@@ -128,30 +136,29 @@ public function __construct(
128
136
public function dispatch ($ observer )
129
137
{
130
138
$ pendingJobs = $ this ->_getPendingSchedules ();
131
- $ currentTime = time ();
139
+ $ currentTime = $ this -> timezone -> scopeTimeStamp ();
132
140
$ jobGroupsRoot = $ this ->_config ->getJobs ();
133
141
134
142
foreach ($ jobGroupsRoot as $ groupId => $ jobsRoot ) {
135
- if ($ this ->_request ->getParam (
136
- 'group '
137
- ) === null && $ this ->_scopeConfig ->getValue (
138
- 'system/cron/ ' . $ groupId . '/use_separate_process ' ,
139
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE
140
- ) == 1
141
- ) {
143
+ if ($ this ->_request ->getParam ('group ' ) !== null && $ this ->_request ->getParam ('group ' ) != $ groupId ) {
144
+ continue ;
145
+ }
146
+ if (($ this ->_request ->getParam ('standaloneProcessStarted ' ) !== '1 ' ) && (
147
+ $ this ->_scopeConfig ->getValue (
148
+ 'system/cron/ ' . $ groupId . '/use_separate_process ' ,
149
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
150
+ ) == 1
151
+ )) {
142
152
$ this ->_shell ->execute (
143
- '%s -f %s -- --group=%s ' ,
153
+ 'php -f %s -- --group=%s --standaloneProcessStarted =%s ' ,
144
154
[
145
- PHP_BINARY ,
146
155
BP . '/ ' . DirectoryList::PUB . '/cron.php ' ,
147
- $ groupId
156
+ $ groupId ,
157
+ '1 '
148
158
]
149
159
);
150
160
continue ;
151
161
}
152
- if ($ this ->_request ->getParam ('group ' ) !== null && $ this ->_request ->getParam ('group ' ) != $ groupId ) {
153
- continue ;
154
- }
155
162
156
163
foreach ($ pendingJobs as $ schedule ) {
157
164
$ jobConfig = isset ($ jobsRoot [$ schedule ->getJobCode ()]) ? $ jobsRoot [$ schedule ->getJobCode ()] : null ;
@@ -160,12 +167,14 @@ public function dispatch($observer)
160
167
}
161
168
162
169
$ scheduledTime = strtotime ($ schedule ->getScheduledAt ());
163
- if ($ scheduledTime > $ currentTime || ! $ schedule -> tryLockJob () ) {
170
+ if ($ scheduledTime > $ currentTime ) {
164
171
continue ;
165
172
}
166
173
167
174
try {
168
- $ this ->_runJob ($ scheduledTime , $ currentTime , $ jobConfig , $ schedule , $ groupId );
175
+ if ($ schedule ->tryLockJob ()) {
176
+ $ this ->_runJob ($ scheduledTime , $ currentTime , $ jobConfig , $ schedule , $ groupId );
177
+ }
169
178
} catch (\Exception $ e ) {
170
179
$ schedule ->setMessages ($ e ->getMessage ());
171
180
}
@@ -213,11 +222,14 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
213
222
);
214
223
}
215
224
216
- $ schedule ->setExecutedAt (strftime ('%Y-%m-%d %H:%M:%S ' , time ()))->save ();
225
+ $ schedule ->setExecutedAt (strftime ('%Y-%m-%d %H:%M:%S ' , $ this -> timezone -> scopeTimeStamp ()))->save ();
217
226
218
227
call_user_func_array ($ callback , [$ schedule ]);
219
228
220
- $ schedule ->setStatus (Schedule::STATUS_SUCCESS )->setFinishedAt (strftime ('%Y-%m-%d %H:%M:%S ' , time ()));
229
+ $ schedule ->setStatus (Schedule::STATUS_SUCCESS )->setFinishedAt (strftime (
230
+ '%Y-%m-%d %H:%M:%S ' ,
231
+ $ this ->timezone ->scopeTimeStamp ()
232
+ ));
221
233
}
222
234
223
235
/**
@@ -253,7 +265,7 @@ protected function _generate($groupId)
253
265
\Magento \Store \Model \ScopeInterface::SCOPE_STORE
254
266
);
255
267
$ schedulePeriod = $ rawSchedulePeriod * self ::SECONDS_IN_MINUTE ;
256
- if ($ lastRun > time () - $ schedulePeriod ) {
268
+ if ($ lastRun > $ this -> timezone -> scopeTimeStamp () - $ schedulePeriod ) {
257
269
return $ this ;
258
270
}
259
271
@@ -273,7 +285,12 @@ protected function _generate($groupId)
273
285
/**
274
286
* save time schedules generation was ran with no expiration
275
287
*/
276
- $ this ->_cache ->save (time (), self ::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $ groupId , ['crontab ' ], null );
288
+ $ this ->_cache ->save (
289
+ $ this ->timezone ->scopeTimeStamp (),
290
+ self ::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . $ groupId ,
291
+ ['crontab ' ],
292
+ null
293
+ );
277
294
278
295
return $ this ;
279
296
}
@@ -324,7 +341,7 @@ protected function _cleanup($groupId)
324
341
'system/cron/ ' . $ groupId . '/ ' . self ::XML_PATH_HISTORY_CLEANUP_EVERY ,
325
342
\Magento \Store \Model \ScopeInterface::SCOPE_STORE
326
343
);
327
- if ($ lastCleanup > time () - $ historyCleanUp * self ::SECONDS_IN_MINUTE ) {
344
+ if ($ lastCleanup > $ this -> timezone -> scopeTimeStamp () - $ historyCleanUp * self ::SECONDS_IN_MINUTE ) {
328
345
return $ this ;
329
346
}
330
347
@@ -350,7 +367,7 @@ protected function _cleanup($groupId)
350
367
Schedule::STATUS_ERROR => $ historyFailure * self ::SECONDS_IN_MINUTE ,
351
368
];
352
369
353
- $ now = time ();
370
+ $ now = $ this -> timezone -> scopeTimeStamp ();
354
371
/** @var Schedule $record */
355
372
foreach ($ history as $ record ) {
356
373
if (strtotime ($ record ->getExecutedAt ()) < $ now - $ historyLifetimes [$ record ->getStatus ()]) {
@@ -359,7 +376,12 @@ protected function _cleanup($groupId)
359
376
}
360
377
361
378
// save time history cleanup was ran with no expiration
362
- $ this ->_cache ->save (time (), self ::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $ groupId , ['crontab ' ], null );
379
+ $ this ->_cache ->save (
380
+ $ this ->timezone ->scopeTimeStamp (),
381
+ self ::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . $ groupId ,
382
+ ['crontab ' ],
383
+ null
384
+ );
363
385
364
386
return $ this ;
365
387
}
@@ -387,19 +409,19 @@ protected function getConfigSchedule($jobConfig)
387
409
*/
388
410
protected function saveSchedule ($ jobCode , $ cronExpression , $ timeInterval , $ exists )
389
411
{
390
- $ currentTime = time ();
412
+ $ currentTime = $ this -> timezone -> scopeTimeStamp ();
391
413
$ timeAhead = $ currentTime + $ timeInterval ;
392
414
for ($ time = $ currentTime ; $ time < $ timeAhead ; $ time += self ::SECONDS_IN_MINUTE ) {
393
415
$ ts = strftime ('%Y-%m-%d %H:%M:00 ' , $ time );
394
416
if (!empty ($ exists [$ jobCode . '/ ' . $ ts ])) {
395
417
// already scheduled
396
418
continue ;
397
419
}
398
-
399
420
$ schedule = $ this ->generateSchedule ($ jobCode , $ cronExpression , $ time );
400
421
if ($ schedule ->trySchedule ()) {
401
422
// time matches cron expression
402
423
$ schedule ->save ();
424
+ return ;
403
425
}
404
426
}
405
427
}
@@ -416,7 +438,7 @@ protected function generateSchedule($jobCode, $cronExpression, $time)
416
438
->setCronExpr ($ cronExpression )
417
439
->setJobCode ($ jobCode )
418
440
->setStatus (Schedule::STATUS_PENDING )
419
- ->setCreatedAt (strftime ('%Y-%m-%d %H:%M:%S ' , time ()))
441
+ ->setCreatedAt (strftime ('%Y-%m-%d %H:%M:%S ' , $ this -> timezone -> scopeTimeStamp ()))
420
442
->setScheduledAt (strftime ('%Y-%m-%d %H:%M ' , $ time ));
421
443
422
444
return $ schedule ;
0 commit comments