Class: InstagramConnect::SyncMediaJob

Inherits:
ApplicationJob
  • Object
show all
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 =

like_count and comments_count come straight off the media node — no insights permission needed for the account's own posts. thumbnail_url is the poster frame for videos, whose media_url is a playable mp4.

Client::MEDIA_FIELDS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enqueue_throttled(account) ⇒ Object



25
26
27
28
29
30
# File 'app/jobs/instagram_connect/sync_media_job.rb', line 25

def self.enqueue_throttled()
  Rails.cache.fetch("instagram_connect:sync_media:#{.id}", expires_in: THROTTLE) do
    perform_later(.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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/jobs/instagram_connect/sync_media_job.rb', line 34

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

  started_at = Time.current
  result = fetch(, 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=#{.id}: #{result.error_message}")
  end

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