activejob-google_cloud_pubsub 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7e1151a90370ecf792eae87e6daf9fd28e985de3
4
- data.tar.gz: 50b07a891b460d56c44fa1e60ed08b31ddfdd354
3
+ metadata.gz: 2e759c958bd41ca18bb5d4cc514c5586ceba3ea8
4
+ data.tar.gz: 213be00c966bd8c92c2a22a7c96dec0ad9a3ff9c
5
5
  SHA512:
6
- metadata.gz: 32504d3cd91e414cd6df30b90c7c5b16035a1bda30f2dfe48540adb22ba377fdfea0d867347c793fd2ed717f7e1e224cd9afd68cd3a9649ab7ca63b2bdf30e01
7
- data.tar.gz: 4f249b0da43e0212f5ec16ed3df3c31eefdad9fa91045b0c84938fb6ab1fba0c3ba6ae16a4c9a8ef48b39e1f90fbab9af60f7f20270a6bc777bacd3317ccb572
6
+ metadata.gz: 21a981c551c6a78180b3de7eff2fafaaac555b134ab50d9da63fe8219c84509635ed0dd09986480922fe46a82716cbccf657b543484f7d83cffe4683b6e98a25
7
+ data.tar.gz: be921aa73fdcfdca65a985897c9a8f9aa92b860f8254b0d9418deb6530e3e6f824e8a5fd446e28659dbaf6b5412f25bb79aa8acec63e90260f880b921aaedac7
data/README.md CHANGED
@@ -62,6 +62,9 @@ When passing options to the adapter, you need to create the object instead of a
62
62
 
63
63
  ``` ruby
64
64
  Rails.application.config.active_job.queue_adapter = ActiveJob::GoogleCloudPubsub::Adapter.new(
65
+ async: false,
66
+ logger: Rails.logger,
67
+
65
68
  pubsub: Google::Cloud::Pubsub.new(
66
69
  project: 'MY-PROJECT-ID',
67
70
  keyfile: 'path/to/keyfile.json'
@@ -69,7 +72,23 @@ Rails.application.config.active_job.queue_adapter = ActiveJob::GoogleCloudPubsub
69
72
  )
70
73
  ```
71
74
 
72
- Please see [`Google::Cloud::Pubsub.new`](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-pubsub/master/google/cloud/pubsub?method=new-class) for details.
75
+ #### `async`
76
+
77
+ Whether to publish messages asynchronously.
78
+
79
+ Default: `true`
80
+
81
+ #### `logger`
82
+
83
+ The logger that outputs a message publishing error. Specify `nil` to disable logging.
84
+
85
+ Default: `Logger.new($stdout)`
86
+
87
+ #### `pubsub`
88
+
89
+ The instance of `Google::Cloud::Pubsub::Project`. Please see [`Google::Cloud::Pubsub.new`](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-pubsub/master/google/cloud/pubsub?method=new-class) for details.
90
+
91
+ Default: `Google::Cloud::Pubsub.new`
73
92
 
74
93
  ### Worker
75
94
 
@@ -1,18 +1,26 @@
1
1
  require 'activejob-google_cloud_pubsub/pubsub_extension'
2
+ require 'concurrent'
2
3
  require 'google/cloud/pubsub'
3
4
  require 'json'
5
+ require 'logger'
4
6
 
5
7
  module ActiveJob
6
8
  module GoogleCloudPubsub
7
9
  class Adapter
8
10
  using PubsubExtension
9
11
 
10
- def initialize(pubsub: Google::Cloud::Pubsub.new)
11
- @pubsub = pubsub
12
+ def initialize(async: true, pubsub: Google::Cloud::Pubsub.new, logger: Logger.new($stdout))
13
+ @executor = async ? :io : :immediate
14
+ @pubsub = pubsub
15
+ @logger = logger
12
16
  end
13
17
 
14
18
  def enqueue(job, attributes = {})
15
- @pubsub.topic_for(job.queue_name).publish JSON.dump(job.serialize), attributes
19
+ Concurrent::Promise.execute(executor: @executor) {
20
+ @pubsub.topic_for(job.queue_name).publish JSON.dump(job.serialize), attributes
21
+ }.rescue {|e|
22
+ @logger&.error e
23
+ }
16
24
  end
17
25
 
18
26
  def enqueue_at(job, timestamp)
@@ -1,5 +1,5 @@
1
1
  module ActiveJob
2
2
  module GoogleCloudPubsub
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -13,28 +13,34 @@ module ActiveJob
13
13
 
14
14
  using PubsubExtension
15
15
 
16
- cattr_accessor(:logger) { Logger.new($stdout) }
17
-
18
- def initialize(queue: 'default', min_threads: 0, max_threads: Concurrent.processor_count, pubsub: Google::Cloud::Pubsub.new)
19
- @queue_name, @min_threads, @max_threads, @pubsub = queue, min_threads, max_threads, pubsub
16
+ def initialize(queue: 'default', min_threads: 0, max_threads: Concurrent.processor_count, pubsub: Google::Cloud::Pubsub.new, logger: Logger.new($stdout))
17
+ @queue_name = queue
18
+ @min_threads = min_threads
19
+ @max_threads = max_threads
20
+ @pubsub = pubsub
21
+ @logger = logger
20
22
  end
21
23
 
22
24
  def run
23
25
  pool = Concurrent::ThreadPoolExecutor.new(min_threads: @min_threads, max_threads: @max_threads, max_queue: -1)
24
26
 
25
27
  @pubsub.subscription_for(@queue_name).listen do |message|
26
- logger.info "Message(#{message.message_id}) was received."
28
+ @logger&.info "Message(#{message.message_id}) was received."
27
29
 
28
30
  begin
29
31
  Concurrent::Promise.execute(args: message, executor: pool) {|msg|
30
32
  process msg
31
33
  }.rescue {|e|
32
- logger.error e
34
+ @logger&.error e
33
35
  }
34
36
  rescue Concurrent::RejectedExecutionError
35
- message.delay! 10.seconds.to_i
37
+ Concurrent::Promise.execute(args: message) {|msg|
38
+ msg.delay! 10.seconds.to_i
36
39
 
37
- logger.info "Message(#{message.message_id}) was rescheduled after 10 seconds because the thread pool is full."
40
+ @logger&.info "Message(#{msg.message_id}) was rescheduled after 10 seconds because the thread pool is full."
41
+ }.rescue {|e|
42
+ @logger&.error e
43
+ }
38
44
  end
39
45
  end
40
46
  end
@@ -59,7 +65,7 @@ module ActiveJob
59
65
 
60
66
  message.delay! deadline
61
67
 
62
- logger.info "Message(#{message.message_id}) was rescheduled after #{deadline} seconds because the timestamp is #{ts}."
68
+ @logger&.info "Message(#{message.message_id}) was rescheduled after #{deadline} seconds because the timestamp is #{ts}."
63
69
  end
64
70
  else
65
71
  _process message
@@ -94,7 +100,7 @@ module ActiveJob
94
100
  if succeeded || failed
95
101
  message.acknowledge!
96
102
 
97
- logger.info "Message(#{message.message_id}) was acknowledged."
103
+ @logger&.info "Message(#{message.message_id}) was acknowledged."
98
104
  else
99
105
  # terminated from outside
100
106
  message.delay! 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activejob-google_cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima