Class: InstagramConnect::SyncMediaJob
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- InstagramConnect::SyncMediaJob
- Defined in:
- app/jobs/instagram_connect/sync_media_job.rb
Overview
Mirrors the account's posts — every post, from any app, not just ones published through this gem.
The comments webhook names only a media id, so without this a host shows conversations grouped under bare 17-digit numbers. And Instagram sends no webhook when a post is deleted, so absence from the account's own COMPLETED listing is the only deletion signal there is: a vanished post is stamped removed_from_instagram_at, never destroyed. A partial or failed walk marks nothing — absence from an incomplete listing proves nothing.
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 inbox enqueueing it once per page view.
10.minutes
- MEDIA_FIELDS =
"id,caption,media_type,media_url,permalink,timestamp".freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#perform(account_id, full: false) ⇒ Object
full: walks the entire history with cursors and, having seen it all, marks the known posts the listing no longer contains.
Class Method Details
.enqueue_throttled(account) ⇒ Object
22 23 24 25 26 27 |
# File 'app/jobs/instagram_connect/sync_media_job.rb', line 22 def self.enqueue_throttled(account) Rails.cache.fetch("instagram_connect:sync_media:#{account.id}", expires_in: THROTTLE) do perform_later(account.id) true end end |
Instance Method Details
#perform(account_id, full: false) ⇒ Object
full: walks the entire history with cursors and, having seen it all, marks the known posts the listing no longer contains.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/jobs/instagram_connect/sync_media_job.rb', line 31 def perform(account_id, full: false) account = Account.find_by(id: account_id) return if account.nil? || !account.active? started_at = Time.current result = fetch(account, full: full) unless result.success? # A sync that fails must say so — the silent version of this class of # job has already cost a production afternoon. return logger.error("[instagram_connect] media sync failed for " \ "account=#{account.id}: #{result.}") end Array(result.data["data"]).each { |item| upsert(account, item) } mark_removed(account, started_at) if full end |