Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Arantor on August 09, 2009, 03:30:45 PM

Title: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on August 09, 2009, 03:30:45 PM
Judging by the number of mods that don't use scheduled tasks in 2.0, it seems to be that there is a little bit of confusion on how to create them. So, here goes - with thanks to JBlaze for getting me started on it in the first place.

Any scheduled task requires 3 basic things: code to run, a description in the language files, and an entry in the database.

For the purpose of this, I'm actually going to talk you through a simplified version of my Auto Lock Old Topics (http://custom.simplemachines.org/mods/index.php?mod=2040) mod, where instead of locking threads configurably, we'll just say we lock every thread that hasn't been replied to in 30 days.

The very first thing we need to do is give our task an identifier. It doesn't have to be special but needs to be short and as unique as you can be. I used 'autoLock' for mine, so we'll continue to use that here to keep it straightforward.

Description

Let's start with the easiest thing: description. Each scheduled task has a name and a quick description, which you can see in the Scheduled Tasks part of the Admin area in 2.0. The name is also a link to configure when the script should run, and we don't want to get into modifying things we don't need to for it.

In fact, adding a description is nice and easy, you just have to add 2 lines to ManageScheduledTasks.english.php in the theme (for the budding mod authors out there, it's $languagedir/ManageScheduledTasks.english.php you want, plus any other language variations)

Here's where the identifier comes in. We create two language strings, scheduled_task_{identifier} and scheduled_task_desc_{identifier} and add them to $txt.

So to the bottom of ManageScheduledTasks.english.php, I would add:

$txt['scheduled_task_autoLock'] = 'Auto Lock Old Topics';
$txt['scheduled_task_desc_autoLock'] = 'Locks topics that have not been replied to in 30 days.';


And bippity-boppity-boop, that's that bit done.

Code to run

Having set up the description of the mod, we do actually need to write some code that would get run when our task is called for.

The key thing here is the name of the function, and location. It should live in Sources/ScheduledTasks.php (for you mod authors, $sourcedir/ScheduledTasks.php), and go by the name of scheduled_{identifier}. So with our autoLock identifier, we want something like:


function scheduled_autoLock()
{
global $modSettings, $sourcedir, $mbname, $txt, $smcFunc, $scripturl;

$timestamp = time() - (30 * 86400);
$query = 'UPDATE {db_prefix}topics, {db_prefix}messages
SET {db_prefix}topics.locked = 1
WHERE {db_prefix}topics.id_last_msg = {db_prefix}messages.id_msg
AND {db_prefix}messages.poster_time <= {int:timestamp}';

$result = $smcFunc['db_query']('',
$query,
array(
'timestamp' => $timestamp,
)
);

return true;
}


You only have to inherit what variables you actually need, though I tend to make sure all the variables I might need are handy, and the code simply needs to run, returning true if successful, and false if not. You can check out the fuller code in the mod I mentioned, which also does it on a per-board level.

So, with that we have the description for our task, and the code for it to run.

Database work

We do also have to do one last thing - tell SMF about the task that needs to be run. Code alone is not sufficient here for that. For the mod authors out there, this last bit would be managed via an install.php and uninstall.php script as part of your mod install. Again, the technique is shown in the master version of the Auto Lock Old Topics mod.

Adding it is a simple case of adding a new row to the scheduled_tasks table, perhaps smf_scheduled_tasks. The query you'll use with $smcFunc takes the form of:


INSERT INTO {db_prefix}scheduled_tasks (id_task, next_time, time_offset, time_regularity, time_unit, disabled, task)
VALUES (NULL, 0, 0, 1, "d", 0, "autoLock")


(Of course, if we're being proper, we should be using $smcFunc['db_insert'] instead of $smcFunc['db_query']. But this way you can see what the columns are and how they relate to each other.)

Just let's quickly step through the columns:

Once all of this is done, your scheduled task should be available to set what time you want it to run (since it will likely be different for every user)


Hopefully that's helped - if you have any questions please do ask.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: aED on December 08, 2009, 11:36:52 PM
Hi Arantor,

Sorry to bump this but is there  any way I can make a scheduled task to run every first day of the month?
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on December 09, 2009, 03:28:46 AM
No, you'd have to have it run every 4 weeks or every 30 days. Or when it runs, have it recalculate the next interval.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: aED on December 09, 2009, 07:06:52 AM
hehehe :P thanks I Just figured it out after posting my message:)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on December 09, 2009, 08:32:22 AM
What did you do in the end?
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: butchs on March 22, 2010, 10:52:32 PM
Thank you for this excellent write up!
8)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: treams on April 29, 2010, 04:06:05 PM
thanks for this.

