passive_job 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1 -0
  3. data/MIT-LICENSE +43 -0
  4. data/README.md +41 -0
  5. data/lib/active_job.rb +40 -0
  6. data/lib/active_job/arguments.rb +81 -0
  7. data/lib/active_job/base.rb +21 -0
  8. data/lib/active_job/callbacks.rb +144 -0
  9. data/lib/active_job/configured_job.rb +16 -0
  10. data/lib/active_job/core.rb +89 -0
  11. data/lib/active_job/enqueuing.rb +75 -0
  12. data/lib/active_job/execution.rb +41 -0
  13. data/lib/active_job/gem_version.rb +15 -0
  14. data/lib/active_job/logging.rb +102 -0
  15. data/lib/active_job/queue_adapter.rb +33 -0
  16. data/lib/active_job/queue_adapters.rb +17 -0
  17. data/lib/active_job/queue_adapters/backburner_adapter.rb +26 -0
  18. data/lib/active_job/queue_adapters/delayed_job_adapter.rb +23 -0
  19. data/lib/active_job/queue_adapters/inline_adapter.rb +15 -0
  20. data/lib/active_job/queue_adapters/qu_adapter.rb +29 -0
  21. data/lib/active_job/queue_adapters/que_adapter.rb +23 -0
  22. data/lib/active_job/queue_adapters/queue_classic_adapter.rb +40 -0
  23. data/lib/active_job/queue_adapters/resque_adapter.rb +41 -0
  24. data/lib/active_job/queue_adapters/sidekiq_adapter.rb +35 -0
  25. data/lib/active_job/queue_adapters/sneakers_adapter.rb +34 -0
  26. data/lib/active_job/queue_adapters/sucker_punch_adapter.rb +25 -0
  27. data/lib/active_job/queue_adapters/test_adapter.rb +40 -0
  28. data/lib/active_job/queue_name.rb +39 -0
  29. data/lib/active_job/railtie.rb +23 -0
  30. data/lib/active_job/test_case.rb +7 -0
  31. data/lib/active_job/test_helper.rb +196 -0
  32. data/lib/active_job/version.rb +8 -0
  33. data/lib/active_support/date.rb +4 -0
  34. data/lib/active_support/rails.rb +17 -0
  35. data/lib/active_support/testing/autorun.rb +3 -0
  36. data/lib/global_id.rb +6 -0
  37. data/lib/global_id/global_id.rb +93 -0
  38. data/lib/global_id/identification.rb +13 -0
  39. data/lib/global_id/locator.rb +83 -0
  40. data/lib/passive_job.rb +1 -0
  41. data/lib/rails/generators/job/job_generator.rb +24 -0
  42. data/lib/rails/generators/job/templates/job.rb +9 -0
  43. metadata +136 -0
