aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/cron/event/cron_runner_listener.php
blob: 9e9ecf0d47284062d33fe36040406e82e7bf57dc (plain)
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
99
100
101
102
103
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\cron\event;

use phpbb\cron\manager;
use phpbb\lock\db;
use phpbb\request\request_interface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;

/**
 * Event listener that executes cron tasks, after the response was served
 */
class cron_runner_listener implements EventSubscriberInterface
{
	/**
	 * @var \phpbb\lock\db
	 */
	private $cron_lock;

	/**
	 * @var \phpbb\cron\manager
	 */
	private $cron_manager;

	/**
	 * @var \phpbb\request\request_interface
	 */
	private $request;

	/**
	 * Constructor
	 *
	 * @param db 				$lock
	 * @param manager			$manager
	 * @param request_interface	$request
	 */
	public function __construct(db $lock, manager $manager, request_interface $request)
	{
		$this->cron_lock	= $lock;
		$this->cron_manager	= $manager;
		$this->request		= $request;
	}

	/**
	 * Runs the cron job after the response was sent
	 *
	 * @param PostResponseEvent	$event	The event
	 */
	public function on_kernel_terminate(PostResponseEvent $event)
	{
		$request = $event->getRequest();
		$controller_name = $request->get('_route');

		if ($controller_name !== 'phpbb_cron_run')
		{
			return;
		}

		$cron_type = $request->get('cron_type');

		if ($this->cron_lock->acquire())
		{
			$task = $this->cron_manager->find_task($cron_type);
			if ($task)
			{
				if ($task->is_parametrized())
				{
					$task->parse_parameters($this->request);
				}

				if ($task->is_ready())
				{
					$task->run();
				}

				$this->cron_lock->release();
			}
		}
	}

	/**
	 * {@inheritdoc}
	 */
	static public function getSubscribedEvents()
	{
		return array(
			KernelEvents::TERMINATE		=> 'on_kernel_terminate',
		);
	}
}