active_beaneater 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: cef3e6bc47d58798b948cfda4e5a627fbf2426cf
4
+ data.tar.gz: d433befd9375f671e29ff1a5179e6b650c210f32
5
+ SHA512:
6
+ metadata.gz: ec2d3fcd031b05c967927cba6e84fbecf6a2db7718955b0886bd56fe04c77cc8cf6a3fdfff51675a8f76cbf7fed0ca9877070ee418871d7dae00466b3871a979
7
+ data.tar.gz: 77c3774729f824c819d6419d16d7a3c7596adb11a736559a344400bcf8c5561cdbe650f4d15e697a766c72d12878eb139489a1d5285c5320a9123f9eb1ed421a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_beaneater.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nicolas Goy
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,67 @@
1
+ # ActiveBeaneater
2
+
3
+ Simple beanstalk backend for Rails's ActiveJob.
4
+
5
+ Require Rails 4.2 or greater
6
+
7
+ WARNING: This is early alpha, not supposed to be stable.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_beaneater'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_beaneater
24
+
25
+ ## Usage
26
+
27
+ Set your `ActiveJob` backend to active_beaneater:
28
+
29
+ In `development.rb` and/or `production.rb`:
30
+
31
+ ```ruby
32
+ config.active_job.queue_adapter = :active_beaneater
33
+ ```
34
+
35
+ The env var `BEANSTALK_URL` will be used to connect to beanstalk.
36
+
37
+ ### Configuration
38
+
39
+ You may configure `ActiveBeanter` in a rails initializer:
40
+
41
+ ```ruby
42
+ ActiveBeaneater.configure do |config|
43
+ config.client = Beaneater.new("localhost:11300") # Default to BEANSTALK_URL ENV var
44
+ config.prefix = 'yourapp' # Default to active-beaneater, can be an array, elements will be joined by a dot
45
+ end
46
+ ```
47
+
48
+ ### Running jobs
49
+
50
+ Simply issue `active_beaneater` from within your app root directory.
51
+
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_beaneater. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
@@ -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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_beaneater/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_beaneater"
8
+ spec.version = ActiveBeaneater::VERSION
9
+ spec.authors = ["Nicolas Goy"]
10
+ spec.email = ["kuon@goyman.com"]
11
+
12
+ spec.summary = %q{Simple beanstalk backend for Rails's ActiveJob.}
13
+ spec.homepage = "http://github.com/kuon/active_beaneater"
14
+ spec.license = "MIT"
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'rails', '~> 4.2'
23
+ spec.add_dependency 'beaneater', '~> 1.0'
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_beaneater"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'optparse'
5
+ require 'active_beaneater'
6
+
7
+ options = {}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: active_beaneater [options]"
11
+
12
+ opts.on("--only=NAMES", "Run only jobs on given queues") do |v|
13
+ options[:only] = v.split(',').map(&:to_sym)
14
+ end
15
+
16
+ opts.on("--exclude=NAMES", "Run jobs on all tubes except those listed") do |v|
17
+ options[:exclude] = v.split(',').map(&:to_sym)
18
+ end
19
+
20
+ opts.on("-h", "--help", "Prints this help") do
21
+ puts(opts)
22
+ exit
23
+ end
24
+ end.parse!
25
+
26
+
27
+ require "rails"
28
+ require File.expand_path("./config/environment.rb")
29
+
30
+ Rails.application.eager_load!
31
+
32
+ # Override the rails logger in development to ensure we see it
33
+ if Rails.env.development?
34
+ Rails.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDERR))
35
+ end
36
+
37
+ Rails.logger.info("Starting job worker")
38
+
39
+ ActiveBeaneater::Worker.work(options)
40
+
@@ -0,0 +1,42 @@
1
+ require "active_job/queue_adapters/active_beaneater_adapter"
2
+ require "active_beaneater/version"
3
+ require "active_beaneater/queue_options"
4
+ require "active_beaneater/worker"
5
+ require "ostruct"
6
+ require "beaneater"
7
+
8
+ module URI
9
+ class BEANSTALK < Generic
10
+ DEFAULT_PORT = 11300
11
+ end
12
+ @@schemes['BEANSTALK'] = BEANSTALK
13
+ end
14
+
15
+ module ActiveBeaneater
16
+
17
+ def self.configure
18
+
19
+ url = ENV['BEANSTALK_URL']
20
+ url ||= ['beanstalk://localhost']
21
+ uri = URI(url)
22
+
23
+
24
+ config = {
25
+ client: Beaneater.new("#{uri.host}:#{uri.port}"),
26
+ prefix: 'active-beaneater'
27
+ }
28
+
29
+ config = OpenStruct.new(config)
30
+ yield(config) if block_given?
31
+
32
+
33
+ Rails.application.config.beaneater = config.freeze
34
+ end
35
+
36
+ def self.resolve_queue_name(queue)
37
+ prefix = Rails.application.config.beaneater.prefix
38
+ prefix = [prefix] if !prefix.respond_to?(:to_ary)
39
+ (prefix + [queue]).join('.')
40
+ end
41
+
42
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveBeaneater
4
+ module QueueOptions
5
+ extend ::ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def queue_priority(pri)
10
+ self.queue_priority_number = case pri
11
+ when Integer
12
+ pri
13
+ when :low
14
+ 10000
15
+ when :high
16
+ 0
17
+ else
18
+ fails ArgumentError, 'unknown priority'
19
+ end
20
+ end
21
+
22
+ def queue_timeout_after(timeout)
23
+ self.queue_respond_timeout = timeout
24
+ end
25
+
26
+ end
27
+
28
+ included do
29
+ # Beaneater native job, this is required for touch for long running jobs
30
+ attr_accessor :native_job
31
+
32
+ class_attribute :queue_priority_number
33
+ class_attribute :queue_respond_timeout
34
+
35
+ self.queue_priority_number = 0
36
+ self.queue_respond_timeout = 1.minute
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveBeaneater
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,43 @@
1
+ module ActiveBeaneater
2
+ class Worker
3
+
4
+ def self.work(only: nil, exclude: [])
5
+ client = Rails.application.config.beaneater.client
6
+
7
+
8
+ tube_names = []
9
+
10
+ ObjectSpace.each_object(Class) do |k|
11
+ if k.ancestors.include?(ActiveJob::Base)
12
+ tube_names << ActiveBeaneater.resolve_queue_name(k.queue_name)
13
+ end
14
+ end
15
+
16
+ tube_names = only if only
17
+ tube_names.uniq!
18
+ tube_names -= exclude
19
+
20
+ tube_names.each do |name|
21
+ client.jobs.register(name) do |job|
22
+ Rails.logger.debug("Processing job on tube #{name}")
23
+ begin
24
+ perform(job)
25
+ rescue Exception => e
26
+ Rails.logger.error(e)
27
+ raise e
28
+ end
29
+ end
30
+ end
31
+
32
+ Rails.logger.info("Watching tubes [#{tube_names.join(', ')}]")
33
+ client.jobs.process!
34
+ end
35
+
36
+ def self.perform(job)
37
+ active_job = ActiveJob::Base.deserialize(MultiJson.load(job.body))
38
+ active_job.native_job = job if active_job.respond_to?(:native_job=)
39
+ active_job.perform_now
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ module ActiveJob
2
+ module QueueAdapters
3
+ # Rails.application.config.active_job.queue_adapter = :beaneater
4
+ class ActiveBeaneaterAdapter
5
+
6
+ class << self
7
+ def enqueue(job) #:nodoc:
8
+ enqueue_at(job, nil)
9
+ end
10
+
11
+ def enqueue_at(job, timestamp) #:nodoc:
12
+ delay = timestamp ? timestamp - Time.current.to_f : 0
13
+
14
+ tube = client.tubes[ActiveBeaneater.resolve_queue_name(job.queue_name)]
15
+
16
+ pri = job.respond_to?(:queue_priority_number) ?
17
+ job.queue_priority_number : 0
18
+
19
+ ttr = job.respond_to?(:queue_respond_timeout) ?
20
+ job.queue_respond_timeout : 10.minutes
21
+
22
+ tube.put(job.serialize.to_json,
23
+ delay: delay,
24
+ pri: pri,
25
+ ttr: ttr)
26
+ end
27
+
28
+ private
29
+
30
+ def client
31
+ Rails.application.config.beaneater.client
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_beaneater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Goy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: beaneater
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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
+ description:
84
+ email:
85
+ - kuon@goyman.com
86
+ executables:
87
+ - active_beaneater
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - active_beaneater.gemspec
100
+ - bin/console
101
+ - bin/setup
102
+ - exe/active_beaneater
103
+ - lib/active_beaneater.rb
104
+ - lib/active_beaneater/queue_options.rb
105
+ - lib/active_beaneater/version.rb
106
+ - lib/active_beaneater/worker.rb
107
+ - lib/active_job/queue_adapters/active_beaneater_adapter.rb
108
+ homepage: http://github.com/kuon/active_beaneater
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Simple beanstalk backend for Rails's ActiveJob.
132
+ test_files: []