Skip to content

Commit

Permalink
! Let's make background tasks actually run in the background. This ca…
Browse files Browse the repository at this point in the history
…n 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]>
  • Loading branch information
Arantor committed Jan 24, 2014
1 parent b91c7e0 commit 85a770b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/Load.php
Original file line number Diff line number Diff line change
Expand Up @@ -1885,10 +1885,22 @@ function smfAutoTask()
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']))
{
Expand Down
12 changes: 11 additions & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +65,15 @@
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');
Expand Down Expand Up @@ -107,6 +116,7 @@
);
}
}
obExit_cron();
exit;

// The heart of this cron handler...
Expand Down

0 comments on commit 85a770b

Please sign in to comment.