cron.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. require_once __DIR__ . '/app/bootstrap.php';
  7. /**
  8. * Update cron exit codes
  9. */
  10. const UPDATE_CRON_NORMAL_EXIT = 0;
  11. const UPDATE_CRON_EXIT_WITH_ERROR = 1;
  12. $status = new \Magento\Update\Status();
  13. $cronReadinessChecker = new \Magento\Update\CronReadinessCheck();
  14. $notification = 'update-cron: Please check var/log/update.log for execution summary.' . PHP_EOL;
  15. if (!$cronReadinessChecker->runReadinessCheck()) {
  16. print $notification;
  17. exit(UPDATE_CRON_EXIT_WITH_ERROR);
  18. }
  19. if ($status->isUpdateInProgress()) {
  20. $status->add('Update is already in progress.', \Psr\Log\LogLevel::WARNING);
  21. print $notification;
  22. exit(UPDATE_CRON_EXIT_WITH_ERROR);
  23. }
  24. if ($status->isUpdateError()) {
  25. $status->add('There was an error in previous Update attempt.');
  26. print $notification;
  27. exit(UPDATE_CRON_EXIT_WITH_ERROR);
  28. }
  29. $backupDirectory = BACKUP_DIR;
  30. if (!file_exists($backupDirectory)) {
  31. if (!mkdir($backupDirectory)) {
  32. $status->add(sprintf('Backup directory, "%s" cannot be created.', $backupDirectory), \Psr\Log\LogLevel::ERROR);
  33. print $notification;
  34. exit(UPDATE_CRON_EXIT_WITH_ERROR);
  35. }
  36. chmod($backupDirectory, 0770);
  37. }
  38. try {
  39. $status->setUpdateInProgress();
  40. } catch (\RuntimeException $e) {
  41. $status->add($e->getMessage(), \Psr\Log\LogLevel::ERROR);
  42. print $notification;
  43. exit(UPDATE_CRON_EXIT_WITH_ERROR);
  44. }
  45. $jobQueue = new \Magento\Update\Queue();
  46. $exitCode = UPDATE_CRON_NORMAL_EXIT;
  47. try {
  48. while (!empty($jobQueue->peek()) &&
  49. strpos($jobQueue->peek()[\Magento\Update\Queue::KEY_JOB_NAME], 'setup:') === false
  50. ) {
  51. $job = $jobQueue->popQueuedJob();
  52. $status->add(
  53. sprintf('Job "%s" has been started', $job)
  54. );
  55. try {
  56. $job->execute();
  57. $status->add(sprintf('Job "%s" has successfully completed', $job), \Psr\Log\LogLevel::INFO);
  58. } catch (\Exception $e) {
  59. $status->setUpdateError();
  60. $status->add(
  61. sprintf(
  62. 'An error occurred while executing job "%s": %s', $job, $e->getMessage(),
  63. \Psr\Log\LogLevel::ERROR
  64. )
  65. );
  66. $status->setUpdateInProgress(false);
  67. $exitCode = UPDATE_CRON_EXIT_WITH_ERROR;
  68. };
  69. }
  70. } catch (\Exception $e) {
  71. $status->setUpdateError();
  72. $status->add($e->getMessage(), \Psr\Log\LogLevel::ERROR);
  73. $exitCode = UPDATE_CRON_EXIT_WITH_ERROR;
  74. } finally {
  75. $status->setUpdateInProgress(false);
  76. if ($exitCode != UPDATE_CRON_NORMAL_EXIT) {
  77. print $notification;
  78. }
  79. exit($exitCode);
  80. }