Skip to content

Commit 892718b

Browse files
author
Kopylova,Olga(okopylova)
committed
Merge pull request #174 from magento-ogre/PR_Branch
[Ogres] Bugfixes
2 parents c4bbc29 + ef60a26 commit 892718b

File tree

14 files changed

+150
-87
lines changed

14 files changed

+150
-87
lines changed

app/code/Magento/Cron/Model/Config/Backend/Sitemap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(
6565
public function afterSave()
6666
{
6767
$time = $this->getData('groups/generate/fields/time/value');
68-
$frequency = $this->getData('groups/generate/frequency/value');
68+
$frequency = $this->getData('groups/generate/fields/frequency/value');
6969

7070
$cronExprArray = [
7171
intval($time[1]), //Minute

app/code/Magento/Cron/Model/Observer.php

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class Observer
8787
*/
8888
protected $_shell;
8989

90+
/**
91+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
92+
*/
93+
protected $timezone;
94+
9095
/**
9196
* @param \Magento\Framework\ObjectManagerInterface $objectManager
9297
* @param ScheduleFactory $scheduleFactory
@@ -95,6 +100,7 @@ class Observer
95100
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
96101
* @param \Magento\Framework\App\Console\Request $request
97102
* @param \Magento\Framework\ShellInterface $shell
103+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
98104
*/
99105
public function __construct(
100106
\Magento\Framework\ObjectManagerInterface $objectManager,
@@ -103,7 +109,8 @@ public function __construct(
103109
\Magento\Cron\Model\ConfigInterface $config,
104110
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
105111
\Magento\Framework\App\Console\Request $request,
106-
\Magento\Framework\ShellInterface $shell
112+
\Magento\Framework\ShellInterface $shell,
113+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
107114
) {
108115
$this->_objectManager = $objectManager;
109116
$this->_scheduleFactory = $scheduleFactory;
@@ -112,6 +119,7 @@ public function __construct(
112119
$this->_scopeConfig = $scopeConfig;
113120
$this->_request = $request;
114121
$this->_shell = $shell;
122+
$this->timezone = $timezone;
115123
}
116124

117125
/**
@@ -128,30 +136,29 @@ public function __construct(
128136
public function dispatch($observer)
129137
{
130138
$pendingJobs = $this->_getPendingSchedules();
131-
$currentTime = time();
139+
$currentTime = $this->timezone->scopeTimeStamp();
132140
$jobGroupsRoot = $this->_config->getJobs();
133141

134142
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+
)) {
142152
$this->_shell->execute(
143-
'%s -f %s -- --group=%s',
153+
'php -f %s -- --group=%s --standaloneProcessStarted=%s',
144154
[
145-
PHP_BINARY,
146155
BP . '/' . DirectoryList::PUB . '/cron.php',
147-
$groupId
156+
$groupId,
157+
'1'
148158
]
149159
);
150160
continue;
151161
}
152-
if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') != $groupId) {
153-
continue;
154-
}
155162

156163
foreach ($pendingJobs as $schedule) {
157164
$jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null;
@@ -160,12 +167,14 @@ public function dispatch($observer)
160167
}
161168

162169
$scheduledTime = strtotime($schedule->getScheduledAt());
163-
if ($scheduledTime > $currentTime || !$schedule->tryLockJob()) {
170+
if ($scheduledTime > $currentTime) {
164171
continue;
165172
}
166173

167174
try {
168-
$this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId);
175+
if ($schedule->tryLockJob()) {
176+
$this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId);
177+
}
169178
} catch (\Exception $e) {
170179
$schedule->setMessages($e->getMessage());
171180
}
@@ -213,11 +222,14 @@ protected function _runJob($scheduledTime, $currentTime, $jobConfig, $schedule,
213222
);
214223
}
215224

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();
217226

218227
call_user_func_array($callback, [$schedule]);
219228

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+
));
221233
}
222234

223235
/**
@@ -253,7 +265,7 @@ protected function _generate($groupId)
253265
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
254266
);
255267
$schedulePeriod = $rawSchedulePeriod * self::SECONDS_IN_MINUTE;
256-
if ($lastRun > time() - $schedulePeriod) {
268+
if ($lastRun > $this->timezone->scopeTimeStamp() - $schedulePeriod) {
257269
return $this;
258270
}
259271

@@ -273,7 +285,12 @@ protected function _generate($groupId)
273285
/**
274286
* save time schedules generation was ran with no expiration
275287
*/
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+
);
277294

278295
return $this;
279296
}
@@ -324,7 +341,7 @@ protected function _cleanup($groupId)
324341
'system/cron/' . $groupId . '/' . self::XML_PATH_HISTORY_CLEANUP_EVERY,
325342
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
326343
);
327-
if ($lastCleanup > time() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
344+
if ($lastCleanup > $this->timezone->scopeTimeStamp() - $historyCleanUp * self::SECONDS_IN_MINUTE) {
328345
return $this;
329346
}
330347

@@ -350,7 +367,7 @@ protected function _cleanup($groupId)
350367
Schedule::STATUS_ERROR => $historyFailure * self::SECONDS_IN_MINUTE,
351368
];
352369

353-
$now = time();
370+
$now = $this->timezone->scopeTimeStamp();
354371
/** @var Schedule $record */
355372
foreach ($history as $record) {
356373
if (strtotime($record->getExecutedAt()) < $now - $historyLifetimes[$record->getStatus()]) {
@@ -359,7 +376,12 @@ protected function _cleanup($groupId)
359376
}
360377

361378
// 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+
);
363385

364386
return $this;
365387
}
@@ -387,19 +409,19 @@ protected function getConfigSchedule($jobConfig)
387409
*/
388410
protected function saveSchedule($jobCode, $cronExpression, $timeInterval, $exists)
389411
{
390-
$currentTime = time();
412+
$currentTime = $this->timezone->scopeTimeStamp();
391413
$timeAhead = $currentTime + $timeInterval;
392414
for ($time = $currentTime; $time < $timeAhead; $time += self::SECONDS_IN_MINUTE) {
393415
$ts = strftime('%Y-%m-%d %H:%M:00', $time);
394416
if (!empty($exists[$jobCode . '/' . $ts])) {
395417
// already scheduled
396418
continue;
397419
}
398-
399420
$schedule = $this->generateSchedule($jobCode, $cronExpression, $time);
400421
if ($schedule->trySchedule()) {
401422
// time matches cron expression
402423
$schedule->save();
424+
return;
403425
}
404426
}
405427
}
@@ -416,7 +438,7 @@ protected function generateSchedule($jobCode, $cronExpression, $time)
416438
->setCronExpr($cronExpression)
417439
->setJobCode($jobCode)
418440
->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()))
420442
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
421443

