PDA

View Full Version : Scheduled Task - Run Every 5 Seconds


Mko
12-16-2014, 03:36 PM
I've created a Scheduled Task that will submit a request to an API and store the value returned in a table in my database.
I'm interested in having this Scheduled Task run every 5 seconds, yet in the vB3 AdminCP, the only fields I see are Day of the Week, Day of the Month, Hour, and Minute. However, Minute has 6 fields attributed to it, which I am also confused about.

How would I get my Scheduled Task to run every 5 seconds through vBulletin?

Thanks,
Mark

kh99
12-16-2014, 03:45 PM
There are 6 "minute" fields because you can schedule a task to run up to 6 times per hour at certain minutes of the hour. If you choose '*' then it will run once per minute, but that's the most often you can schedule a task to run (unless you modify the code somehow).

I'm assuming that you're using the returned value somewhere, maybe you could do something like before it's displayed check to see if it's more than 5 seconds old, and if it is, request it before displaying it. But then I guess you'd also need a way to keep it from constantly being requested by multiple threads, especially if you have a very busy site,

Mko
12-16-2014, 03:54 PM
There are 6 "minute" fields because you can schedule a task to run up to 6 times per hour at certain minutes of the hour. If you choose '*' then it will run once per minute, but that's the most often you can schedule a task to run (unless you modify the code somehow).

I'm assuming that you're using the returned value somewhere, maybe you could do something like before it's displayed check to see if it's more than 5 seconds old, and if it is, request it before displaying it. But then I guess you'd also need a way to keep it from constantly being requested by multiple threads, especially if you have a very busy site,
I see. Thanks for the suggestion!

Would the following settings be what I'm after if I wish to have my Task run every minute?
https://vborg.vbsupport.ru/external/2014/12/20.png

kh99
12-16-2014, 03:59 PM
Yes, I believe that would do it.

But in case you're not aware, the tasks run when users view your forum pages, so the timing it approximate, and it will only run once per minute if you have enough traffic to your site.

Lynne
12-16-2014, 04:00 PM
That should do it. Be aware that the Scheduled Tasks only run if you have users on your site. The tasks will backup if you don't have people clicking on your site.

Mko
12-16-2014, 04:24 PM
I see! Thanks for the information.

As a follow-up question...
I have a custom bridge I developed with vB. Since both you reminded me how a user needs to be viewing the forum in order for Scheduled Tasks to execute, I decided to generate $cronimage in my site-wide header (not present on my directory with my vBulletin files) so that Scheduled Tasks hopefully would execute even if nobody is viewing the Forum Index.

Here is what I included (community/ is the directory where I have my vBulletin):
if ($vbulletin->cron <= TIMENOW)
{
$cronimage = '<img src="' . create_full_url('community/cron.php?' . $vbulletin->session->vars['sessionurl'] . 'rand=' . TIMENOW) . '" alt="" width="0" height="0" border="0" style="display: none;" />';
}
else
{
$cronimage = '';
}
echo $cronimage;It outputs correctly, but I just want to double check that this is the proper way of "inducing" Scheduled Tasks to run from an external page.

Thanks again!

kh99
12-16-2014, 05:11 PM
It seems right, but to be honest I don't know for sure.

ozzy47
12-16-2014, 09:37 PM
Yeah it looks ok to me also. But I don't understand, what is it you need to run so often?

Mko
12-16-2014, 10:19 PM
Yeah it looks ok to me also. But I don't understand, what is it you need to run so often?
I was initially going to call the API to fetch the amount of players logged in, but I opted to do it using a table in my db and not using a Scheduled Task.

Anyways, good to see that the code I added in the header looks OK. Thanks for all the help! :)

ozzy47
12-16-2014, 10:23 PM
Gotcha. Glad to see you found a way to do what you wanted though. :)

Paul M
12-17-2014, 12:15 AM
so that Scheduled Tasks hopefully would execute even if nobody is viewing the Forum Index.
The default vb cron does not rely on someone viewing the forum index, it is part of the footer, so runs off any page.

ozzy47
12-17-2014, 10:24 AM
This is true, unless of course it is a custom page, and you did not include the footer on it. :)

Easy5s.net
06-27-2015, 07:03 AM
Please let me know, I've set up a cron to run every minute as above. If no one is accessing the site, the cron does not run?

kh99
06-27-2015, 09:16 AM
Please let me know, I've set up a cron to run every minute as above. If no one is accessing the site, the cron does not run?

That's correct, it won't run if no one is accessing the site. There has been mention before of using an actual cron job to run the scheduled tasks. If you search here and at vbulletin.com you should find something about it. I can't remember the details but it might have been as easy as setting it up to run the cron.php script.

Easy5s.net
06-27-2015, 12:10 PM
I have search more one 1h in vb.com but not find :(

TheLastSuperman
06-27-2015, 05:27 PM
That's correct, it won't run if no one is accessing the site. There has been mention before of using an actual cron job to run the scheduled tasks. If you search here and at vbulletin.com you should find something about it. I can't remember the details but it might have been as easy as setting it up to run the cron.php script.

I have search more one 1h in vb.com but not find :(

I believe Kevin (kh99) meant to convey that unless your site has actual traffic 24/7/365 the cron job may or may not always run every minute (or 5/10/15/etc/etc) FROM vBulletin IF no traffic is on the site. vBulletin runs cron jobs based on activity, so if no visitors are on your site the cron from vBulletin will not run every minute.... now if they login and stay 15mins or if they login then leave (session timeout will show them and keep cron going for at least 15-30mins based on your setting) the vB cron jobs will only run while the site thinks 1 or more people are actually online (members or guests).

Try as Kevin suggested and setup an actual cron in cpanel versus vBulletin, when you set it up to run from cpanel it will run every minute whether your site has someone on it or not UNLESS the server restarts or has an issue but generally should not.

https://documentation.cpanel.net/display/ALD/Cron+Jobs

Easy5s.net
06-28-2015, 09:42 AM
Cron can run when google bot visit?

TheLastSuperman
06-28-2015, 05:06 PM
Cron can run when google bot visit?

Yes! Despite only being a bot its still an "unregistered" user to the system whether it knew it was a bot or not it still knew someone/something is on the site so cron will run then.

There is no guarantee on it running from vBulletin cron unless activity is ALWAYS present on the site, this is why we mentioned running cron from cpanel.