Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Vladimir Samoilov on April 23, 2016, 02:48:22 AM

Title: $smcFunc doesn't work after sleep() function
Post by: Vladimir Samoilov on April 23, 2016, 02:48:22 AM
If I call $smcFunc['db_query'] after sleep(5) it works perfectly.
But if I call $smcFunc['db_query'] after sleep(60) it doesn't work with error message "database is busy".

Can anybody explaine how this issue may be resolved?

Thanks.
Title: Re: $smcFunc doesn't work after sleep() function
Post by: Arantor on April 23, 2016, 05:51:21 AM
Don't call it with sleep(60), essentially. The database connection drops after a period of time if it is not used, and waiting for a full minute causes it to timeout, as it should.

What are you doing where a full minute of wait is intentional?
Title: Re: $smcFunc doesn't work after sleep() function
Post by: Vladimir Samoilov on April 23, 2016, 07:39:09 AM
I am uploading the pictures from smf to some social network using api. So I want to each picture was sending over some time interval (from 1 second to several minutes). And from time to time I am sending query to the database for checking state of this proccess. For example, user can write state "stop" or "pause" to database and I need read this state in the my loop.

I use the following solution

while (.....)
{

sleep(60);

.....some code for upload picture......

if ($needcheck)
{
$request = $smcFunc['db_query']('', '
SELECT state
FROM {db_prefix}spmod_task
WHERE taskid = {int:ti}
LIMIT 1',
array(
'ti' => 0,
)
}

}


Thank you for help.
Title: Re: $smcFunc doesn't work after sleep() function
Post by: nend on April 23, 2016, 08:19:03 AM
Have you considered using cron jobs to achieve this?

If it is a function that needs user interaction,  then use Ajax style request.

Also a interesting tidbit, if you go the Ajax route be sure you put the time in there correctly, a week or two ago I missed a extra 0 and flooded SMF with request. I was pleasantly surprised when site wasn't reachable, the forum banned my IP in the flood control table.