• Welcome to Simple Machines Community Forum. Please login or sign up.
October 18, 2021, 08:23:31 AM

News:

Join the Facebook Fan Page.


Rethink new scheduled tasks in 2.1

Started by Arantor, September 08, 2013, 12:49:52 PM

Previous topic - Next topic

Arantor

Most mods don't make new scheduled tasks - partly because it's a PITA to do so and partly because they just don't need to.

I have, on the other hand, made mods that added scheduled tasks and yes, it's a PITA. 2.1 makes this easier by having a hook to be called when initialising new scheduled tasks, but for the most part this seems like the wrong way to do it, doubly so because there are now two separate hooks for doing this stuff and to be honest you only need one.

Right now, there's a hook to include files to make scheduled tasks work, and a hook to extend management of scheduled hooks. The former hook is to ensure that the file will be loaded, the latter to ensure that a language file will be loaded appropriately (since that's about all it can really do)

I suggest rethinking this because it's not intuitive and juggling this opens the door to future improvements.

1. Don't have a separate hook for declaring new scheduled tasks, you don't need it. Just add a column to the DB for a file to load when the task is declared. Mod authors already have to add a task manually to the DB since there's no interface in - say - package-info.xml for it (as least, not that I've seen), so they might as well just add the filename for the right function while they're there.

2. There are already two 'kinds' of hooks, one for straight includes and one for includes plus functions. I suggest a new kind of hook, solely for the benefit of language files. There are other places where this would improve things (e.g. the help popup) - there's already a hook there, sure, which means you load a function whose purpose is to load a language file. Cut the nonsense and just have it able to load the language file more directly.

3. Assuming the first two are done, it really isn't particularly awkward to add new scheduled tasks via package-info.xml just to make it a little easier to manage. Something perhaps like:
<task runevery="1" runfreq="day" name="mytask" file="$sourcedir/MyMod/MyTasks.php" />

You get to cut down on hooks, you get to improve what hooks there are and mod authors have a little easier time of it without having to get into the database directly to add something.
No good deed goes unpunished
All helpful urges should be circumvented

Arantor

And if you're *really* going for it, you can always break up ScheduledTasks.php into small chunks by moving all the actual tasks out of the file and into their own classes and have an autoloader load them on demand.

For best results you'd switch out the current task column into a class_name column, plus a column for a filename to load; if you have mods they can use that whereas everything else can use the autoloader magic.

I've been playing with this, I ended up creating each class with two methods, taskInfo() and taskAction(), info returns the name and description, action actually does the deed. Seems to work pretty well and means you only load what you need to load.
No good deed goes unpunished
All helpful urges should be circumvented

Advertisement: