ost_runner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eee3e8bbf9917f4ef52aa897c4d5afb856a81ba5
4
+ data.tar.gz: a6864c1952732f1a4520a014405548fdf67a282a
5
+ SHA512:
6
+ metadata.gz: d81fd0ccc7e899b2c37cd01097520549efd1ab4757498de7ec16d681e4bb89a897c9f8dfe060b0edb280d660ac4c167f1fc5d63a46e3c5170d690f8be07562c8
7
+ data.tar.gz: 1fa7d4b9d39fb281dae8fbbe69bf19b4098972f00517ab0c063bfd9268793a7ca967f1dc884d409c51c490163518722e2b895e5f7d06833511389a5dddcab43f
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activejob', require: false
4
+ gem 'activesupport', require: false
5
+ gem 'guard-minitest', require: false
6
+ gem 'guard', require: false
7
+ gem 'minitest-reporters', require: false
8
+ gem 'rubocop', require: false
9
+ gem 'rails', require: false
@@ -0,0 +1,5 @@
1
+ guard :minitest, include: ['lib'] do
2
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[2]}_test.rb" }
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sebastian Rabuini
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.
@@ -0,0 +1,37 @@
1
+ # OstRunner
2
+
3
+ OstRunner is an ActiveJob QueueAdapter to run ActiveJob using my preferred
4
+ queueing backend: Ost.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ost_runner'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ You can easily set your queuing backend:
21
+
22
+ ```ruby
23
+ # config/application.rb
24
+ module YourApp
25
+ class Application < Rails::Application
26
+ config.active_job.queue_adapter = :ost
27
+ end
28
+ end
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/srabuini/ost_runner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,17 @@
1
+ require 'active_support'
2
+
3
+ module ActiveJob
4
+ module QueueAdapters
5
+ class OstAdapter
6
+ def enqueue(job)
7
+ Ost[job.queue_name] << job.serialize.to_json
8
+ end
9
+
10
+ def enqueue_at(*)
11
+ raise NotImplementedError, 'Use a queueing backend to enqueue jobs in'\
12
+ ' the future. Read more at'\
13
+ ' http://guides.rubyonrails.org/active_job_basics.html'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require 'ost_runner/version'
2
+ require 'ost_runner/worker'
3
+ require 'active_job/queue_adapters/ost_adapter'
4
+
5
+ class OstRunner::WorkerTask < Rails::Railtie
6
+ rake_tasks do
7
+ load 'ost_runner/tasks/worker.rake'
8
+ end
9
+ end
@@ -0,0 +1,64 @@
1
+ stop = proc do
2
+ if defined?(Ost)
3
+ Ost.stop
4
+ puts "\nStopping Ost..."
5
+ else
6
+ exit 0
7
+ end
8
+ end
9
+
10
+ trap(:INT, &stop)
11
+ trap(:TERM, &stop)
12
+
13
+ namespace :ost_runner do
14
+ def pid_path
15
+ Rails.root.join('tmp/pids/ost_runner.pid')
16
+ end
17
+
18
+ def queue
19
+ ENV['ost_runner_queue'] || Rails.application.class.parent_name.downcase
20
+ end
21
+
22
+ def pool
23
+ ENV['ost_runner_pool'] || 1
24
+ end
25
+
26
+ desc 'Listens to and executes ActiveJob Ost jobs'
27
+ task start: :environment do
28
+ OstRunner::Worker.new(queue, pool: pool).start
29
+ end
30
+
31
+ desc 'Listens to and executes ActiveJob Ost jobs as a daemon'
32
+ task start_as_daemon: :environment do
33
+ Process.daemon(true)
34
+
35
+ File.open(pid_path, File::RDWR | File::EXCL | File::CREAT, 0o666) do |f|
36
+ f.write(Process.pid)
37
+ end
38
+
39
+ at_exit do
40
+ File.delete(pid_path) if File.exist?(pid_path)
41
+ end
42
+
43
+ Rake::Task['ost_runner:start'].invoke
44
+ end
45
+
46
+ desc 'Stops a worker running as a daemon'
47
+ task stop: :environment do
48
+ pid = Integer(File.read(pid_path).chomp)
49
+
50
+ running = true
51
+
52
+ Process.kill(:TERM, pid)
53
+
54
+ while running
55
+ begin
56
+ Process.kill(0, pid)
57
+ running = true
58
+ rescue Errno::ESRCH
59
+ running = false
60
+ File.delete(pid_path) if File.exist?(pid_path)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module OstRunner
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,26 @@
1
+ module OstRunner
2
+ class Worker
3
+ attr_reader :queue_name, :size
4
+
5
+ def initialize(queue_name, options = {})
6
+ @queue_name = queue_name
7
+ @daemon = options[:daemon]
8
+ @size = options[:pool] || 1
9
+ end
10
+
11
+ def start
12
+ Array.new(size) do
13
+ Thread.new do
14
+ Ost[queue_name].each do |msg|
15
+ self.class.execute(msg)
16
+ end
17
+ end
18
+ end.each(&:join)
19
+ end
20
+
21
+ def self.execute(msg)
22
+ job_data = ActiveSupport::JSON.decode(msg)
23
+ ActiveJob::Base.execute(job_data)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ require 'ost_runner/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'ost_runner'
5
+ spec.version = OstRunner::VERSION
6
+ spec.authors = ['Sebastian Rabuini']
7
+ spec.email = ['sebas@wasabitlabs.com']
8
+
9
+ spec.summary = 'ActiveJob Ost Worker'
10
+ spec.description = 'Use Ost as your ActiveJob backend'
11
+ spec.homepage = 'https://github.com/srabuini/ost_runner.rb'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'ost', '~> 1.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.15'
23
+ spec.add_development_dependency 'rake', '~> 12.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.10'
25
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ost_runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Rabuini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ost
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.10'
69
+ description: Use Ost as your ActiveJob backend
70
+ email:
71
+ - sebas@wasabitlabs.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Guardfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - lib/active_job/queue_adapters/ost_adapter.rb
82
+ - lib/ost_runner.rb
83
+ - lib/ost_runner/tasks/worker.rake
84
+ - lib/ost_runner/version.rb
85
+ - lib/ost_runner/worker.rb
86
+ - ost_runner.gemspec
87
+ homepage: https://github.com/srabuini/ost_runner.rb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.5.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: ActiveJob Ost Worker
111
+ test_files: []