foldershare-8.x-1.2/src/EventSubscriber/FolderShareScheduledTaskHandler.php
src/EventSubscriber/FolderShareScheduledTaskHandler.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | <?php namespace Drupal\foldershare\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\PostResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Drupal\foldershare\Entity\FolderShareScheduledTask; /** * Handles executing scheduled tasks after user requests are serviced. * * HTTP requests to the server are processed by Drupal and a response * posted back to the user. Upon completion, the user gets their next * page and a "terminate" event is sent. This class responds to such * events and uses them to service the scheduled task queue. Because * these events occur very often (once per page), the task queue is * serviced very frequently, which allows for fairly fine-grained task * scheduling. * * Task servicing is disabled automatically when the site is in maintenance * mode. This prevents activity that could interfer with maintenance * activity, such as installing modules or performing module updates. * * To register this event subscriber, an entry in "foldershare.services.yml" * is required: * * @code * services: * foldershare.scheduledtask.subscriber: * class: Drupal\foldershare\EventSubscriber\FolderShareScheduledTaskHandler * arguments: [] * tags: * - { name: event_subscriber } * @endcode * * @ingroup foldershare * * @see foldershare.services.yml * @see \Drupal\foldershare\Entity\FolderShareScheduledTask */ class FolderShareScheduledTaskHandler implements EventSubscriberInterface { /** * Constructs the handler. */ public function __construct() { } /*--------------------------------------------------------------------- * * Event handling. * *---------------------------------------------------------------------*/ /** * Responds to a terminate event after a posted response. * * If the site is not in maintenance mode, the scheduled task list of * the FolderShare module is checked and ready tasks run. * * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event * The termination event. * * @see ::getSubscribedEvents() */ public function onTerminate(PostResponseEvent $event ) { $mode = \Drupal::state()->get( 'system.maintenance_mode' ); if ((int) $mode === 0) { // Invoke the task handler. The handler searches for any pending tasks // and executes them. $requestTime = (int) $event ->getRequest()->server->get( 'REQUEST_TIME' ); FolderShareScheduledTask::executeTasks( $requestTime ); } } /** * Subscribes to the terminate event. * * @return array * Returns an array of event listener definitions, including one that * invokes this class's terminate event handler. * * @see ::onTerminate() */ public static function getSubscribedEvents() { return [ KernelEvents::TERMINATE => [ [ 'onTerminate' , 100, ], ], ]; } } |