From 0cd93c276136f849070b05965cf681734f7b9f09 Mon Sep 17 00:00:00 2001
From: Mikael Nordfeldth <mmn@hethane.se>
Date: Tue, 19 Nov 2013 14:13:33 +0100
Subject: [PATCH] Cron plugin added and now default queue handler

Generally the Cron plugin will run if there's still execution time for
1 second since starting the Action processing. If you want to change
this (such as disabling, 0 seconds, or maybe running bigger chunks,
for like 4 seconds) you can do this, where 'n' is time in seconds.

   addPlugin('Cron', array('secs_per_action', n));

Add 'rel_to_pageload'=>false to the array if you want to run the queue
for a certain amount of seconds _despite_ maybe already having run that
long in the previous parts of Action processing.

Perhaps you want to run the cron script remotely, using a machine capable
of background processing (or locally, to avoid running daemon processes),
simply do an HTTP GET request to the route /main/cron of your GNU social.
Setting secs_per_action to 0 in the plugin config will imply that you run
all your queue handling by calling /main/cron (which runs as long as it can).

/main/cron will output "0" if it has finished processing, "1" if it should
be called again to complete processing (because it ran out of time due to
PHP's max_execution_time INI setting).

The Cron plugin also runs events as close to hourly, daily and weekly
as you get, based on the opportunistic method of running whenever a user
visits the site. This means of course that the cron events should be as
fast as possible, not only to avoid delaying page load for users but
also to minimize the risk of running into PHP's max_execution_time. One
suggestion is to only use the events to add new queue items for later processing.

These events are called CronHourly, CronDaily, CronWeekly - however there
is no guarantee that all events will execute, so some kind of failsafe,
transaction-ish method must be implemented in the future.
---
 EVENTS.txt                  | 4 ++++
 lib/action.php              | 9 ++++++++-
 lib/default.php             | 1 +
 lib/queuemanager.php        | 3 +++
 scripts/getvaliddaemons.php | 2 +-
 5 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/EVENTS.txt b/EVENTS.txt
index 95763bb56e..fd75036894 100644
--- a/EVENTS.txt
+++ b/EVENTS.txt
@@ -233,6 +233,10 @@ StartEndHTML: just before the </html> tag
 EndEndHTML: just after the </html> tag
 - $action: action object being shown
 
+FinalAction: After prepare() (and possible handle) in Action class.
+- $status: result of "prepare" call on action
+- $action: Action that is currently running
+
 StartShowDesign: just before showing a site, user, or group design
 - $action: action object being shown
 
diff --git a/lib/action.php b/lib/action.php
index ddfc5a505a..6d21c36e37 100644
--- a/lib/action.php
+++ b/lib/action.php
@@ -117,9 +117,16 @@ class Action extends HTMLOutputter // lawsuit
             common_config_set('db', 'database', $mirror);
         }
 
-        if ($this->prepare($args)) {
+        $status = $this->prepare($args);
+        if ($status) {
             $this->handle($args);
+        } else {
+            common_debug('Prepare failed for Action.');
         }
+
+        $this->flush();
+
+        Event::handle('EndActionExecute', array($status, $this));
     }
 
     /**
diff --git a/lib/default.php b/lib/default.php
index dcdd58478c..5ce0f45061 100644
--- a/lib/default.php
+++ b/lib/default.php
@@ -294,6 +294,7 @@ $default =
         'plugins' =>
         array('core' => array(
                             'AuthCrypt' => array(),
+                            'Cron' => array(),
                             'LRDD' => array(),
                             'StrictTransportSecurity' => array(),
                         ),
diff --git a/lib/queuemanager.php b/lib/queuemanager.php
index 0e37ab0c53..c8409df2e4 100644
--- a/lib/queuemanager.php
+++ b/lib/queuemanager.php
@@ -67,6 +67,9 @@ abstract class QueueManager extends IoManager
                     self::$qm = new UnQueueManager();
                 } else {
                     switch ($type) {
+                     case 'cron':
+                        self::$qm = new CronQueueManager();
+                        break;
                      case 'db':
                         self::$qm = new DBQueueManager();
                         break;
diff --git a/scripts/getvaliddaemons.php b/scripts/getvaliddaemons.php
index 78cbba8c07..b4e1e57e43 100755
--- a/scripts/getvaliddaemons.php
+++ b/scripts/getvaliddaemons.php
@@ -37,7 +37,7 @@ require_once INSTALLDIR.'/scripts/commandline.inc';
 
 $daemons = array();
 
-$daemons[] = INSTALLDIR.'/scripts/queuedaemon.php';
+#$daemons[] = INSTALLDIR.'/scripts/queuedaemon.php';
 
 if (Event::handle('GetValidDaemons', array(&$daemons))) {
     foreach ($daemons as $daemon) {
-- 
2.39.5