Browse Source

! Let's make background tasks actually run in the background. This can still be improved, though, to try and avoid some of the concurrent requests but we'll get to that.

Signed-off-by: Peter Spicer <[email protected]>
Peter Spicer 10 years ago
parent
commit
85a770b778
2 changed files with 24 additions and 2 deletions
  1. 13 1
      Sources/Load.php
  2. 11 1
      cron.php

+ 13 - 1
Sources/Load.php

@@ -1885,10 +1885,22 @@ function loadTheme($id_theme = 0, $initialize = true)
 			tempImage.src = smf_scripturl + "?scheduled=' . $type . ';ts=' . $ts . '";
 		}
 		window.setTimeout("smfAutoTask();", 1);');
-
 		}
 	}
 
+	// And we should probably trigger the cron too.
+	if (empty($modSettings['cron_is_real_cron']))
+	{
+		$ts = time();
+		$ts -= $ts % 15;
+		addInlineJavaScript('
+	function triggerCron() {
+		var tempImage = new Image();
+		tempImage.src = ' . JavaScriptEscape($boardurl) . ' + "/cron.php?ts=' . $ts . '";
+	}
+	window.setTimeout(triggerCron, 1);', true);
+	}
+
 	// Any files to include at this point?
 	if (!empty($modSettings['integrate_theme_include']))
 	{

+ 11 - 1
cron.php

@@ -21,7 +21,7 @@
  */
 
 define('SMF', 'BACKGROUND');
-define('FROM_CLI', !isset($_SERVER['REQUEST_METHOD']));
+define('FROM_CLI', empty($_SERVER['REQUEST_METHOD']));
 define('WIRELESS', false);
 
 // This one setting is worth bearing in mind. If you are running this from proper cron, make sure you
@@ -65,6 +65,15 @@ if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
 if (file_exists($cachedir . '/cron.lock'))
 	obExit_cron();
 
+// Before we go any further, if this is not a CLI request, we need to do some checking.
+if (!FROM_CLI)
+{
+	// We will clean up $_GET shortly. But we want to this ASAP.
+	$ts = isset($_GET['ts']) ? (int) $_GET['ts'] : 0;
+	if ($ts <= 0 || $ts % 15 != 0 || time() - $ts < 0 || time() - $ts > 20)
+		obExit_cron();
+}
+
 // Load the most important includes. In general, a background should be loading its own dependencies.
 require_once($sourcedir . '/Errors.php');
 require_once($sourcedir . '/Load.php');
@@ -107,6 +116,7 @@ while ($task_details = fetch_task())
 		);
 	}
 }
+obExit_cron();
 exit;
 
 // The heart of this cron handler...