activejob-google_cloud_pubsub 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c7fb77dfcc2712ebff760e45cb9fcfb605230f1
4
+ data.tar.gz: '086f22928fb681f54ede2c7c114433362b699bc7'
5
+ SHA512:
6
+ metadata.gz: 0d15086d08144818ae5ca082c0e9f0457455b2d02bd5471cfaf5dad6d90096eaabf0f38317448d2886f7a8a84af9e9d48b8e0601863a1a4187433e6b0afaffd5
7
+ data.tar.gz: 9a73c4e6a1f361a2346e467f48aa62820f766ed1ac518947166a935c9d169582506a832997e212b915889fe46b02370b87ee743821da1be5504d107e435b36ec
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+
4
+ rvm:
5
+ - 2.4.1
6
+
7
+ before_install:
8
+ - curl -sL https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz | tar xz
9
+ - google-cloud-sdk/install.sh --override-components beta pubsub-emulator --quiet
10
+ - export PATH=$PATH:$PWD/google-cloud-sdk/bin
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'concurrent-ruby-ext'
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,110 @@
1
+ # ActiveJob::GoogleCloudPubsub
2
+
3
+ Google Cloud Pub/Sub adapter and worker for ActiveJob
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'activejob-google_cloud_pubsub'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ First, change the ActiveJob backend.
14
+
15
+ ``` ruby
16
+ Rails.application.config.active_job.queue_adapter = :google_cloud_pubsub
17
+ ```
18
+
19
+ Write the Job class and code to use it.
20
+
21
+ ``` ruby
22
+ class HelloJob < ApplicationJob
23
+ def perform(name)
24
+ puts "hello, #{name}!"
25
+ end
26
+ end
27
+ ```
28
+
29
+ ``` ruby
30
+ class HelloController < ApplicationController
31
+ def say
32
+ HelloJob.perform_later params[:name]
33
+ end
34
+ end
35
+ ```
36
+
37
+ 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.
38
+
39
+ When the installation is completed, execute the following command to start up the worker.
40
+
41
+ ``` sh
42
+ $ gcloud beta emulators pubsub start
43
+
44
+ (Switch to another terminal)
45
+
46
+ $ eval `gcloud beta emulators pubsub env-init`
47
+ $ cd path/to/your-app
48
+ $ bundle exec activejob-google_cloud_pubsub-worker
49
+ ```
50
+
51
+ If you hit the previous action, the job will be executed.
52
+ (Both the emulator and the worker stop with <kbd>Ctrl+C</kbd>)
53
+
54
+ ## Configuration
55
+
56
+ ### Adapter
57
+
58
+ When passing options to the adapter, you need to create the object instead of a symbol.
59
+
60
+ ``` ruby
61
+ Rails.application.config.active_job.queue_adapter = ActiveJob::QueueAdapters::GoogleCloudPubsubAdapter.new(options)
62
+ ```
63
+
64
+ All options are passed to [`Google::Cloud::Pubsub.new`](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-pubsub/v0.23.2/google/cloud/pubsub?method=new-class).
65
+
66
+ ### Worker
67
+
68
+ The following command line flags can be specified. All flags are optional.
69
+
70
+ #### `--require=PATH`
71
+
72
+ The path of the file to load before the worker starts up.
73
+
74
+ Default: `./config/environment`
75
+
76
+ #### `--queue=NAME`
77
+
78
+ The name of the queue the worker handles.
79
+
80
+ Note: One worker can handle only one queue. If you use multiple queues, you need to launch multiple worker processes.
81
+
82
+ Default: `default`
83
+
84
+ #### `--min_threads=N`
85
+
86
+ Minimum number of worker threads.
87
+
88
+ Default: `0`
89
+
90
+ #### `--max_threads=N`
91
+
92
+ Maximum number of worker threads.
93
+
94
+ Default: number of logical cores
95
+
96
+ #### `--project=PROJECT_ID`, `--keyfile=PATH`
97
+
98
+ Credentials of Google Cloud Platform. Please refer to [the document](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/AUTHENTICATION.md) for details.
99
+
100
+ ``` sh
101
+ $ bundle exec rake spec
102
+ ```
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ursm/activejob-google_cloud_pubsub.
107
+
108
+ ## License
109
+
110
+ 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,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'active_job/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/ursm/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'
27
+
28
+ spec.add_development_dependency 'bundler'
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'rspec'
31
+ 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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'activejob-google_cloud_pubsub'
4
+ require 'google/cloud/pubsub'
5
+ require 'optparse/kwargs'
6
+
7
+ Version = ActiveJob::GoogleCloudPubsub::VERSION
8
+
9
+ args = {
10
+ require: './config/environment'
11
+ }
12
+
13
+ opt = OptionParser.new
14
+
15
+ opt.on '--[no-]require=PATH' do |v|
16
+ args[:require] = v
17
+ end
18
+
19
+ worker_args = opt.define_by_keywords(
20
+ {},
21
+ ActiveJob::GoogleCloudPubsub::Worker.instance_method(:initialize),
22
+ {
23
+ min_threads: Integer,
24
+ max_threads: Integer
25
+ }
26
+ )
27
+
28
+ pubsub_args = opt.define_by_keywords(
29
+ {},
30
+ Google::Cloud::Pubsub.method(:new),
31
+ {
32
+ scope: Array,
33
+ timeout: Integer
34
+ }
35
+ )
36
+
37
+ opt.parse ARGV
38
+
39
+ require args[:require] if args[:require]
40
+
41
+ ActiveJob::GoogleCloudPubsub::Worker.new(**worker_args, **pubsub_args).run
@@ -0,0 +1,29 @@
1
+ require 'active_job/google_cloud_pubsub/naming'
2
+ require 'google/cloud/pubsub'
3
+ require 'json'
4
+
5
+ module ActiveJob
6
+ module GoogleCloudPubsub
7
+ class Adapter
8
+ include Naming
9
+
10
+ def initialize(**pubsub_args)
11
+ @pubsub = Google::Cloud::Pubsub.new(pubsub_args)
12
+ end
13
+
14
+ def enqueue(job, attributes = {})
15
+ topic = @pubsub.topic(topic_name(job.queue_name), autocreate: true)
16
+
17
+ topic.publish JSON.dump(job.serialize), attributes
18
+ end
19
+
20
+ def enqueue_at(job, timestamp)
21
+ enqueue job, timestamp: timestamp
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ require 'active_job'
28
+
29
+ ActiveJob::QueueAdapters::GoogleCloudPubsubAdapter = ActiveJob::GoogleCloudPubsub::Adapter
@@ -0,0 +1,13 @@
1
+ module ActiveJob
2
+ module GoogleCloudPubsub
3
+ module Naming
4
+ def topic_name(queue_name)
5
+ "activejob-queue-#{queue_name}"
6
+ end
7
+
8
+ def subscription_name(queue_name)
9
+ "activejob-worker-#{queue_name}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveJob
2
+ module GoogleCloudPubsub
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,98 @@
1
+ require 'active_job/base'
2
+ require 'active_job/google_cloud_pubsub/naming'
3
+ require 'active_support/core_ext/numeric/time'
4
+ require 'concurrent'
5
+ require 'google/cloud/pubsub'
6
+ require 'json'
7
+ require 'logger'
8
+
9
+ module ActiveJob
10
+ module GoogleCloudPubsub
11
+ class Worker
12
+ include Naming
13
+
14
+ MAX_DEADLINE = 10.minutes
15
+
16
+ cattr_accessor(:logger) { Logger.new($stdout) }
17
+
18
+ def initialize(queue: 'default', min_threads: 0, max_threads: Concurrent.processor_count, **pubsub_args)
19
+ @queue_name, @min_threads, @max_threads = queue, min_threads, max_threads
20
+
21
+ @pubsub = Google::Cloud::Pubsub.new(**pubsub_args)
22
+ end
23
+
24
+ def subscription
25
+ topic = @pubsub.topic(topic_name(@queue_name), autocreate: true)
26
+
27
+ topic.subscription(subscription_name(@queue_name)) || topic.subscribe(subscription_name(@queue_name))
28
+ end
29
+ alias ensure_subscription subscription
30
+
31
+ def run
32
+ pool = Concurrent::ThreadPoolExecutor.new(min_threads: @min_threads, max_threads: @max_threads, max_queue: -1)
33
+
34
+ subscription.listen do |message|
35
+ begin
36
+ Concurrent::Promise.execute(args: message, executor: pool) {|msg|
37
+ process msg
38
+ }.rescue {|e|
39
+ logger.error e
40
+ }
41
+ rescue Concurrent::RejectedExecutionError
42
+ message.delay! 10.seconds.to_i
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def process(message)
50
+ if timestamp = message.attributes['timestamp']
51
+ ts = Time.at(timestamp.to_f)
52
+
53
+ if ts >= Time.now
54
+ _process message
55
+ else
56
+ message.delay! [ts - Time.now, MAX_DEADLINE.to_i].min
57
+ end
58
+ else
59
+ _process message
60
+ end
61
+ end
62
+
63
+ def _process(message)
64
+ timer_opts = {
65
+ execution_interval: MAX_DEADLINE - 10.seconds,
66
+ timeout_interval: 5.seconds,
67
+ run_now: true
68
+ }
69
+
70
+ delay_timer = Concurrent::TimerTask.execute(timer_opts) {
71
+ message.delay! MAX_DEADLINE.to_i
72
+ }
73
+
74
+ begin
75
+ succeeded = false
76
+ failed = false
77
+
78
+ ActiveJob::Base.execute JSON.parse(message.data)
79
+
80
+ succeeded = true
81
+ rescue Exception
82
+ failed = true
83
+
84
+ raise
85
+ ensure
86
+ delay_timer.shutdown
87
+
88
+ if succeeded || failed
89
+ message.acknowledge!
90
+ else
91
+ # terminated from outside
92
+ message.delay! 0
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_job'
2
+
3
+ module ActiveJob
4
+ module GoogleCloudPubsub
5
+ autoload :VERSION, 'active_job/google_cloud_pubsub/version'
6
+ autoload :Worker, 'active_job/google_cloud_pubsub/worker'
7
+ end
8
+
9
+ module QueueAdapters
10
+ autoload :GoogleCloudPubsubAdapter, 'active_job/google_cloud_pubsub/adapter'
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activejob-google_cloud_pubsub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keita Urashima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-01 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'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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
+ description:
112
+ email:
113
+ - ursm@ursm.jp
114
+ executables:
115
+ - activejob-google_cloud_pubsub-worker
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - activejob-google_cloud_pubsub.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - exe/activejob-google_cloud_pubsub-worker
130
+ - lib/active_job/google_cloud_pubsub/adapter.rb
131
+ - lib/active_job/google_cloud_pubsub/naming.rb
132
+ - lib/active_job/google_cloud_pubsub/version.rb
133
+ - lib/active_job/google_cloud_pubsub/worker.rb
134
+ - lib/activejob-google_cloud_pubsub.rb
135
+ homepage: https://github.com/ursm/activejob-google_cloud_pubsub
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.6.11
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Google Cloud Pub/Sub adapter and worker for ActiveJob
159
+ test_files: []