Class: InstagramConnect::SyncStoriesJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/instagram_connect/sync_stories_job.rb

Overview

Mirrors the account's stories. Meta's /stories edge returns only the stories still inside their 24 hours, and sends no webhook when one expires — so this walk upserts what it sees and marks nothing: a story that stops appearing has simply expired, which the row's posted_at already tells the host. Expired stories keep their rows forever, same never-delete rule as posts; their CDN URLs die with the story.

Safe to re-run: MediaItem.record upserts on (account, ig_media_id).

Constant Summary collapse

THROTTLE =

Hosts enqueue this from screens; the cache key stops a busy screen enqueueing it once per page view.

10.minutes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue_throttled(account) ⇒ Object



17
18
19
20
21
22
# File 'app/jobs/instagram_connect/sync_stories_job.rb', line 17

def self.enqueue_throttled()
  Rails.cache.fetch("instagram_connect:sync_stories:#{.id}", expires_in: THROTTLE) do
    perform_later(.id)
    true
  end
end

Instance Method Details

#perform(account_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/jobs/instagram_connect/sync_stories_job.rb', line 24

def perform()
   = Account.find_by(id: )
  return if .nil? || !.active?

  result = .client.list_stories
  unless result.success?
    return logger.error("[instagram_connect] stories sync failed for " \
                        "account=#{.id}: #{result.error_message}")
  end

  Array(result.data["data"]).each { |item| upsert(, item) }
end