about summary refs log tree commit diff
path: root/app/services/activitypub/process_collection_items_service.rb
blob: 9c30d81e98586f81b632390b2744654102f273f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

class ActivityPub::ProcessCollectionItemsService < BaseService
  def call(account_id, on_behalf_of)
    RedisLock.acquire(lock_options(account_id)) do |lock|
      if lock.acquired?
        CollectionItem.unprocessed.where(account_id: account_id).find_each do |item|
          # Avoid failing servers holding up the rest of the queue.
          next if item.retries.positive? && rand(3).positive?

          begin
            FetchRemoteStatusService.new.call(item.uri, nil, on_behalf_of)
          rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
            nil
          rescue HTTP::TimeoutError
            item.increment!(:retries)
          end

          item.update!(processed: true) if item.retries.zero? || item.retries > 4
        end
      end
    end
  end

  private

  def lock_options(account_id)
    { redis: Redis.current, key: "process_collection_items:#{account_id}" }
  end
end