activejob-google_cloud_pubsub 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e759c958bd41ca18bb5d4cc514c5586ceba3ea8
|
4
|
+
data.tar.gz: 213be00c966bd8c92c2a22a7c96dec0ad9a3ff9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
@
|
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
|
-
|
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)
|
@@ -13,28 +13,34 @@ module ActiveJob
|
|
13
13
|
|
14
14
|
using PubsubExtension
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@
|
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
|
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
|
34
|
+
@logger&.error e
|
33
35
|
}
|
34
36
|
rescue Concurrent::RejectedExecutionError
|
35
|
-
|
37
|
+
Concurrent::Promise.execute(args: message) {|msg|
|
38
|
+
msg.delay! 10.seconds.to_i
|
36
39
|
|
37
|
-
|
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
|
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
|
103
|
+
@logger&.info "Message(#{message.message_id}) was acknowledged."
|
98
104
|
else
|
99
105
|
# terminated from outside
|
100
106
|
message.delay! 0
|