i am using this guide and your auto purge mod as a basis for my first mod to delete the reference for MOVED topics to help keep the board i frequent a little cleaner.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on April 29, 2010, 04:06:45 PM
Or you could use the Redirection Topics mod I wrote a while ago to automate that process too ;)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: treams on April 29, 2010, 04:14:03 PM
ha well doesnt that beat all. i searched and searched the mod site for something like this and couldnt find what i wanted. i never would have thought to use "redirection" as a keyword though.

well ya saved me a ton of time. i guess i will have to think of something else to write.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on April 29, 2010, 04:15:10 PM
Well, the moved topics are redirection topics, they direct you to where you want to go, I also made it actually do a redirect as well.

Robbo_ now maintains the mod though.

Don't give up though, there's plenty of exciting mod possibilities :)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: butchs on April 29, 2010, 08:25:41 PM
I would assume that with some coding you can externally estimate then date and then insert new values into the database once a month?
:o
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on April 29, 2010, 08:33:53 PM
Sure you can. But it's easier if you don't have to keep track of such things.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: butchs on April 30, 2010, 06:08:01 PM
I agree.  The code will be a PITA.  :P
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on April 30, 2010, 06:20:16 PM
That's the thing, creating a scheduled task is, as above, basically a fire-and-forget situation, you define the function and matching language string, insert a row in a DB and you're basically done.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Mick. on May 05, 2010, 11:13:37 AM
Arantor, great write up brother.

I have a question and it may be related to aED'd reply #2.

Scheduled tasks is not possible twice a day huh?     I have a mod idea that i currently do manually.   It has to do with members currently online.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on May 05, 2010, 01:05:49 PM
Sure it is. Once every 12 hours - regularity 12, time unit h.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Mick. on May 05, 2010, 01:32:58 PM
Quote from: Arantor on May 05, 2010, 01:05:49 PM
Sure it is. Once every 12 hours - regularity 12, time unit h.

Excellent.

The idea is,......  see, i have the member currently online set to 60 minutes.  ...and, in the language file i changed: "Users active in past 60 minutes" to "Users online".... and changed in "features and option" the time threshold from 60 to 360.

Now it shows a lot more users online.   But late at night like most forums, activity is not great.  That's where the scheduled task comes in.  At whatever time set, say, midnight it will revert from 360 mins to 60 mins.   ....and maybe a clear cache with it.

Then in the morning say at 6am, it will revert from 60 mins to 360 mins again.

That's why asked if the schedule task can be a key player here.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on May 05, 2010, 01:34:37 PM
Sure, in that case you create two tasks, each to run daily.

Then you just set one to run at midnight, and one to run at 6am - since the time a task is run is configurable in the ACP (and isn't related to this tutorial directly)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Mick. on May 05, 2010, 01:44:33 PM
Quote from: Arantor on May 05, 2010, 01:34:37 PM
Sure, in that case you create two tasks, each to run daily.

Then you just set one to run at midnight, and one to run at 6am - since the time a task is run is configurable in the ACP (and isn't related to this tutorial directly)

I didnt mean to thread-jack ;)

Thanx Arantor, that cleared my questions.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on May 05, 2010, 01:45:45 PM
Well, it's relevant - how to use scheduled tasks in a slightly different way, it's all good :)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Zaine on August 27, 2011, 01:55:18 PM
Removed my last question and asking it in a better way :)

