3

I have a cron job that calls a script that iterates through some items and submits them as posts to Facebook Graph API every minute. The issue is, each call takes a few seconds. If there is more than 10 posts to be sent to the API in a given minute, the script runs longer than a minute and then starts causing issues when the script starts running again at the next minute.

The general process is like this: 1. Each facebook profile posts every hour 2. Each of these profiles have a 'posting minute', which is the minute of the hour that they are posted at 3. A cron job runs every minute to see which profiles should be posted do, any given minute, and then posts to them

My question: Is it possible to continue the script immediately after calling the $facebook->api(...) method below, rather than waiting for it to complete before continuing? So that it can ensure to post to all profiles within the given minute, rather than potentially risk having too many profiles to post to and the script exceeding 60 seconds.

$profilesThatNeedToBePostedTo = getProfilesToPostTo(date(i));
foreach($profilesThatNeedToBePostedTo as $profile)
{
    $facebook->api($endPoint, 'POST', $postData); // $postData and $endPoint omitted for brevity
}

2 Answers2

0

what you are asking if in PHP you can do Asynchronous calls. There are some solutions here: Asynchronous PHP calls

Just to point there are not the most reliable options.

Is not limiting number of post to 9 each time reduce the times you have exceeded the 60 seconds. But what happens if the Facebook Graph is slow? what do you do?

Or run you code for 60 seconds and once the 60 seconds time is reached close the function. And let it run again the next time.

Community
  • 1
  • 1
Roninio
  • 1,761
  • 1
  • 17
  • 24
  • I'd need them to all be posted in the minute that they should. The database stores a posting_minute field (0 to 59) and each minute, the profiles for the current minute of the hour are retrieved, and then posted on. So for example, if it is 10:15AM and the script runs and posts 9 items that have a posting_minute of 15, and there is still 5 more to post, this won't work as the next minute will pull only posts that have been posted at 10:16AM. – Mark Gladstone May 05 '14 at 08:28
  • I would take all the posts needed to post and place it in a Queue. And than another cron would be responsible to post the the data to Facebook Graph. This is the safe way to go in such solutions. So if something goes wrong or, facebook is not answering. You know what you need to post based on what is in the Queue. – Roninio May 05 '14 at 08:48
0

I am not sure if I understand your question hundred percent clearly. You meant that you want to run the cronjob every minute. However, it takes longer than 1 minute to call facebook API. Do you mean that foreach loop may delay your cronjob? If so, I wonder if you did a right cronjob script, better to show your cronjob script. Because I think php script should not affect your sh script(cronjob)

Danni
  • 63
  • 1
  • 6