gps-job 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
+ SHA256:
3
+ metadata.gz: 502f52d768ebf9e42c239b93e3bd0b544038b0141166453514fca5b79a938abf
4
+ data.tar.gz: ff39db2c0858766e4f13f54122a3a94535e4292e392511f25f8b212dc1614082
5
+ SHA512:
6
+ metadata.gz: 69de08171bd39098566e0ffcf5dcf853c49c919d7e8068d6ac259ca12fb594991744a132f5d7f85b2571dea35ba7d5a65c537575722adb698efa8af123cdff9b
7
+ data.tar.gz: 980b69efaf87c9f2b62f7d4511650ea867e6b0a04451b712ae97d4b5e510dd34ca752fd090bc9db66969191534f6eb4534e0837f7d1ece0711047eb418741013
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Victor Antoniazzi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Google Pub/Sub job
2
+
3
+ Google Cloud Pub/Sub adapter and worker for ActiveJob.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ gem 'gps-job'
9
+ ```
10
+
11
+ First, change the ActiveJob backend.
12
+
13
+ ``` ruby
14
+ require 'gps/job/adapter'
15
+
16
+ Rails.application.config.active_job.queue_adapter = :google_cloud_pubsub
17
+ ```
18
+
19
+ Start the worker
20
+
21
+ ``` sh
22
+ $ rake gps:worker
23
+ ```
24
+
25
+ Write the Job class and code to use it.
26
+
27
+ ``` ruby
28
+ class MyJob < ApplicationJob
29
+ def perform(name)
30
+ puts "How are you?"
31
+ end
32
+ end
33
+ ```
34
+
35
+ ## Google Pub/Sub Emulator
36
+
37
+ https://cloud.google.com/pubsub/docs/emulator
38
+
39
+ ``` sh
40
+ $ gcloud beta emulators pubsub start
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ ``` sh
46
+ $ bin/setup
47
+ ```
48
+
49
+ I :heart: Open source!
50
+
51
+ [Follow github guides for forking a project](https://guides.github.com/activities/forking/)
52
+
53
+ [Follow github guides for contributing open source](https://guides.github.com/activities/contributing-to-open-source/#contributing)
54
+
55
+ ## Code Status
56
+
57
+ [![Build Status](https://travis-ci.org/vgsantoniazzi/gps-job.svg?branch=master)](https://travis-ci.org/vgsantoniazzi/gps-job)
58
+
59
+ ## License
60
+
61
+ Gem is released under the [MIT license](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'bundler/gem_tasks'
10
+ require 'rake/testtask'
11
+
12
+ import 'lib/tasks/gps/worker.rake'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'test'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = false
18
+ t.warning = false
19
+ end
20
+
21
+ task default: :test
data/lib/gps/job.rb ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'google/cloud/pubsub'
5
+ require 'gps/job/railtie'
6
+
7
+ module Gps
8
+ module Job
9
+ class << self
10
+ attr_writer :configuration, :metrics
11
+
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def metrics
17
+ @metrics ||= Metrics.new
18
+ end
19
+ end
20
+
21
+ def self.configure
22
+ self.configuration ||= Configuration.new
23
+ yield(configuration)
24
+ end
25
+
26
+ def self.topic
27
+ pubsub.topic(configuration.queue) || pubsub.create_topic(configuration.queue)
28
+ end
29
+
30
+ def self.pubsub
31
+ Google::Cloud.new(
32
+ Gps::Job.configuration.project_id,
33
+ Gps::Job.configuration.credentials
34
+ ).pubsub
35
+ end
36
+
37
+ autoload :Configuration, 'gps/job/configuration'
38
+ autoload :Metrics, 'gps/job/metrics'
39
+ autoload :Worker, 'gps/job/worker'
40
+ autoload :Adapter, 'gps/job/adapter'
41
+ end
42
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJob
4
+ module QueueAdapters
5
+ class GoogleCloudPubsubAdapter
6
+ def enqueue(job, attributes = {})
7
+ Gps::Job.configuration.logger&.info "Google Pub/Sub Enqueued job #{job.inspect}"
8
+ Gps::Job.topic.publish(job_data(job, attributes).to_json)
9
+ end
10
+
11
+ def enqueue_at(job, timestamp)
12
+ enqueue(job, timestamp: timestamp)
13
+ end
14
+
15
+ private
16
+
17
+ def job_data(job, attributes)
18
+ info = job.serialize
19
+ info['at'] = attributes[:timestamp] if attributes[:timestamp].to_i > Time.now.to_i
20
+ info
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gps
4
+ module Job
5
+ class Configuration
6
+ attr_accessor :logger, :queue, :project_id, :credentials, :event_name
7
+
8
+ def initialize
9
+ @logger = Logger.new(STDOUT)
10
+ @queue = :default
11
+ @project_id = :default
12
+ @credentials = '~/.google/keyfile.json'
13
+ @event_name = 'perform.gps_job'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gps
4
+ module Job
5
+ class Metrics
6
+ attr_accessor :total_duration, :total_count
7
+
8
+ def initialize
9
+ @total_duration = 0
10
+ @total_count = 0
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gps
4
+ module Job
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load 'tasks/gps/worker.rake'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gps
4
+ module Job
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gps
4
+ module Job
5
+ class Worker
6
+ def self.run_worker!
7
+ Gps::Job.configuration.logger&.info 'Google Pub/Sub Worker running.'
8
+ subscribe_to_active_support_instrumentation!
9
+ subscribe_to_google_pub_sub!
10
+ end
11
+
12
+ def self.subscribe_to_active_support_instrumentation!
13
+ ActiveSupport::Notifications.subscribe(Gps::Job.configuration.event_name) do |_name, started, finished, _unique_id, data|
14
+ Gps::Job.configuration.logger&.info "Finished job: #{data}"
15
+ Gps::Job.metrics.total_duration += (finished - started)
16
+ Gps::Job.metrics.total_count += 1
17
+ end
18
+ end
19
+
20
+ def self.subscribe_to_google_pub_sub!
21
+ subscription.listen do |message|
22
+ data = JSON.parse(message.data)
23
+ if perform_now?(data)
24
+ message.acknowledge!
25
+ perform_job(data)
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.perform_now?(data)
31
+ data['at'].to_i < Time.now.to_i
32
+ end
33
+
34
+ def self.perform_job(data)
35
+ Gps::Job.configuration.logger&.info "Started Job: #{data}"
36
+ ActiveSupport::Notifications.instrument(Gps::Job.configuration.event_name, data) do
37
+ data.fetch('job_class').constantize.perform_now(*data.fetch('arguments'))
38
+ end
39
+ end
40
+
41
+ def self.subscription
42
+ Gps::Job.topic.subscription(Gps::Job.configuration.queue) ||
43
+ Gps::Job.topic.subscribe(Gps::Job.configuration.queue)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'gps/job'
4
+
5
+ namespace :gps do
6
+ task :worker do
7
+ Rails.application.initialize! if defined?(Rails)
8
+ Gps::Job::Worker.run_worker!
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gps-job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Antoniazzi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.19.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.19.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google-cloud-pubsub
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.21.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.21.0
55
+ description: Google Pub/Sub Active Job Adapter
56
+ email:
57
+ - vgsantoniazzi@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/gps/job.rb
66
+ - lib/gps/job/adapter.rb
67
+ - lib/gps/job/configuration.rb
68
+ - lib/gps/job/metrics.rb
69
+ - lib/gps/job/railtie.rb
70
+ - lib/gps/job/version.rb
71
+ - lib/gps/job/worker.rb
72
+ - lib/tasks/gps/worker.rake
73
+ homepage: http://github.com/vgsantoniazzi/gps-job
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.10
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Google Pub/Sub Active Job Adapter
97
+ test_files: []