Ich bin auf Schienen und ich benutze Koala in einem Cron-Job, um alle Kommentare Facebook zu importieren.Ist ein Callback für Schleifen erforderlich, die API-Anfragen enthalten?
Ist es in Ordnung, eine for
Schleife zu verwenden, wo jedes Mal ich eine request
mache und die response
speichere? Oder gibt es ein Risiko, für das die for
Neustart, bevor ich eine response
von Facebook bekomme alles durcheinander bringt?
Mit anderen Worten: Wartet die Schleife auf die Antwort oder brauche ich eine Rückruffunktion? Hier
ist die Schleife:
def self.import_comments
# Access Facebook API
facebook = Feed.get_facebook_access
# Run 190 queries per cron job
for i in 1..190
id_of_latest_feed = Feed.get_latest['fb_id']
id_of_latest_feed_checked = Option.get_feed_needle
# Check if there are more recent feeds than the latest checked
if id_of_latest_feed != id_of_latest_feed_checked
# Get the facebook id of the feed which comes after the latest checked
latest_feed_checked = Feed.where(fb_id: id_of_latest_feed_checked).first
this_date = latest_feed_checked['fb_updated_time']
feed_to_check = Feed.get_older_than(this_date)
unless feed_to_check.nil?
# Get the ID of the feed to check
fb_id = feed_to_check['fb_id']
# Update needle
Option.update_feed_needle_to(fb_id)
# -------- REQUEST! --------- #
# Get comments from Facebook
@comments = facebook.get_object("#{ fb_id }/comments?filter=stream")
# Save each comment
@comments.each do |comment|
if Comment.exists?(fb_id: comment['id'])
# don't do anyhting
else
# save the comment
end
end
end
end
end
end
Ich habe den Code ausgeführt und hatte bis jetzt kein Problem, also denke ich, dass Sie Recht haben –