vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Run scheduled task from plugin? (https://vborg.vbsupport.ru/showthread.php?t=318442)

JamesAB 04-27-2015 10:52 AM

Run scheduled task from plugin?
 
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:
PHP Code:

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:
Code:

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:
Code:

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
PHP Code:

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:
Code:

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.


All times are GMT. The time now is 05:38 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01106 seconds
  • Memory Usage 1,728KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete