CronController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Drupal\Core\CronInterface;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. /**
  8. * Controller for Cron handling.
  9. */
  10. class CronController extends ControllerBase {
  11. /**
  12. * The cron service.
  13. *
  14. * @var \Drupal\Core\CronInterface
  15. */
  16. protected $cron;
  17. /**
  18. * Constructs a CronController object.
  19. *
  20. * @param \Drupal\Core\CronInterface $cron
  21. * The cron service.
  22. */
  23. public function __construct(CronInterface $cron) {
  24. $this->cron = $cron;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function create(ContainerInterface $container) {
  30. return new static($container->get('cron'));
  31. }
  32. /**
  33. * Run Cron once.
  34. *
  35. * @return \Symfony\Component\HttpFoundation\Response
  36. * A Symfony response object.
  37. */
  38. public function run() {
  39. $this->cron->run();
  40. // HTTP 204 is "No content", meaning "I did what you asked and we're done."
  41. return new Response('', 204);
  42. }
  43. /**
  44. * Run cron manually.
  45. *
  46. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  47. * A Symfony direct response object.
  48. */
  49. public function runManually() {
  50. if ($this->cron->run()) {
  51. $this->messenger()->addStatus($this->t('Cron ran successfully.'));
  52. }
  53. else {
  54. $this->messenger()->addError($this->t('Cron run failed.'));
  55. }
  56. return $this->redirect('system.status');
  57. }
  58. }