422444
return $schedule;

app/code/Magento/Cron/Model/Schedule.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,20 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
4444

4545
const STATUS_ERROR = 'error';
4646

47-
/**
48-
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
49-
*/
50-
protected $timezone;
51-
5247
/**
5348
* @param \Magento\Framework\Model\Context $context
5449
* @param \Magento\Framework\Registry $registry
55-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
5650
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
5751
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
5852
* @param array $data
5953
*/
6054
public function __construct(
6155
\Magento\Framework\Model\Context $context,
6256
\Magento\Framework\Registry $registry,
63-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
6457
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
6558
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
6659
array $data = []
6760
) {
68-
$this->timezone = $timezone;
6961
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
7062
}
7163

@@ -111,14 +103,11 @@ public function trySchedule()
111103
if (!is_numeric($time)) {
112104
$time = strtotime($time);
113105
}
114-
115-
$dateWithTimezone = $this->timezone->date($time);
116-
117-
$match = $this->matchCronExpression($e[0], $dateWithTimezone->format('i'))
118-
&& $this->matchCronExpression($e[1], $dateWithTimezone->format('H'))
119-
&& $this->matchCronExpression($e[2], $dateWithTimezone->format('d'))
120-
&& $this->matchCronExpression($e[3], $dateWithTimezone->format('m'))
121-
&& $this->matchCronExpression($e[4], $dateWithTimezone->format('N'));
106+
$match = $this->matchCronExpression($e[0], strftime('%M', $time))
107+
&& $this->matchCronExpression($e[1], strftime('%H', $time))
108+
&& $this->matchCronExpression($e[2], strftime('%d', $time))
109+
&& $this->matchCronExpression($e[3], strftime('%m', $time))
110+
&& $this->matchCronExpression($e[4], strftime('%w', $time));
122111

123112
return $match;
124113
}

app/code/Magento/Cron/Test/Unit/Model/ObserverTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class ObserverTest extends \PHPUnit_Framework_TestCase
5959
*/
6060
protected $_cronGroupConfig;
6161