@@ -0,0 +1,8 @@
1
+ require_relative 'gem_version'
2
+
3
+ module ActiveJob
4
+ # Returns the version of the currently loaded Active Job as a <tt>Gem::Version</tt>
5
+ def self.version
6
+ gem_version
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_support/core_ext/date/acts_like'
2
+ require 'active_support/core_ext/date/calculations'
3
+ require 'active_support/core_ext/date/conversions'
4
+ require 'active_support/core_ext/date/zones'
@@ -0,0 +1,17 @@
1
+ # Defines Object#blank? and Object#present?.
2
+ require 'active_support/core_ext/object/blank'
3
+
4
+ # Rails own autoload, eager_load, etc.
5
+ require 'active_support/dependencies/autoload'
6
+
7
+ # Support for ClassMethods and the included macro.
8
+ require 'active_support/concern'
9
+
10
+ # Defines Class#class_attribute.
11
+ require 'active_support/core_ext/class/attribute'
12
+
13
+ # Defines Module#delegate.
14
+ require 'active_support/core_ext/module/delegation'
15
+
16
+ # Defines ActiveSupport::Deprecation.
17
+ require 'active_support/deprecation'
@@ -0,0 +1,3 @@
1
+ gem 'minitest'
2
+
3
+ require 'minitest/autorun'
data/lib/global_id.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'global_id/global_id'
2
+
3
+ class GlobalID
4
+ autoload :Locator, 'global_id/locator'
5
+ autoload :Identification, 'global_id/identification'
6
+ end
@@ -0,0 +1,93 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/string/inflections' # For #model_class constantize
3
+ require 'active_support/core_ext/array/access'
4
+ require 'active_support/core_ext/object/try' # For #find
5
+ require 'uri'
6
+
7
+ class GlobalID
8
+ class << self
9
+ attr_reader :app
10
+
11
+ def create(model, options = {})
12
+ app = options.fetch :app, GlobalID.app
13
+ raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
14
+ new URI("gid://#{app}/#{model.class.name}/#{model.id}"), options
15
+ end
16
+
17
+ def find(gid, options = {})
18
+ parse(gid, options).try(:find, options)
19
+ end
20
+
21
+ def parse(gid, options = {})
22
+ gid.is_a?(self) ? gid : new(gid, options)
23
+ rescue URI::Error
24
+ parse_encoded_gid(gid, options)
25
+ end
26
+
27
+ def app=(app)
28
+ @app = validate_app(app)
29
+ end
30
+
31
+ def validate_app(app)
32
+ URI.parse('gid:///').hostname = app
33
+ rescue URI::InvalidComponentError
34
+ raise ArgumentError, 'Invalid app name. ' \
35
+ 'App names must be valid URI hostnames: alphanumeric and hyphen characters only.'
36
+ end
37
+
38
+ private
39
+ def parse_encoded_gid(gid, options)
40
+ new(Base64.urlsafe_decode64(repad_gid(gid)), options) rescue nil
41
+ end
42
+
43
+ # We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
44
+ def repad_gid(gid)
45
+ padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
46
+ gid + ('=' * padding_chars)
47
+ end
48
+ end
49
+
50
+ attr_reader :uri, :app, :model_name, :model_id
51
+
52
+ def initialize(gid, options = {})
53
+ extract_uri_components gid
54
+ end
55
+
56
+ def find(options = {})
57
+ Locator.locate self, options
58
+ end
59
+
60
+ def model_class
61
+ model_name.constantize
62
+ end
63
+
64
+ def ==(other)
65
+ other.is_a?(GlobalID) && @uri == other.uri
66
+ end
67
+
68
+ def to_s
69
+ @uri.to_s
70
+ end
71
+
72
+ def to_param
73
+ # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
74
+ Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
75
+ end
76
+
77
+ private
78
+ PATH_REGEXP = %r(\A/([^/]+)/([^/]+)\z)
79
+
80
+ # Pending a URI::GID to handle validation
81
+ def extract_uri_components(gid)
82
+ @uri = gid.is_a?(URI) ? gid : URI.parse(gid)
83
+ raise URI::BadURIError, "Not a gid:// URI scheme: #{@uri.inspect}" unless @uri.scheme == 'gid'
84
+
85
+ if @uri.path =~ PATH_REGEXP
86
+ @app = @uri.host
87
+ @model_name = $1
88
+ @model_id = $2
89
+ else
90
+ raise URI::InvalidURIError, "Expected a URI like gid://app/Person/1234: #{@uri.inspect}"
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_support/concern'
2
+
3
+ class GlobalID
4
+ module Identification
5
+ extend ActiveSupport::Concern
6
+
7
+ def to_global_id
8
+ @global_id ||= GlobalID.create(self)
9
+ end
10
+ alias to_gid to_global_id
11
+
12
+ end
13
+ end
@@ -0,0 +1,83 @@
1
+ class GlobalID
2
+ module Locator
3
+ class << self
4
+ # Takes either a GlobalID or a string that can be turned into a GlobalID
5
+ #
6
+ # Options:
7
+ # * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
8
+ # allowed to be located. Passing one or more classes limits instances of returned
9
+ # classes to those classes or their subclasses. Passing one or more modules in limits
10
+ # instances of returned classes to those including that module. If no classes or
11
+ # modules match, +nil+ is returned.
12
+ def locate(gid, options = {})
13
+ if gid = GlobalID.parse(gid)
14
+ locator_for(gid).locate gid if find_allowed?(gid.model_class, options[:only])
15
+ end
16
+ end
17
+
18
+ # Tie a locator to an app.
19
+ # Useful when different apps collaborate and reference each others' Global IDs.
20
+ #
21
+ # The locator can be either a block or a class.
22
+ #
23
+ # Using a block:
24
+ #
25
+ # GlobalID::Locator.use :foo do |gid|
26
+ # FooRemote.const_get(gid.model_name).find(gid.model_id)
27
+ # end
28
+ #
29
+ # Using a class:
30
+ #
31
+ # GlobalID::Locator.use :bar, BarLocator.new
32
+ #
33
+ # class BarLocator
34
+ # def locate(gid)
35
+ # @search_client.search name: gid.model_name, id: gid.model_id
36
+ # end
37
+ # end
38
+ def use(app, locator = nil, &locator_block)
39
+ raise ArgumentError, 'No locator provided. Pass a block or an object that responds to #locate.' unless locator || block_given?
40
+
41
+ GlobalID.validate_app(app)
42
+
43
+ @locators[normalize_app(app)] = locator || BlockLocator.new(locator_block)
44
+ end
45
+
46
+ private
47
+ def locator_for(gid)
48
+ @locators.fetch(normalize_app(gid.app)) { default_locator }
49
+ end
50
+
51
+ def find_allowed?(model_class, only = nil)
52
+ only ? Array(only).any? { |c| model_class <= c } : true
53
+ end
54
+
55
+ def normalize_app(app)
56
+ app.to_s.downcase
57
+ end
58
+ end
59
+
60
+ private
61
+ @locators = {}
62
+
63
+ class ActiveRecordFinder
64
+ def locate(gid)
65
+ gid.model_class.find gid.model_id
66
+ end
67
+ end
68
+
69
+ def self.default_locator
70
+ ActiveRecordFinder.new
71
+ end
72
+
73
+ class BlockLocator
74
+ def initialize(block)
75
+ @locator = block
76
+ end
77
+
78
+ def locate(gid)
79
+ @locator.call(gid)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1 @@
1
+ require 'active_job' if !defined?(ActiveJob)
@@ -0,0 +1,24 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Rails
4
+ module Generators # :nodoc:
5
+ class JobGenerator < Rails::Generators::NamedBase # :nodoc:
6
+ desc 'This generator creates an active job file at app/jobs'
7
+
8
+ class_option :queue, type: :string, default: 'default', desc: 'The queue name for the generated job'
9
+
10
+ check_class_collision suffix: 'Job'
11
+
12
+ hook_for :test_framework
13
+
14
+ def self.default_generator_root
15
+ File.dirname(__FILE__)
16
+ end
17
+
18
+ def create_job_file
19
+ template 'job.rb', File.join('app/jobs', class_path, "#{file_name}_job.rb")
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Job < ActiveJob::Base
3
+ queue_as :<%= options[:queue] %>
4
+
5
+ def perform(*args)
6
+ # Do something later
7
+ end
8
+ end
9
+ <% end -%>
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passive_job
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Trung Lê
8
+ - David Heinemeier Hansson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.2'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 3.2.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '3.2'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ - !ruby/object:Gem::Dependency
35
+ name: bundler
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Declare job classes that can be run by a variety of queueing backends.
63
+ email:
64
+ - trung.le@ruby-journal
65
+ - david@loudthinking.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - CHANGELOG.md
71
+ - MIT-LICENSE
72
+ - README.md
73
+ - lib/active_job.rb
74
+ - lib/active_job/arguments.rb
75
+ - lib/active_job/base.rb
76
+ - lib/active_job/callbacks.rb
77
+ - lib/active_job/configured_job.rb
78
+ - lib/active_job/core.rb
79
+ - lib/active_job/enqueuing.rb
80
+ - lib/active_job/execution.rb
81
+ - lib/active_job/gem_version.rb
82
+ - lib/active_job/logging.rb
83
+ - lib/active_job/queue_adapter.rb
84
+ - lib/active_job/queue_adapters.rb
85
+ - lib/active_job/queue_adapters/backburner_adapter.rb
86
+ - lib/active_job/queue_adapters/delayed_job_adapter.rb
87
+ - lib/active_job/queue_adapters/inline_adapter.rb
88
+ - lib/active_job/queue_adapters/qu_adapter.rb
89
+ - lib/active_job/queue_adapters/que_adapter.rb
90
+ - lib/active_job/queue_adapters/queue_classic_adapter.rb
91
+ - lib/active_job/queue_adapters/resque_adapter.rb
92
+ - lib/active_job/queue_adapters/sidekiq_adapter.rb
93
+ - lib/active_job/queue_adapters/sneakers_adapter.rb
94
+ - lib/active_job/queue_adapters/sucker_punch_adapter.rb
95
+ - lib/active_job/queue_adapters/test_adapter.rb
96
+ - lib/active_job/queue_name.rb
97
+ - lib/active_job/railtie.rb
98
+ - lib/active_job/test_case.rb
99
+ - lib/active_job/test_helper.rb
100
+ - lib/active_job/version.rb
101
+ - lib/active_support/date.rb
102
+ - lib/active_support/rails.rb
103
+ - lib/active_support/testing/autorun.rb
104
+ - lib/global_id.rb
105
+ - lib/global_id/global_id.rb
106
+ - lib/global_id/identification.rb
107
+ - lib/global_id/locator.rb
108
+ - lib/passive_job.rb
109
+ - lib/rails/generators/job/job_generator.rb
110
+ - lib/rails/generators/job/templates/job.rb
111
+ homepage: https://github.com/ruby-journal/passive_job
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 1.9.3
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Job framework with pluggable queues.
135
+ test_files: []
136
+ has_rdoc: