Background Tasks

A background task is a small piece of PHP which is run at a given maximum rate, the background task can perform operations that take a long time without impacting on the visitors experience. Each background task as a small amount of storage made available to it this storage can be accessed by snippets.

When the CMS is first installed it uses a pseudo cron system, that is it relies on visitors to the site to trigger cron jobs in the background, however for best results you should disable the pseudo cron and use the real system cron, for information on this go to Settings then under clearFusionCMS select Cron and follow the instructions there.

Creating a Background Task & Snippet

The following example is going to create a background task that fetches the latest news from the BBC and draws it as part of the site.

From the dashboard select Elements and click New Background Task, then in the Task Name put ‘newsfeed’ and the following should go in the Task Snippet (PHP):

$client = new flRssClient(clearFusionCMS::getCache());
if($channel = $client->fetch('', 5, false, 5)) {
	if(count($channel)) {
		$s = '<ul>';
		foreach($channel as $item) {
			$date = new flDate();
			$date = $date->timeSpan($item->pubDate);
			$s .= '<li>' . $item->title . '<br />' . $item->description . "<br /><span>$date[offset] $date[units] ago</span></li>\n";
		$s .= "</ul>\n";
		$dataStore->d = $s;

Before saving the task select 1 Day from the Maximum Rate to Execute Task at then save the task snippet. Finally click Run Task Now to force the task to run once to collect the latest news from the RSS feed.

Next create a task called ‘showtNews’ with the following:

echo clearFusionCMS::getTaskData('newsfeed')->d;

Place the snippet in a document using [[!showNews]], save the document and view the public document. The task will collect data from the RSS feed while the snippet will show the results, there’s no pauses while the news is read as it’s done in the background.