activejob-google_cloud_pubsub 0.1.0 → 0.2.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: 9c7fb77dfcc2712ebff760e45cb9fcfb605230f1
4
- data.tar.gz: '086f22928fb681f54ede2c7c114433362b699bc7'
3
+ metadata.gz: c7e0c78787b37b8180943e0f4a6c4ea393a00bae
4
+ data.tar.gz: 651b28c7e58141252a34b9be2d550dbdd26fc1bc
5
5
  SHA512:
6
- metadata.gz: 0d15086d08144818ae5ca082c0e9f0457455b2d02bd5471cfaf5dad6d90096eaabf0f38317448d2886f7a8a84af9e9d48b8e0601863a1a4187433e6b0afaffd5
7
- data.tar.gz: 9a73c4e6a1f361a2346e467f48aa62820f766ed1ac518947166a935c9d169582506a832997e212b915889fe46b02370b87ee743821da1be5504d107e435b36ec
6
+ metadata.gz: cf2214ee8c25208dbfb4e2f03dea18550a010571bd321ae14421c4e3f5314dbc9aa5cc6c54fead51ce9febd371234a8e060cea07eb913596edc060f4969c3ff2
7
+ data.tar.gz: 7dbd633493e0f635bb29b07da2c8a35f941d80301194cb109e61189dd49ca05b9d68ce51f880497f22b1ae0ed3b709aa45508832691fa9a01a1c7b0a279212ed
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'concurrent-ruby-ext'
6
+ gem 'faraday', '< 0.12.0' # https://github.com/lostisland/faraday/issues/679
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ActiveJob::GoogleCloudPubsub
2
2
 