62+
/**
63+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
64+
*/
65+
protected $timezone;
66+
6267
/**
6368
* Prepare parameters
6469
*/
@@ -93,14 +98,17 @@ public function setUp()
9398
['execute']
9499
)->getMock();
95100

101+
$this->timezone = $this->getMock('Magento\Framework\Stdlib\DateTime\TimezoneInterface');
102+
$this->timezone->expects($this->any())->method('scopeTimeStamp')->will($this->returnValue(time()));
96103
$this->_observer = new \Magento\Cron\Model\Observer(
97104
$this->_objectManager,
98105
$this->_scheduleFactory,
99106
$this->_cache,
100107
$this->_config,
101108
$this->_scopeConfig,
102109
$this->_request,
103-
$this->_shell
110+
$this->_shell,
111+
$this->timezone
104112
);
105113
}
106114

@@ -161,16 +169,17 @@ public function testDispatchCanNotLock()
161169
$lastRun = time() + 10000000;
162170
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
163171
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
164-
172+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
165173
$schedule = $this->getMockBuilder(
166174
'Magento\Cron\Model\Schedule'
167175
)->setMethods(
168-
['getJobCode', 'tryLockJob', 'getScheduledAt', '__wakeup']
176+
['getJobCode', 'tryLockJob', 'getScheduledAt', '__wakeup', 'save']
169177
)->disableOriginalConstructor()->getMock();
170178
$schedule->expects($this->any())->method('getJobCode')->will($this->returnValue('test_job1'));
171179
$schedule->expects($this->once())->method('getScheduledAt')->will($this->returnValue('-1 day'));
172180
$schedule->expects($this->once())->method('tryLockJob')->will($this->returnValue(false));
173-
181+
$abstractModel = $this->getMock('Magento\Framework\Model\AbstractModel', [], [], '', false);
182+
$schedule->expects($this->any())->method('save')->will($this->returnValue($abstractModel));
174183
$this->_collection->addItem($schedule);
175184

176185
$this->_config->expects(
@@ -198,7 +207,7 @@ public function testDispatchExceptionTooLate()
198207
$lastRun = time() + 10000000;
199208
$this->_cache->expects($this->any())->method('load')->will($this->returnValue($lastRun));
200209
$this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0));
201-
210+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
202211
$schedule = $this->getMockBuilder(
203212
'Magento\Cron\Model\Schedule'
204213
)->setMethods(
@@ -262,7 +271,7 @@ public function testDispatchExceptionNoCallback()
262271
);
263272
$schedule->expects($this->once())->method('setMessages')->with($this->equalTo($exceptionMessage));
264273
$schedule->expects($this->once())->method('save');
265-
274+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
266275
$this->_collection->addItem($schedule);
267276

268277
$jobConfig = ['test_group' => ['test_job1' => ['instance' => 'Some_Class']]];
@@ -293,7 +302,7 @@ public function testDispatchExceptionNotExecutable()
293302
];
294303

295304
$exceptionMessage = 'Invalid callback: Not_Existed_Class::notExistedMethod can\'t be called';
296-
305+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
297306
$schedule = $this->getMockBuilder(
298307
'Magento\Cron\Model\Schedule'
299308
)->setMethods(
@@ -349,6 +358,7 @@ public function testDispatchRunJob()
349358
$jobConfig = [
350359
'test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']],
351360
];
361+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
352362

353363
$scheduleMethods = [
354364
'getJobCode',
@@ -430,7 +440,7 @@ public function testDispatchNotGenerate()
430440
)->will(
431441
$this->returnValue(['test_group' => []])
432442
);
433-
443+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
434444
$this->_cache->expects(
435445
$this->at(0)
436446
)->method(
@@ -501,7 +511,7 @@ public function testDispatchGenerate()
501511
],
502512
];
503513
$this->_config->expects($this->at(1))->method('getJobs')->will($this->returnValue($jobs));
504-
514+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
505515
$this->_cache->expects(
506516
$this->at(0)
507517
)->method(
@@ -568,7 +578,7 @@ public function testDispatchCleanup()
568578
)->getMock();
569579
$schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue('-1 day'));
570580
$schedule->expects($this->any())->method('getStatus')->will($this->returnValue('success'));
571-
581+
$this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group'));
572582
$this->_collection->addItem($schedule);
573583

574584
$this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));

0 commit comments

Comments
 (0)