How should you handle if it your scheduled tasks errors?   I'm logging errors using log_error(); and returning false from the Scheduled task function but the interface still happily reports that the task completed successfully, even when logging a critical error.

I'm getting the errors in the error log, which is good, but I was expecting to see that interface warn me that the manual run failed.

Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: emanuele on November 09, 2011, 06:49:29 PM
Hi Zaine,

it's the first time for me looking into scheduled tasks, but as far as I can understand from the code it seems that if the scheduled function returns false the task should not be logged:
Code (from ScheduledTasks.php lines 124-129) Select
// Do the task...
$completed = call_user_func('scheduled_' . $row['task']);

// Log that we did it ;)
if ($completed)
{
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: DanCarroll on November 11, 2011, 07:41:34 AM
Here's a tip for anyone looking to run a task on a given day of the month such as the first.

Just schedule your task as a daily and do a check at the beginning to see if it is the first and then decide what code to run.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Iomega0318 on May 22, 2012, 02:51:17 AM
Can this be used to run a link, I have a cron job I would like SMF to handle and was wanting to schedule it for once a week. However the cron job has a link for it, and before you ask, my server won't let me run cron jobs normally..
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: live627 on May 22, 2012, 04:08:15 AM
No. No way other than the code shown in the OP.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: emanuele on May 22, 2012, 04:39:16 AM
fetch_web_data (http://support.simplemachines.org/function_db/index.php?action=view_function;id=586)? :P

Of course I mean create a function that call fetch_web_data.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Acans on August 07, 2013, 10:47:47 AM
Very useful tutorial. Perfect for updating my member count across multiple shared forums :) (Instead of pressing that damn recount button everyday :P)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: live627 on August 07, 2013, 11:09:22 PM
crontab

/end thread


(sub)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: DieGoO on August 12, 2013, 12:07:12 AM
I managed to create a scheduled task, but it seems like it isn't running automatically, or  at least, it doesn't show on the task log..

But if i run it manually it does show on the task log..

Did i miss something?
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on August 12, 2013, 01:08:19 AM
I don't know, what did you do exactly?
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: DieGoO on August 12, 2013, 08:10:41 PM
Nevermind  :P .. After reading through the topic again, i figured out why it wasn't working..
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: live627 on August 14, 2013, 04:24:22 AM
Quote from: Arantor on August 12, 2013, 01:08:19 AM
I don't know, what did you do exactly?
/me is reminded of The IT Crowd :P
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on August 14, 2013, 04:31:54 AM
You realise that the IT Crowd was practically a documentary rather than a comedy, right? ;)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: live627 on August 14, 2013, 04:57:43 AM
That explains why it was so boring.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: 4Kstore on November 07, 2013, 02:55:33 AM
Sorry for the bump, but I need to ask for save some time, there is no hook actually for this? SMF 2.0?
I read something http://www.simplemachines.org/community/index.php?topic=510873.0
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: live627 on November 07, 2013, 03:49:13 AM
I use integrate_pre_load...
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: Arantor on November 07, 2013, 10:42:55 AM
I wrote this years ago. Off the top of my head there is no hook in 2.0, the one in 2.1 is not ideal IMO.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: 4Kstore on November 07, 2013, 08:46:04 PM
Thanks for the responses..
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: PopUpSteve on March 21, 2014, 04:52:09 PM
Thank you, a very informative tutorial.
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: pirma on March 25, 2014, 06:57:19 AM
Very helpful article. Perfect for upgrading my new member count number across numerous contributed message boards.
(https://www.simplemachines.org/community/proxy.php?request=http%3A%2F%2Fportalpark.net%2F10204%2F11%2Fw.png&hash=0e6e204fa4bd208c4f28610d5979263d7216ca03)
Title: Re: How to create Scheduled Tasks in SMF 2.0
Post by: alexetgus on May 03, 2014, 08:28:30 AM
Great !
I was looking for is exactly this topic!

Thank you very much ! ;)


PS :
Sorry for my english if it errors exists.
I'm french and i speak french. Only french. :(