J
J
Jamy_Iler2015-10-12 16:45:23
PHP
Jamy_Iler, 2015-10-12 16:45:23

How to competently organize a script call every N seconds?

There is a script whose task is to receive new messages on the VK page every N seconds using the getDialogs method.
There is no cron on my server, so I use external sites, one of them is cron-job.org. I call this script of mine once a minute.

set_time_limit(59);
require_once("start_func.php");

//ждём когда последний запущенный скрипт закончит работу
$time_shift = 0;
while(file_get_contents("cron.txt")!=0 & $time_shift<59)
{
  sleep(1);
  $time_shift++;
}

//говорим всем о том что запускаем скрипт
file_put_contents("cron.txt", 1);
$interval = 3;
$start_time = time();

//выполняем скрипт $i раз
while($time_shift<57 & time()-$start_time<57)
{
  start_func(); //Делаем запрос к ВК АПИ
  sleep($interval);
}

//говорим всем о том, что текущий скрипт закончил выполнение
file_put_contents("cron.txt", 0);

Often, when making requests to VK, there are incomprehensible delays of 10-180 seconds and the site that called the script sends me an email with an error. After 100 errors it stops calling my script. Error: Execution timeout (I don't want to increase the response time for cron-job).
How can I build an algorithm so that its execution time does not exceed 60 seconds and the query function is called once every N seconds?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Алексей Уколов, 2015-10-12
@Jamy_Iler

while(file_get_contents("cron.txt")!=0 & $time_shift<59)
...
while($time_shift<57 & time()-$start_time<57)

Скорее всего, дело в том, что вы в условии амперсанд забыли - их должно быть два.

Алексей Скобкин, 2015-10-12
@skobkin

Можно вот таким способом.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question