activejob-google-cloud-pubsub 0.7.2
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +192 -0
- data/.travis.yml +15 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +6 -0
- data/activejob-google-cloud-pubsub.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/activejob-google_cloud_pubsub-worker +43 -0
- data/lib/activejob_google_cloud_pubsub.rb +12 -0
- data/lib/activejob_google_cloud_pubsub/adapter.rb +36 -0
- data/lib/activejob_google_cloud_pubsub/pubsub_extension.rb +37 -0
- data/lib/activejob_google_cloud_pubsub/version.rb +5 -0
- data/lib/activejob_google_cloud_pubsub/worker.rb +79 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6c9860274e03768fcec32f520c0137029f118595
|
4
|
+
data.tar.gz: 7debd4cdc31b82694f90b934ca4310438bedb4a4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fda7e35dbb61582683ec6f440e3b85643a319ea41ef32d1931aef15ab47d2e8e31cc58aa22b947cbe63ba851ecff128c12529d47eedcc0439c53f3ddbbd8dc90
|
7
|
+
data.tar.gz: 4fb3b16b2d217e4bed67962c659c08b34bb0e664d35f55d7d7353c3de8cd0f41f8f0755842ba8d6b0b66b873113759822b53f509100c4941ce0097075f89412d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
Style/SymbolArray:
|
2
|
+
Enabled: true
|
3
|
+
EnforcedStyle: brackets
|
4
|
+
|
5
|
+
# kind_of? is a good way to check a type
|
6
|
+
Style/ClassCheck:
|
7
|
+
EnforcedStyle: kind_of?
|
8
|
+
|
9
|
+
# It's better to be more explicit about the type
|
10
|
+
Style/BracesAroundHashParameters:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/TernaryParentheses:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: require_parentheses_when_complex
|
16
|
+
|
17
|
+
# specs sometimes have useless assignments, which is fine
|
18
|
+
Lint/UselessAssignment:
|
19
|
+
Exclude:
|
20
|
+
- '**/spec/**/*'
|
21
|
+
|
22
|
+
# We could potentially enable the 2 below:
|
23
|
+
Layout/IndentFirstHashElement:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Layout/AlignHash:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
# HoundCI doesn't like this rule
|
30
|
+
Layout/DotPosition:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
# We allow !! as it's an easy way to convert ot boolean
|
34
|
+
Style/DoubleNegation:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/NumericPredicate:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
# Sometimes we allow a rescue block that doesn't contain code
|
41
|
+
Lint/HandleExceptions:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/RescueStandardError:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
# Cop supports --auto-correct.
|
48
|
+
Lint/UnusedBlockArgument:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
# Needed for $verbose
|
52
|
+
Style/GlobalVars:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
# We want to allow class Fastlane::Class
|
56
|
+
Style/ClassAndModuleChildren:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
# $? Exit
|
60
|
+
Style/SpecialGlobalVars:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Metrics/AbcSize:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Metrics/MethodLength:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Metrics/ModuleLength:
|
70
|
+
Enabled: true
|
71
|
+
Max: 110
|
72
|
+
|
73
|
+
Metrics/CyclomaticComplexity:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
Metrics/BlockNesting:
|
77
|
+
Max: 5
|
78
|
+
|
79
|
+
Metrics/BlockLength:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
# The %w might be confusing for new users
|
83
|
+
Style/WordArray:
|
84
|
+
MinSize: 19
|
85
|
+
|
86
|
+
# raise and fail are both okay
|
87
|
+
Style/SignalException:
|
88
|
+
Enabled: false
|
89
|
+
|
90
|
+
# Better too much 'return' than one missing
|
91
|
+
Style/RedundantReturn:
|
92
|
+
Enabled: false
|
93
|
+
|
94
|
+
# Having if in the same line might not always be good
|
95
|
+
Style/IfUnlessModifier:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
# and and or is okay
|
99
|
+
Style/AndOr:
|
100
|
+
Enabled: false
|
101
|
+
|
102
|
+
# Configuration parameters: CountComments.
|
103
|
+
Metrics/ClassLength:
|
104
|
+
Max: 400
|
105
|
+
|
106
|
+
# Configuration parameters: AllowURI, URISchemes.
|
107
|
+
Metrics/LineLength:
|
108
|
+
Enabled: false
|
109
|
+
Max: 370
|
110
|
+
|
111
|
+
# Configuration parameters: CountKeywordArgs.
|
112
|
+
Metrics/ParameterLists:
|
113
|
+
Max: 17
|
114
|
+
|
115
|
+
Metrics/PerceivedComplexity:
|
116
|
+
Max: 25
|
117
|
+
|
118
|
+
# Sometimes it's easier to read without guards
|
119
|
+
Style/GuardClause:
|
120
|
+
Enabled: false
|
121
|
+
|
122
|
+
# We allow both " and '
|
123
|
+
Style/StringLiterals:
|
124
|
+
Enabled: false
|
125
|
+
|
126
|
+
# something = if something_else
|
127
|
+
# that's confusing
|
128
|
+
Style/ConditionalAssignment:
|
129
|
+
Enabled: false
|
130
|
+
|
131
|
+
# Better to have too much self than missing a self
|
132
|
+
Style/RedundantSelf:
|
133
|
+
Enabled: false
|
134
|
+
|
135
|
+
# e.g.
|
136
|
+
# def self.is_supported?(platform)
|
137
|
+
# we may never use `platform`
|
138
|
+
Lint/UnusedMethodArgument:
|
139
|
+
Enabled: false
|
140
|
+
|
141
|
+
# the let(:key) { ... }
|
142
|
+
Lint/ParenthesesAsGroupedExpression:
|
143
|
+
Exclude:
|
144
|
+
- '**/spec/**/*'
|
145
|
+
|
146
|
+
# This would reject is_ in front of methods
|
147
|
+
# We use `is_supported?` everywhere already
|
148
|
+
Naming/PredicateName:
|
149
|
+
Enabled: false
|
150
|
+
|
151
|
+
# We allow the $
|
152
|
+
Style/PerlBackrefs:
|
153
|
+
Enabled: false
|
154
|
+
|
155
|
+
# Disable '+ should be surrounded with a single space' for xcodebuild_spec.rb
|
156
|
+
Layout/SpaceAroundOperators:
|
157
|
+
Exclude:
|
158
|
+
- '**/spec/actions_specs/xcodebuild_spec.rb'
|
159
|
+
|
160
|
+
# We're not there yet
|
161
|
+
Style/Documentation:
|
162
|
+
Enabled: false
|
163
|
+
|
164
|
+
# Added after upgrade to 0.38.0
|
165
|
+
Style/MutableConstant:
|
166
|
+
Enabled: false
|
167
|
+
|
168
|
+
# length > 0 is good
|
169
|
+
Style/ZeroLengthPredicate:
|
170
|
+
Enabled: false
|
171
|
+
|
172
|
+
# Adds complexity
|
173
|
+
Style/IfInsideElse:
|
174
|
+
Enabled: false
|
175
|
+
|
176
|
+
Style/RescueModifier:
|
177
|
+
Enabled: false
|
178
|
+
|
179
|
+
Naming/VariableNumber:
|
180
|
+
Enabled: false
|
181
|
+
|
182
|
+
Style/ClassVars:
|
183
|
+
Enabled: false
|
184
|
+
|
185
|
+
Style/FrozenStringLiteralComment:
|
186
|
+
Enabled: false
|
187
|
+
|
188
|
+
AllCops:
|
189
|
+
TargetRubyVersion: "2.3.3"
|
190
|
+
Exclude:
|
191
|
+
- './tmp/**/*'
|
192
|
+
- './Gemfile'
|
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- 2.4
|
6
|
+
- 2.5
|
7
|
+
|
8
|
+
before_install:
|
9
|
+
- curl -sL https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz | tar xz
|
10
|
+
- google-cloud-sdk/install.sh --override-components beta pubsub-emulator --quiet
|
11
|
+
- export PATH=$PATH:$PWD/google-cloud-sdk/bin
|
12
|
+
|
13
|
+
matrix:
|
14
|
+
allow_failures:
|
15
|
+
- rvm: 2.5 # grpc does not yet support Ruby 2.5
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Keita Urashima
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# ActiveJob::GoogleCloudPubsub
|
2
|
+
|
3
|
+
[](https://travis-ci.org/magloft/activejob-google-cloud-pubsub)
|
4
|
+
[](https://badge.fury.io/magloft/activejob-google-cloud-pubsub)
|
5
|
+
|
6
|
+
Google Cloud Pub/Sub adapter and worker for ActiveJob
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'activejob-google-cloud-pubsub'
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
First, change the ActiveJob backend.
|
17
|
+
|
18
|
+
``` ruby
|
19
|
+
Rails.application.config.active_job.queue_adapter = :google_cloud_pubsub
|
20
|
+
```
|
21
|
+
|
22
|
+
Write the Job class and code to use it.
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
class HelloJob < ApplicationJob
|
26
|
+
def perform(name)
|
27
|
+
puts "hello, #{name}!"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
``` ruby
|
33
|
+
class HelloController < ApplicationController
|
34
|
+
def say
|
35
|
+
HelloJob.perform_later params[:name]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
In order to test the worker in your local environment, it is a good idea to use the Pub/Sub emulator provided by `gcloud` command. Refer to [this document](https://cloud.google.com/pubsub/docs/emulator) for the installation procedure.
|
41
|
+
|
42
|
+
When the installation is completed, execute the following command to start up the worker.
|
43
|
+
|
44
|
+
``` sh
|
45
|
+
$ gcloud beta emulators pubsub start
|
46
|
+
|
47
|
+
(Switch to another terminal)
|
48
|
+
|
49
|
+
$ eval `gcloud beta emulators pubsub env-init`
|
50
|
+
$ cd path/to/your-app
|
51
|
+
$ bundle exec activejob-google-cloud-pubsub-worker --project_id=dummy
|
52
|
+
```
|
53
|
+
|
54
|
+
If you hit the previous action, the job will be executed.
|
55
|
+
(Both the emulator and the worker stop with <kbd>Ctrl+C</kbd>)
|
56
|
+
|
57
|
+
## Configuration
|
58
|
+
|
59
|
+
### Adapter
|
60
|
+
|
61
|
+
When passing options to the adapter, you need to create the object instead of a symbol.
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
Rails.application.config.active_job.queue_adapter = ActiveJob::GoogleCloudPubsub::Adapter.new(
|
65
|
+
async: false,
|
66
|
+
logger: Rails.logger,
|
67
|
+
|
68
|
+
pubsub: Google::Cloud::Pubsub.new(
|
69
|
+
project_id: 'MY-PROJECT-ID',
|
70
|
+
credentials: 'path/to/keyfile.json',
|
71
|
+
timeout: 60
|
72
|
+
)
|
73
|
+
)
|
74
|
+
```
|
75
|
+
|
76
|
+
#### `async`
|
77
|
+
|
78
|
+
Whether to publish messages asynchronously.
|
79
|
+
|
80
|
+
Default: `true`
|
81
|
+
|
82
|
+
#### `logger`
|
83
|
+
|
84
|
+
The logger that outputs a message publishing error. Specify `nil` to disable logging.
|
85
|
+
|
86
|
+
Default: `Logger.new($stdout)`
|
87
|
+
|
88
|
+
#### `pubsub`
|
89
|
+
|
90
|
+
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.
|
91
|
+
|
92
|
+
Default: `Google::Cloud::Pubsub.new(timeout: 60)`
|
93
|
+
|
94
|
+
### Worker
|
95
|
+
|
96
|
+
The following command line flags can be specified. All flags are optional.
|
97
|
+
|
98
|
+
#### `--require=PATH`
|
99
|
+
|
100
|
+
The path of the file to load before the worker starts up.
|
101
|
+
|
102
|
+
Default: `./config/environment`
|
103
|
+
|
104
|
+
#### `--queue=NAME`
|
105
|
+
|
106
|
+
The name of the queue the worker handles.
|
107
|
+
|
108
|
+
Note: One worker can handle only one queue. If you use multiple queues, you need to launch multiple worker processes.
|
109
|
+
|
110
|
+
Default: `default`
|
111
|
+
|
112
|
+
#### `--project_id=PROJECT_ID`, `--credentials=KEYFILE_PATH`
|
113
|
+
|
114
|
+
Credentials of Google Cloud Platform. Please see [the document](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/AUTHENTICATION.md) for details.
|
115
|
+
|
116
|
+
## Development
|
117
|
+
|
118
|
+
``` sh
|
119
|
+
$ bundle exec rake spec
|
120
|
+
```
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
|
124
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ursm/activejob-google_cloud_pubsub.
|
125
|
+
|
126
|
+
## License
|
127
|
+
|
128
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'activejob_google_cloud_pubsub/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'activejob-google-cloud-pubsub'
|
7
|
+
spec.version = ActiveJob::GoogleCloudPubsub::VERSION
|
8
|
+
spec.authors = ['Keita Urashima']
|
9
|
+
spec.email = ['ursm@ursm.jp']
|
10
|
+
|
11
|
+
spec.summary = 'Google Cloud Pub/Sub adapter and worker for ActiveJob'
|
12
|
+
spec.homepage = 'https://github.com/magloft/activejob-google-cloud-pubsub'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'activejob'
|
24
|
+
spec.add_runtime_dependency 'activesupport'
|
25
|
+
spec.add_runtime_dependency 'concurrent-ruby'
|
26
|
+
spec.add_runtime_dependency 'google-cloud-pubsub', '>= 0.27.0'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler'
|
29
|
+
spec.add_development_dependency 'rake'
|
30
|
+
spec.add_development_dependency 'rspec'
|
31
|
+
spec.add_development_dependency 'rubocop'
|
32
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'activejob_google_cloud_pubsub'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'activejob_google_cloud_pubsub'
|
4
|
+
require 'optparse/kwargs'
|
5
|
+
|
6
|
+
Version = ActiveJob::GoogleCloudPubsub::VERSION
|
7
|
+
|
8
|
+
opts = {
|
9
|
+
require: './config/environment'
|
10
|
+
}
|
11
|
+
|
12
|
+
parser = OptionParser.new
|
13
|
+
|
14
|
+
parser.on '--[no-]require=PATH' do |v|
|
15
|
+
opts[:require] = v
|
16
|
+
end
|
17
|
+
|
18
|
+
worker_args = parser.define_by_keywords(
|
19
|
+
{},
|
20
|
+
ActiveJob::GoogleCloudPubsub::Worker.instance_method(:initialize),
|
21
|
+
{}
|
22
|
+
)
|
23
|
+
|
24
|
+
pubsub_args = parser.define_by_keywords(
|
25
|
+
{},
|
26
|
+
Google::Cloud::Pubsub.method(:new),
|
27
|
+
{
|
28
|
+
scope: Array,
|
29
|
+
timeout: Integer
|
30
|
+
}
|
31
|
+
)
|
32
|
+
|
33
|
+
parser.parse ARGV
|
34
|
+
|
35
|
+
require opts[:require] if opts[:require]
|
36
|
+
|
37
|
+
pubsub = Google::Cloud::Pubsub.new(pubsub_args)
|
38
|
+
|
39
|
+
begin
|
40
|
+
ActiveJob::GoogleCloudPubsub::Worker.new(pubsub: pubsub, **worker_args).run
|
41
|
+
rescue Interrupt
|
42
|
+
exit
|
43
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveJob
|
2
|
+
module GoogleCloudPubsub
|
3
|
+
autoload :Adapter, 'activejob_google_cloud_pubsub/adapter'
|
4
|
+
autoload :VERSION, 'activejob_google_cloud_pubsub/version'
|
5
|
+
autoload :Worker, 'activejob_google_cloud_pubsub/worker'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'active_job'
|
10
|
+
require 'google/cloud/pubsub'
|
11
|
+
|
12
|
+
ActiveJob::QueueAdapters.autoload :GoogleCloudPubsubAdapter, 'activejob_google_cloud_pubsub/adapter'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'activejob_google_cloud_pubsub/pubsub_extension'
|
2
|
+
require 'concurrent'
|
3
|
+
require 'google/cloud/pubsub'
|
4
|
+
require 'json'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
module ActiveJob
|
8
|
+
module GoogleCloudPubsub
|
9
|
+
class Adapter
|
10
|
+
using PubsubExtension
|
11
|
+
|
12
|
+
def initialize(async: true, pubsub: Google::Cloud::Pubsub.new(timeout: 60), logger: Logger.new($stdout))
|
13
|
+
@executor = async ? :io : :immediate
|
14
|
+
@pubsub = pubsub
|
15
|
+
@logger = logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def enqueue(job, attributes = {})
|
19
|
+
promise = Concurrent::Promise.execute(executor: @executor) do
|
20
|
+
@pubsub.topic_for(job.queue_name).publish JSON.dump(job.serialize), attributes
|
21
|
+
end
|
22
|
+
promise.rescue do |e|
|
23
|
+
@logger&.error e
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def enqueue_at(job, timestamp)
|
28
|
+
enqueue job, timestamp: timestamp
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'active_job'
|
35
|
+
|
36
|
+
ActiveJob::QueueAdapters::GoogleCloudPubsubAdapter = ActiveJob::GoogleCloudPubsub::Adapter
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'google/cloud/pubsub'
|
2
|
+
|
3
|
+
module ActiveJob
|
4
|
+
module GoogleCloudPubsub
|
5
|
+
module PubsubExtension
|
6
|
+
refine Google::Cloud::Pubsub::Project do
|
7
|
+
def topic_for(queue_name)
|
8
|
+
name = "activejob-queue-#{queue_name}"
|
9
|
+
|
10
|
+
topic(name) || create_topic(name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def subscription_for(queue_name)
|
14
|
+
name = "activejob-worker-#{queue_name}"
|
15
|
+
|
16
|
+
subscription(name) || topic_for(queue_name).subscribe(name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
refine Google::Cloud::Pubsub::ReceivedMessage do
|
21
|
+
def scheduled_at
|
22
|
+
return nil unless (timestamp = attributes['timestamp'])
|
23
|
+
|
24
|
+
Time.at(timestamp.to_f)
|
25
|
+
end
|
26
|
+
|
27
|
+
def remaining_time_to_schedule
|
28
|
+
scheduled_at ? [(scheduled_at - Time.now).to_f.ceil, 0].max : 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def time_to_process?
|
32
|
+
remaining_time_to_schedule.zero?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'active_job/base'
|
2
|
+
require 'active_support/core_ext/numeric/time'
|
3
|
+
require 'activejob_google_cloud_pubsub/pubsub_extension'
|
4
|
+
require 'google/cloud/pubsub'
|
5
|
+
require 'json'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
module ActiveJob
|
9
|
+
module GoogleCloudPubsub
|
10
|
+
class Worker
|
11
|
+
MAX_DEADLINE = 10.minutes
|
12
|
+
|
13
|
+
using PubsubExtension
|
14
|
+
|
15
|
+
def initialize(queue: 'default', pubsub: Google::Cloud::Pubsub.new(timeout: 60), logger: Logger.new($stdout))
|
16
|
+
@queue_name = queue
|
17
|
+
@pubsub = pubsub
|
18
|
+
@logger = logger
|
19
|
+
end
|
20
|
+
|
21
|
+
def run
|
22
|
+
subscriber = @pubsub.subscription_for(@queue_name).listen do |message|
|
23
|
+
@logger&.info "Message(#{message.message_id}) was received."
|
24
|
+
process message
|
25
|
+
end
|
26
|
+
|
27
|
+
subscriber.on_error do |error|
|
28
|
+
@logger&.error(error)
|
29
|
+
end
|
30
|
+
|
31
|
+
subscriber.start
|
32
|
+
|
33
|
+
sleep
|
34
|
+
end
|
35
|
+
|
36
|
+
def ensure_subscription
|
37
|
+
@pubsub.subscription_for @queue_name
|
38
|
+
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def process(message)
|
45
|
+
timer_opts = {
|
46
|
+
execution_interval: MAX_DEADLINE - 10.seconds,
|
47
|
+
timeout_interval: 5.seconds,
|
48
|
+
run_now: true
|
49
|
+
}
|
50
|
+
|
51
|
+
delay_timer = Concurrent::TimerTask.execute(timer_opts) do
|
52
|
+
message.modify_ack_deadline! MAX_DEADLINE.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
begin
|
56
|
+
succeeded = false
|
57
|
+
failed = false
|
58
|
+
|
59
|
+
ActiveJob::Base.execute JSON.parse(message.data)
|
60
|
+
|
61
|
+
succeeded = true
|
62
|
+
rescue StandardError
|
63
|
+
failed = true
|
64
|
+
raise
|
65
|
+
ensure
|
66
|
+
delay_timer.shutdown
|
67
|
+
|
68
|
+
if succeeded || failed
|
69
|
+
message.acknowledge!
|
70
|
+
@logger&.info "Message(#{message.message_id}) was acknowledged."
|
71
|
+
else
|
72
|
+
# terminated from outside
|
73
|
+
message.modify_ack_deadline! 0
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activejob-google-cloud-pubsub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keita Urashima
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activejob
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: concurrent-ruby
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: google-cloud-pubsub
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.27.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.27.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- ursm@ursm.jp
|
128
|
+
executables:
|
129
|
+
- activejob-google_cloud_pubsub-worker
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
136
|
+
- ".travis.yml"
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- activejob-google-cloud-pubsub.gemspec
|
142
|
+
- bin/console
|
143
|
+
- bin/setup
|
144
|
+
- exe/activejob-google_cloud_pubsub-worker
|
145
|
+
- lib/activejob_google_cloud_pubsub.rb
|
146
|
+
- lib/activejob_google_cloud_pubsub/adapter.rb
|
147
|
+
- lib/activejob_google_cloud_pubsub/pubsub_extension.rb
|
148
|
+
- lib/activejob_google_cloud_pubsub/version.rb
|
149
|
+
- lib/activejob_google_cloud_pubsub/worker.rb
|
150
|
+
homepage: https://github.com/magloft/activejob-google-cloud-pubsub
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.5.2
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Google Cloud Pub/Sub adapter and worker for ActiveJob
|
174
|
+
test_files: []
|