PDA

View Full Version : Run scheduled task from plugin?


JamesAB
04-27-2015, 10:52 AM
I'm working on a plugin that needs to call for a scheduled task to run at that moment, without waiting for the next scheduled task run time.

Is there a way to force a scheduled task to run from inside the plugin code?

Thanks,
James

kh99
04-27-2015, 11:14 AM
I haven't tested it, but looking at the code in admincp/cronadmin.php for running a scheduled task, I think this might work:

if ($nextitem = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "cron WHERE cronid = " . $cronid))
{
ignore_user_abort(1);
@set_time_limit(0);

require_once(DIR . '/includes/functions_cron.php');
include_once(DIR . '/' . $nextitem['filename']);
}


where $cronid would have to be set to the id of the task before the above code is executed.

I don't know, you may or may not want the ignore_user_abort and/or the set_time_limit calls in your plugin.

JamesAB
04-27-2015, 12:38 PM
Thanks for your help.
I got the scheduled task ID from the Scheduled Task manager:
Scheduled Task: Process FFMPEG queue (id: 31)

I'm putting this code in the attachdata_postsave hook, so I tried changing it to this:
if ($nextitem = $this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "cron WHERE cronid = 31"))
{
ignore_user_abort(1);
@set_time_limit(0);

require_once(DIR . '/includes/functions_cron.php');
include_once(DIR . '/' . $nextitem['filename']);
}

Hoever, its producing this error now:
Fatal error: Call to a member function query_first() on a non-object in /home/forum321/public_html/includes/class_dm_attachment.php(659) : eval()'d code on line 99

Any help would be appreciated.

kh99
04-27-2015, 12:44 PM
Hmm...seems like that should be right. You could try
global $vbulletin;
if ($nextitem = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX . "cron WHERE cronid = 31"))
{
// etc


but I don't see why $this->registry wouldn't work.

JamesAB
04-27-2015, 01:00 PM
Thank you very much:)

That change appears to make it work as intended. My working code is now:
global $vbulletin;
if ($nextitem = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX . "cron WHERE cronid = 31"))
{
ignore_user_abort(1);
@set_time_limit(0);

require_once(DIR . '/includes/functions_cron.php');
include_once(DIR . '/' . $nextitem['filename']);
}

Is there any way to suppress the output from my scheduled task when it's called from the attachdata_postsave hook, but allow it when the scheduled task is manually run from the AdminCP?

For example, I'm using echo commands in the scheduled task that are nice to show the progress in the AdminCP, but I'd like to hide them from the Manage Attachments window when attachdata_postsave is called from there.

Currently all the echo outup from the scheduled task shows up at the top of the Manage Attachments window after the attachdata_postsave hook is called and processes the scheduled task.

Thanks again,
James

kh99
04-27-2015, 01:04 PM
You can check for VB_AREA == 'AdminCP' and only echo if that's true.

JamesAB
04-27-2015, 01:13 PM
That works perfectly.
Thanks for all your help.