delayed_job_class_name_plugin 1.0.1

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
+ SHA1:
3
+ metadata.gz: af391dd335fc36171feaf80d26dde8cb2633eaac
4
+ data.tar.gz: 3548674074f29df3c8079d935f473f5e99735eb8
5
+ SHA512:
6
+ metadata.gz: bc3407a67d489d384c0e8ba0ee7080ceec54da5338882a3faf1dfaa5a05384ce936e78b2e51c14cf36fe53ae8744a61cae1fb487cd061de93ffbecea653d6e69
7
+ data.tar.gz: 9ba02151503d7df487006eba43f62d2cafe0ce3a1f331accfa854b0f8bb9fd4c2575aaab53a53a59acb172b48e0a6d07034f29721071cdb8f1ea05403cb97fdf
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /doc/
6
+ /pkg/
7
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delayed_job_class_name_plugin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Cyrus Eslami
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.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Delayed::ClassName Plugin
2
+
3
+ [Delayed Job](https://github.com/collectiveidea/delayed_job) records a serialized payload object to the `delayed_jobs` table. If you want to know what _kinds of_ jobs are currently enqueued, you must write complicated and brittle queries to parse the serialized payloads (ie `Delayed::Job#handler`).
4
+
5
+ This plugin adds an indexed `class_name` column to the `delayed_jobs` table. By default, it contains the stringified class name of the enqeued payload object.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'delayed_job_class_name_plugin'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install delayed_job_class_name_plugin
22
+
23
+ Run the required database migrations:
24
+
25
+ $ rails generate delayed_job_class_name_plugin:install
26
+ $ rails db:migrate
27
+
28
+ ## Usage
29
+
30
+ In certain cases, you may want to parse the payload object in a custom way to derive its "class name."
31
+
32
+ For example, [ActiveJob](http://edgeguides.rubyonrails.org/active_job_basics.html) in Rails, will always enqueue a `JobWrapper` as the payload object. This object, predictably, wraps the object that's actually doing work. By default, "JobWrapper" would be the value of `Delayed::Job#class_name`. You may find this less helpful than having `#class_name` contain the class of the _wrapped_ object.
33
+
34
+ Let's see how we can use the `#custom_parser` configuration to accomplish this:
35
+
36
+ ```ruby
37
+ # config/initializers/delayed_job.rb
38
+ Delayed::ClassName.configure do |c|
39
+ c.custom_parser = ->(job) {
40
+ if job.class == ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper
41
+ job.payload_object.job_data['job_class']
42
+ else
43
+ job.payload_object.class.name
44
+ end
45
+ }
46
+ end
47
+ ```
48
+
49
+ `Configuration#custom_parser` can take a `Proc` which receives a single parameter, `job`, and returns a `String` that will be written to `Delayed::Job#class_name` at enqueue-time.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/setup ADDED
@@ -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,27 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delayed/class_name/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'delayed_job_class_name_plugin'
8
+ spec.version = Delayed::ClassName::VERSION
9
+ spec.authors = ['Cyrus Eslami']
10
+ spec.email = ['ceslami@gmail.com']
11
+
12
+ spec.summary = 'Delayed::Job plugin to record the class name of the enqueued payload object'
13
+ spec.homepage = 'https://github.com/ceslami/delayed_job_class_name_plugin'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = Dir.glob('spec/**/*')
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '>= 2.0'
21
+
22
+ spec.add_dependency 'delayed_job', '~> 4.1', '>= 4.1.0'
23
+ spec.add_dependency 'delayed_job_active_record', '~> 4.1', '>= 4.1.0'
24
+
25
+ spec.add_development_dependency 'rake', '< 11.0'
26
+ spec.add_development_dependency 'rspec', '3.3.0'
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'delayed/class_name/configuration'
2
+ require 'delayed/class_name/plugin'
3
+ require 'delayed/class_name/version'
4
+
5
+ module Delayed
6
+ module ClassName
7
+ @configuration = Delayed::ClassName::Configuration.new
8
+
9
+ class << self
10
+ def configure
11
+ yield(configuration) if block_given?
12
+ end
13
+
14
+ def configuration
15
+ @configuration
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Delayed
2
+ module ClassName
3
+ class Configuration
4
+ attr_accessor :custom_parser
5
+
6
+ def initialize(options = {})
7
+ options.each do |key, value|
8
+ send("#{key}=", value)
9
+ end
10
+ end
11
+
12
+ def custom_parser?
13
+ !!@custom_parser
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module Delayed
2
+ module ClassName
3
+ class Plugin < Delayed::Plugin
4
+ callbacks do |lifecycle|
5
+ lifecycle.before(:enqueue) do |job|
6
+ payload_object = job.payload_object
7
+
8
+ if Delayed::ClassName.configuration.custom_parser?
9
+ job.class_name = Delayed::ClassName.configuration.custom_parser.call payload_object
10
+ else
11
+ job.class_name = payload_object.class.name
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ module Delayed
4
+ module ClassName
5
+ VERSION = '1.0.1'.freeze
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+ require 'delayed_job'
4
+ require 'delayed_job_active_record'
5
+ require 'delayed/class_name'
6
+
7
+ Delayed::Worker.plugins << Delayed::ClassName::Plugin
@@ -0,0 +1,25 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_record'
4
+
5
+ module DelayedJobClassNamePlugin
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ self.source_paths << File.join(File.dirname(__FILE__), 'templates')
10
+
11
+ def create_migration_file
12
+ migration_template('migration.rb', 'db/migrate/add_class_name_to_delayed_jobs.rb', migration_version: migration_version)
13
+ end
14
+
15
+ def self.next_migration_number(dirname)
16
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
17
+ end
18
+
19
+ private
20
+
21
+ def migration_version
22
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" if ActiveRecord::VERSION::MAJOR >= 5
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ class AddClassNameToDelayedJobs < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ add_column :delayed_jobs, :class_name, :string
4
+ add_index :delayed_jobs, :class_name
5
+ end
6
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Delayed::ClassName do
4
+ let(:job) { Delayed::Job.all.first }
5
+
6
+ context 'when there is not a custom parser' do
7
+ before do
8
+ Delayed::ClassName.configure do |c|
9
+ c.custom_parser = nil
10
+ end
11
+ end
12
+
13
+ it 'assigns the class name of the payload object to class_name' do
14
+ Delayed::Job.enqueue TestJob.new
15
+ expect(job.class_name).to eq 'TestJob'
16
+ end
17
+ end
18
+
19
+ context 'when there is a custom parser' do
20
+ before do
21
+ Delayed::ClassName.configure do |c|
22
+ c.custom_parser = ->(job) {
23
+ job.class.name + '1'
24
+ }
25
+ end
26
+ end
27
+
28
+ it 'assigns the parsed value to class_name' do
29
+ Delayed::Job.enqueue TestJob.new
30
+ expect(job.class_name).to eq 'TestJob1'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'delayed_job_class_name_plugin'
4
+
5
+ spec_dir = File.dirname(__FILE__)
6
+ Dir["#{spec_dir}/support/**/*.rb"].sort.each { |f| require f }
7
+ $LOAD_PATH << File.expand_path("support/delayed_job", __dir__)
8
+
9
+ Delayed::Worker.backend = :test
10
+
11
+ RSpec.configure do |config|
12
+ config.order = 'random'
13
+ config.before :each do
14
+ Delayed::Job.delete_all
15
+ end
16
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ # copied from https://github.com/collectiveidea/delayed_job/blob/master/spec/delayed/backend/test.rb
4
+ # add attr_accessor for class_name field
5
+ require "ostruct"
6
+
7
+ # An in-memory backend suitable only for testing. Tries to behave as if it were an ORM.
8
+ module Delayed
9
+ module Backend
10
+ module Test
11
+ class Job
12
+ attr_accessor :id
13
+ attr_accessor :priority
14
+ attr_accessor :attempts
15
+ attr_accessor :handler
16
+ attr_accessor :last_error
17
+ attr_accessor :run_at
18
+ attr_accessor :locked_at
19
+ attr_accessor :locked_by
20
+ attr_accessor :failed_at
21
+ attr_accessor :queue
22
+ attr_accessor :class_name
23
+
24
+ include Delayed::Backend::Base
25
+
26
+ cattr_accessor :id, default: 0
27
+
28
+ def initialize(hash = {})
29
+ self.attempts = 0
30
+ self.priority = 0
31
+ self.id = (self.class.id += 1)
32
+ hash.each { |k, v| send(:"#{k}=", v) }
33
+ end
34
+
35
+ @jobs = []
36
+ def self.all
37
+ @jobs
38
+ end
39
+
40
+ def self.count
41
+ all.size
42
+ end
43
+
44
+ def self.delete_all
45
+ all.clear
46
+ end
47
+
48
+ def self.create(attrs = {})
49
+ new(attrs).tap(&:save)
50
+ end
51
+
52
+ def self.create!(*args); create(*args); end
53
+
54
+ def self.clear_locks!(worker_name)
55
+ all.select { |j| j.locked_by == worker_name }.each { |j| j.locked_by = nil; j.locked_at = nil }
56
+ end
57
+
58
+ # Find a few candidate jobs to run (in case some immediately get locked by others).
59
+ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
60
+ jobs = all.select do |j|
61
+ j.run_at <= db_time_now &&
62
+ (j.locked_at.nil? || j.locked_at < db_time_now - max_run_time || j.locked_by == worker_name) &&
63
+ !j.failed?
64
+ end
65
+
66
+ jobs = jobs.select { |j| Worker.queues.include?(j.queue) } if Worker.queues.any?
67
+ jobs = jobs.select { |j| j.priority >= Worker.min_priority } if Worker.min_priority
68
+ jobs = jobs.select { |j| j.priority <= Worker.max_priority } if Worker.max_priority
69
+ jobs.sort_by { |j| [j.priority, j.run_at] }[0..limit - 1]
70
+ end
71
+
72
+ # Lock this job for this worker.
73
+ # Returns true if we have the lock, false otherwise.
74
+ def lock_exclusively!(max_run_time, worker)
75
+ now = self.class.db_time_now
76
+ if locked_by != worker
77
+ # We don't own this job so we will update the locked_by name and the locked_at
78
+ self.locked_at = now
79
+ self.locked_by = worker
80
+ end
81
+
82
+ true
83
+ end
84
+
85
+ def self.db_time_now
86
+ Time.current
87
+ end
88
+
89
+ def update_attributes(attrs = {})
90
+ attrs.each { |k, v| send(:"#{k}=", v) }
91
+ save
92
+ end
93
+
94
+ def destroy
95
+ self.class.all.delete(self)
96
+ end
97
+
98
+ def save
99
+ self.run_at ||= Time.current
100
+
101
+ self.class.all << self unless self.class.all.include?(self)
102
+ true
103
+ end
104
+
105
+ def save!; save; end
106
+
107
+ def reload
108
+ reset
109
+ self
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,4 @@
1
+ class TestJob
2
+ def perform
3
+ end
4
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_class_name_plugin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cyrus Eslami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.1.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: delayed_job_active_record
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 4.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '4.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 4.1.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "<"
58
+ - !ruby/object:Gem::Version
59
+ version: '11.0'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '11.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '='
72
+ - !ruby/object:Gem::Version
73
+ version: 3.3.0
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '='
79
+ - !ruby/object:Gem::Version
80
+ version: 3.3.0
81
+ description:
82
+ email:
83
+ - ceslami@gmail.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - ".gitignore"
89
+ - ".rspec"
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - bin/setup
95
+ - delayed_job_class_name_plugin.gemspec
96
+ - lib/delayed/class_name.rb
97
+ - lib/delayed/class_name/configuration.rb
98
+ - lib/delayed/class_name/plugin.rb
99
+ - lib/delayed/class_name/version.rb
100
+ - lib/delayed_job_class_name_plugin.rb
101
+ - lib/generators/delayed_job_class_name_plugin/install_generator.rb
102
+ - lib/generators/delayed_job_class_name_plugin/templates/migration.rb
103
+ - spec/delayed/class_name_spec.rb
104
+ - spec/spec_helper.rb
105
+ - spec/support/delayed_job/delayed/backend/test.rb
106
+ - spec/support/delayed_job/delayed/serialization/test.rb
107
+ - spec/support/test_job.rb
108
+ homepage: https://github.com/ceslami/delayed_job_class_name_plugin
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: '2.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.6.13
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Delayed::Job plugin to record the class name of the enqueued payload object
132
+ test_files:
133
+ - spec/delayed/class_name_spec.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/delayed_job/delayed/backend/test.rb
136
+ - spec/support/delayed_job/delayed/serialization/test.rb
137
+ - spec/support/test_job.rb