MyBB Community Forums

Full Version: Create an hourly task and save some html to a cache
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm trying to improve my TopPosts plugin to avoid running multiple queries by all users, so instead I want to create a task that would run this code in centralised way, save in a mybb cache, and then used when rendering the index page.

I got confused looking at other's plugins code, and I would really appreciate if someone would kindly be able to show some short example of a code that:

- Creates an hourly task
- Save something in a global cache
- how to read on my plugin what was saved by the task

If there is such examples already, please kindly point me to it.

Thanks
Eco

Actually I couldn't find any documentation on how to create tasks. is there any?


Answering my own questions:

ANSWER - CREATE A TASK

Answering myself, I found an example in MyAlerts plugin, and changed accordingly. I haven't tested yet, but for the record, this is what I need to get the task working:

1- The following should be added on the activate function of plugin:

function topposts_activate()
{
...
$taskExists = $db->simple_select('tasks', 'tid', 'file = \'topposts\'', array('limit' => '1'));
    if ($db->num_rows($taskExists) == 0) {
        require_once MYBB_ROOT.'/inc/functions_task.php';

        $myTask = array(
            'title'       => $lang->topposts_task_title,
            'file'        => 'topposts',
            'description' => $lang->topposts_task_description,
            'minute'      => 0,
            'hour'        => 1,
            'day'         => '*',
            'weekday'     => 1,
            'month'       => '*',
            'nextrun'     => TIME_NOW + 3600,
            'lastrun'     => 0,
            'enabled'     => 1,
            'logging'     => 1,
            'locked'      => 0,
        );

        $task_id = $db->insert_query('tasks', $myTask);
        $theTask = $db->fetch_array($db->simple_select('tasks', '*', 'tid = '.(int) $task_id, 1));
        $nextrun = fetch_next_run($theTask);
        $db->update_query('tasks', 'nextrun = '.$nextrun, 'tid = '.(int) $task_id);
        $plugins->run_hooks('admin_tools_tasks_add_commit');
        $cache->update_tasks();
    } else {
        require_once MYBB_ROOT.'/inc/functions_task.php';
        $theTask = $db->fetch_array($db->simple_select('tasks', '*', 'file = \'topposts\'', 1));
        $db->update_query('tasks', array('enabled' => 1, 'nextrun' => fetch_next_run($theTask)), 'file = \'topposts\'');
        $cache->update_tasks();
    }
...
}

2- create a file for the task inside /inc/tasks/ called topposts.php with the content:

<?php
if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function task_topposts($task)
{
    require_once MYBB_ROOT.'inc/plugins/topposts.php';
    global $mybb, $lang;

    if (!$lang->topposts) {
        $lang->load('topposts');
    }

    if (updateTopposts()) {
        add_task_log($task, $lang->topposts_task_update_topposts);
    } else {
        add_task_log($task, $lang->topposts_task_update_topposts);
    }
}

3- Obviously the updateTopposts() need to exist inside the plugin to do what you want it to do. In my case to generate the html and make it available to the index page hook later.

4- Then this on the deactivate function of my plugin:

function topposts_deactivate()
{
    global $Pl, $db;
    $db->update_query('tasks', array('enabled' => 0), 'file = \'topposts\'');
}

5- And this on the uninstall function of my plugin:

function topposts_uninstall()
{
    global $db, $lang;
    $db->delete_query('tasks', 'file = \'topposts\'');
}


ANSWER - HOW TO USE CACHE

This is pretty straightforward, although I had a bad time with a cache that wouldn't update on my live server.

1- First you need to get your content on the cache. This might be a JSON data, or even a piece of html:

function updateCachedContent()
{
        //...
	global $cache;
	$cache->update('my_cached_content', $cached_content);
	//...
}

2- Then at the time you need to show the data, you read it from the cache:

function getCachedContent()
{
	global $cache;
	$cached_content = $cache->read('my_cached_content');
	return $topposts_cached;
}
Thanks a lot this could be help to anyone uses a task to auto load or refresh cache and take it as a task, i have some idea on how it works because i made a plugin with this.
Updated with the bit about the cache.