Class: ActiveJob::GoogleCloudPubsub::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/activejob_google_cloud_pubsub/worker.rb

Constant Summary collapse

MAX_DEADLINE =
10.minutes

Instance Method Summary collapse

Constructor Details

#initialize(queue: 'default', pubsub: Google::Cloud::Pubsub.new(timeout: 60), logger: Logger.new($stdout)) ⇒ Worker

Returns a new instance of Worker.



15
16
17
18
19
# File 'lib/activejob_google_cloud_pubsub/worker.rb', line 15

def initialize(queue: 'default', pubsub: Google::Cloud::Pubsub.new(timeout: 60), logger: Logger.new($stdout))
  @queue_name  = queue
  @pubsub      = pubsub
  @logger      = logger
end

Instance Method Details

#ensure_subscriptionObject



36
37
38
39
40
# File 'lib/activejob_google_cloud_pubsub/worker.rb', line 36

def ensure_subscription
  @pubsub.subscription_for @queue_name

  nil
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/activejob_google_cloud_pubsub/worker.rb', line 21

def run
  subscriber = @pubsub.subscription_for(@queue_name).listen(streams: 1, threads: { callback: 1 }) do |message|
    @logger&.info "Message(#{message.message_id}) was received."
    process message
  end

  subscriber.on_error do |error|
    @logger&.error(error)
  end

  subscriber.start

  sleep
end