3
+ [![Build Status](https://travis-ci.org/ursm/activejob-google_cloud_pubsub.svg?branch=master)](https://travis-ci.org/ursm/activejob-google_cloud_pubsub)
4
+ [![Gem Version](https://badge.fury.io/rb/activejob-google_cloud_pubsub.svg)](https://badge.fury.io/rb/activejob-google_cloud_pubsub)
5
+
3
6
  Google Cloud Pub/Sub adapter and worker for ActiveJob
4
7
 
5
8
  ## Installation
@@ -58,10 +61,15 @@ If you hit the previous action, the job will be executed.
58
61
  When passing options to the adapter, you need to create the object instead of a symbol.
59
62
 
60
63
  ``` ruby
61
- Rails.application.config.active_job.queue_adapter = ActiveJob::QueueAdapters::GoogleCloudPubsubAdapter.new(options)
64
+ Rails.application.config.active_job.queue_adapter = ActiveJob::QueueAdapters::GoogleCloudPubsubAdapter.new(
65
+ pubsub: Google::Cloud::Pubsub.new(
66
+ project: 'MY-PROJECT-ID',
67
+ keyfile: 'path/to/keyfile.json'
68
+ )
69
+ )
62
70
  ```
63
71
 
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).
72
+ Please see [`Google::Cloud::Pubsub.new`](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-pubsub/v0.23.2/google/cloud/pubsub?method=new-class) for details.
65
73
 
66
74
  ### Worker
67
75
 
@@ -95,7 +103,9 @@ Default: number of logical cores
95
103
 
96
104
  #### `--project=PROJECT_ID`, `--keyfile=PATH`
97
105
 
98
- Credentials of Google Cloud Platform. Please refer to [the document](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/AUTHENTICATION.md) for details.
106
+ Credentials of Google Cloud Platform. Please see [the document](https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/AUTHENTICATION.md) for details.
107
+
108
+ ## Development
99
109
 
100
110
  ``` sh
101
111
  $ bundle exec rake spec
@@ -6,17 +6,17 @@ require 'optparse/kwargs'
6
6
 
7
7
  Version = ActiveJob::GoogleCloudPubsub::VERSION
8
8
 
9
- args = {
9
+ opts = {
10
10
  require: './config/environment'
11
11
  }
12
12
 
13
- opt = OptionParser.new
13
+ parser = OptionParser.new
14
14
 
15
- opt.on '--[no-]require=PATH' do |v|
16
- args[:require] = v
15
+ parser.on '--[no-]require=PATH' do |v|
16
+ opts[:require] = v
17
17
  end
18
18
 
19
- worker_args = opt.define_by_keywords(
19
+ worker_args = parser.define_by_keywords(
20
20
  {},
21
21
  ActiveJob::GoogleCloudPubsub::Worker.instance_method(:initialize),
22
22
  {
@@ -25,7 +25,7 @@ worker_args = opt.define_by_keywords(
25
25
  }
26
26
  )
27
27
 
28
- pubsub_args = opt.define_by_keywords(
28
+ pubsub_args = parser.define_by_keywords(
29
29
  {},
30
30
  Google::Cloud::Pubsub.method(:new),
31
31
  {
@@ -34,8 +34,10 @@ pubsub_args = opt.define_by_keywords(
34
34
  }
35
35
  )
36
36
 
37
- opt.parse ARGV
37
+ parser.parse ARGV
38
38
 
39
- require args[:require] if args[:require]
39
+ require opts[:require] if opts[:require]
40
40
 
41
- ActiveJob::GoogleCloudPubsub::Worker.new(**worker_args, **pubsub_args).run
41
+ pubsub = Google::Cloud::Pubsub.new(pubsub_args)
42
+
43
+ ActiveJob::GoogleCloudPubsub::Worker.new(pubsub: pubsub, **worker_args).run
@@ -1,20 +1,18 @@
1
- require 'active_job/google_cloud_pubsub/naming'
1
+ require 'active_job/google_cloud_pubsub/pubsub_extension'
2
2
  require 'google/cloud/pubsub'
3
3
  require 'json'
4
4
 
5
5
  module ActiveJob
6
6
  module GoogleCloudPubsub
7
7
  class Adapter
8
- include Naming
8
+ using PubsubExtension
9
9
 
10
- def initialize(**pubsub_args)
11
- @pubsub = Google::Cloud::Pubsub.new(pubsub_args)
10
+ def initialize(pubsub: Google::Cloud::Pubsub.new)
11
+ @pubsub = pubsub
12
12
  end
13
13
 
14
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
15
+ @pubsub.topic_for(job.queue_name).publish JSON.dump(job.serialize), attributes
18
16
  end
19
17
 
20
18
  def enqueue_at(job, timestamp)
@@ -0,0 +1,21 @@
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, autocreate: true)
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
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveJob
2
2
  module GoogleCloudPubsub
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_job/base'
2
- require 'active_job/google_cloud_pubsub/naming'
2
+ require 'active_job/google_cloud_pubsub/pubsub_extension'
3
3
  require 'active_support/core_ext/numeric/time'
4
4
  require 'concurrent'
5
5
  require 'google/cloud/pubsub'
@@ -9,29 +9,20 @@ require 'logger'
9
9
  module ActiveJob
10
10
  module GoogleCloudPubsub
11
11
  class Worker
12
- include Naming
13
-
14
12
  MAX_DEADLINE = 10.minutes
15
13
 
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
14
+ using PubsubExtension
23
15
 
24
- def subscription
25
- topic = @pubsub.topic(topic_name(@queue_name), autocreate: true)
16
+ cattr_accessor(:logger) { Logger.new($stdout) }
26
17
 
27
- topic.subscription(subscription_name(@queue_name)) || topic.subscribe(subscription_name(@queue_name))
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
28
20
  end
29
- alias ensure_subscription subscription
30
21
 
31
22
  def run
32
23
  pool = Concurrent::ThreadPoolExecutor.new(min_threads: @min_threads, max_threads: @max_threads, max_queue: -1)
33
24
 
34
- subscription.listen do |message|
25
+ @pubsub.subscription_for(@queue_name).listen do |message|
35
26
  begin
36
27
  Concurrent::Promise.execute(args: message, executor: pool) {|msg|
37
28
  process msg
@@ -44,6 +35,12 @@ module ActiveJob
44
35
  end
45
36
  end
46
37
 
38
+ def ensure_subscription
39
+ @pubsub.subscription_for @queue_name
40
+
41
+ nil
42
+ end
43
+
47
44
  private
48
45
 
49
46
  def process(message)
@@ -53,7 +50,7 @@ module ActiveJob
53
50
  if ts >= Time.now
54
51
  _process message
55
52
  else
56
- message.delay! [ts - Time.now, MAX_DEADLINE.to_i].min
53
+ message.delay! [(ts - Time.now).ceil, MAX_DEADLINE.to_i].min
57
54
  end
58
55
  else
59
56
  _process message
@@ -1,4 +1,5 @@
1
1
  require 'active_job'
2
+ require 'google/cloud/pubsub'
2
3
 
3
4
  module ActiveJob
4
5
  module GoogleCloudPubsub
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activejob-google_cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-01 00:00:00.000000000 Z
11
+ date: 2017-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activejob
@@ -128,7 +128,7 @@ files:
128
128
  - bin/setup
129
129
  - exe/activejob-google_cloud_pubsub-worker
130
130
  - lib/active_job/google_cloud_pubsub/adapter.rb
131
- - lib/active_job/google_cloud_pubsub/naming.rb
131
+ - lib/active_job/google_cloud_pubsub/pubsub_extension.rb
132
132
  - lib/active_job/google_cloud_pubsub/version.rb
133
133
  - lib/active_job/google_cloud_pubsub/worker.rb
134
134
  - lib/activejob-google_cloud_pubsub.rb
@@ -1,13 +0,0 @@
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