MyBB Community Forums
Tasks? How to use? - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Development (https://community.mybb.com/forum-68.html)
+---- Thread: Tasks? How to use? (/thread-223517.html)



Tasks? How to use? - itsmeJAY - 06-18-2019

Good afternoon, everyone,

how exactly can Tasks be used? Suppose I have a plugin with settings and want to check if a setting is set at regular intervals and then execute PHP code.

Do I have to associate the plugin with the task somehow? Can someone show me an example?  Angel


Thanks,
itsmeJAY


RE: Tasks? How to use? - Mipher - 06-19-2019

Informations about the task system and how to setup one: Task Manager - MyBB Documentation

Every task is connected to a file in the inc/tasks directory, which name is specified in the field "Task File". In this file you will specify what to do when your task is performed inside a function named task_filename, where filename is obv the name of the file connected to the task.

How to integrate it with a plugin? As you can see, to set up a task you just need to add a new task from the task manager (that is gonna insert a new row in the mybb_tasks table) and create a file connected to the task in the tasks directory. So, for integrating it with a plugin you just need to insert a new row in the mybb_tasks table during plugin installation with the specifics of your task, something like:

require_once MYBB_ROOT . '/inc/functions_task.php';

$new_task = [
	'title'			=> 'Task Name',
	'description'	=> 'Task Description.',
	'file'			=> 'filename',
	'minute'		=> '0',
	'hour'			=> '0',
	'day'			=> '*',
	'month'			=> '*',
	'weekday'		=> '*',
	'enabled'		=> '1',
	'logging'		=> '1',
];

$new_task['nextrun'] = fetch_next_run($new_task);

$db->insert_query('tasks', $new_task);
$cache->update_tasks();

And include in your plugin files a new file in the inc/tasks directory:

<?php

function task_filename($task)
{
	global $mybb;

	/*
	 * Your task operations
	 */

	add_task_log($task, "This plugin task successfully ran.");
}



RE: Tasks? How to use? - itsmeJAY - 06-19-2019

Thank you for this detailed explanation. I still have the following questions: Does the name of the function in the task file have to be defined in the plugin or can I call the function as I like? 

And why we need the Parameter $task in 

function task_filename($task)? 

To add the tasklog or why?

Thank you
itsmeJAY


RE: Tasks? How to use? - Mipher - 06-20-2019

(06-19-2019, 10:30 PM)itsmeJAY Wrote: Thank you for this detailed explanation. I still have the following questions: Does the name of the function in the task file have to be defined in the plugin or can I call the function as I like? 
As I said here:
(06-19-2019, 06:40 PM)Mipher Wrote: Every task is connected to a file in the inc/tasks directory, which name is specified in the field "Task File". In this file you will specify what to do when your task is performed inside a function named task_filename, where filename is obv the name of the file connected to the task.
So, the field "Task File" is the value with key file in the $new_task array that you define during your plugin installation. If you specify as value myplugin you need to create a file in the inc/tasks directory named myplugin.php with inside a function named task_myplugin. Since it is your first time, I advise you to create first the task from the task manager, then search your new task in the mybb_tasks table and finally copy your task specifications in the $new_task array.

(06-19-2019, 10:30 PM)itsmeJAY Wrote: And why we need the Parameter $task in 

function task_filename($task)? 

To add the tasklog or why?
In primis because the function is called by MyBB passing one argument: inc/functions_task.php#L90. But it will not give you any exception if you do not specify it, so, mainly yes, it is needed in order to call add_task_log() correctly. Also, keep in mind that $task contains all informations about your task, so it may return useful if you need to do any specific check (but I do not think you will need it for this).


RE: Tasks? How to use? - itsmeJAY - 06-20-2019

Thank you! You helped me a lot.


RE: Tasks? How to use? - gimbal - 08-15-2019

I could use a bit of help with this too. I've created a simple php script I would like to run daily, and I put it in a simple php file in the inc/tasks folder:

forum/inc/tasks/myscript.php

I then went to ACP > Tools > Task Manager > Add New Task and selected "myscript.php" as the task to run.

When I manually tell it to "run task now" it works, but I can't get the schedule or the logging to work... I tried to understand Mipher's note above, but I don't get it. Is there a simple way to have MyBB run my script on a schedule and log the result? Thanks in advance, sorry for the novice question.