delayed-plugins-reporting 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82b5554cc8efd43c1b3e7d3c5b2fbbcc20d81b7b
4
+ data.tar.gz: 2626a29c9cd6e91814011b3f11a56dd30fcb0636
5
+ SHA512:
6
+ metadata.gz: 31bce618f7613ba8f8ed3b11169aff960d9e3b87575c301b018d433a2d7feec3ea86305c88c1f8f9d60be241fc5897d8878a9d7109cf92944675e1e959bb83ec
7
+ data.tar.gz: 08bd37d91f865fc6c29a42de2a0b8bb8aea68da3116a0c860c228741bd37ac5881ebfc3c4fed7b3fd47c5187a6ded3a8782f71feebd1afeeba5c620516331987
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *~
16
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delayed-plugins-reporting.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yusuke KUOKA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Delayed::Plugins::Reporting
2
+
3
+ A [Delayed::Job](https://github.com/collectiveidea/delayed_job) plugin to report lifecycle events like job invocations,
4
+ failures, retries to log files or monitoring services like Datadog.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'delayed-plugins-reporting'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install delayed-plugins-reporting
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'delayed-plugins-reporting'
26
+ require 'delayed-plugins-reporting-<reporting plugin of your choice>'
27
+
28
+ # Configure the plugin like so:
29
+ Delayed::Plugins::Reporting.options = {
30
+ reporter: {
31
+ class: Delayed::Plugins::Reporting::DatadogJobReporter,
32
+ prefix: 'crowdworks.delayed_job.lifecycle'
33
+ }
34
+ }
35
+
36
+ # Register the plugin like so:
37
+ Delayed::Worker.plugins << Delayed::Plugins::Reporting
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/crowdworks/delayed-plugins-reporting/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delayed-plugins-reporting/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "delayed-plugins-reporting"
8
+ spec.version = Delayed::Plugins::Reporting::VERSION
9
+ spec.authors = ["Yusuke KUOKA"]
10
+ spec.email = ["yusuke.kuoka@crowdworks.co.jp"]
11
+ spec.summary = %q{A Delayed::Job plugin to report lifecycle events to log files or monitoring services, etc.}
12
+ spec.homepage = "https://github.com/crowdworks/delayed-plugins-reporting"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'delayed_job'
2
+ require 'delayed/plugin'
3
+
4
+ class Delayed::Plugins::Reporting < ::Delayed::Plugin
5
+ require 'delayed-plugins-reporting/version'
6
+ require 'delayed-plugins-reporting/context'
7
+
8
+ callbacks do |lifecycle|
9
+ with_context do |context|
10
+ lifecycle.after(:failure) do |worker, job|
11
+ context.after_failure(worker, job)
12
+ end
13
+ end
14
+ end
15
+
16
+ class << self
17
+ def with_context
18
+ yield Context.create(**options)
19
+ end
20
+
21
+ def options=(options)
22
+ @options = options
23
+ end
24
+
25
+ def options
26
+ @options ||= {}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ require 'delayed-plugins-reporting/job_listener'
2
+ require 'delayed-plugins-reporting/pretty_json_job_formatter'
3
+
4
+ class Delayed::Plugins::Reporting
5
+ class Context
6
+ attr_reader :listener
7
+
8
+ def initialize(listener:)
9
+ @listener = listener
10
+ end
11
+
12
+ def after_failure(worker, job)
13
+ max_attempts = worker.max_attempts(job)
14
+
15
+ if max_attempts > 1 && job.attempts == max_attempts
16
+ listener.max_attempts_exceeded(job)
17
+ end
18
+ end
19
+
20
+ class << self
21
+ def create(reporter: {}, formatter: {}, listener: {})
22
+ formatter_options = formatter.dup
23
+ listener_options = listener.dup
24
+ reporter_options = reporter.dup
25
+
26
+ formatter_class = formatter_options.delete(:class) || PrettyJsonJobFormatter
27
+ listener_class = listener_options.delete(:class) || JobListener
28
+ reporter_class = reporter_options.delete(:class) || NullReporter
29
+
30
+ new(
31
+ listener: listener_class.new(
32
+ reporter: reporter_class.new(
33
+ formatter: formatter_class.new(
34
+ **formatter_options
35
+ ),
36
+ **reporter_options
37
+ ),
38
+ **listener_options
39
+ )
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,19 @@
1
+ class Delayed::Plugins::Reporting
2
+ class JobListener
3
+ def initialize(reporter:)
4
+ @reporter = reporter
5
+ end
6
+
7
+ def max_attempts_exceeded(job)
8
+ report.error :max_attempts_exceeded, job: job, message: <<-EOS.gsub!(/^\s+/, '')
9
+ The job failed `max_attempts` times and was given up retrying.
10
+ EOS
11
+ end
12
+
13
+ private
14
+
15
+ def report
16
+ @reporter
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ require 'delayed-plugins-reporting'
2
+
3
+ class Delayed::Plugins::Reporting
4
+ class NullReporter
5
+ def initialize(formatter:)
6
+ end
7
+
8
+ def error(simple_error_name, message:, job:)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ class Delayed::Plugins::Reporting
2
+ class PrettyJsonJobFormatter
3
+ INCLUDED_ATTRIBUTE_NAMES = %i| priority attempts run_at locked_at queue created_at updated_at |
4
+ INCLUDED_METHOD_NAMES = %i| max_attempts |
5
+
6
+ attr_reader :included_attribute_names
7
+ attr_reader :included_method_names
8
+
9
+ def initialize(attributes: INCLUDED_ATTRIBUTE_NAMES, methods: INCLUDED_METHOD_NAMES)
10
+ @included_attribute_names = attributes
11
+ @included_method_names = methods
12
+ end
13
+
14
+ def format(job)
15
+ as_pretty_formatted_json(job)
16
+ end
17
+
18
+ private
19
+
20
+ def as_json(job)
21
+ job.as_json(
22
+ only: included_attribute_names,
23
+ methods: included_method_names
24
+ )
25
+ end
26
+
27
+ def as_pretty_formatted_json(job)
28
+ JSON.pretty_generate(as_json(job))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ require 'delayed-plugins-reporting'
2
+
3
+ class Delayed::Plugins::Reporting
4
+ VERSION = '0.9.0'
5
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed-plugins-reporting
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke KUOKA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - yusuke.kuoka@crowdworks.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - delayed-plugins-reporting.gemspec
54
+ - lib/delayed-plugins-reporting.rb
55
+ - lib/delayed-plugins-reporting/context.rb
56
+ - lib/delayed-plugins-reporting/job_listener.rb
57
+ - lib/delayed-plugins-reporting/null_reporter.rb
58
+ - lib/delayed-plugins-reporting/pretty_json_job_formatter.rb
59
+ - lib/delayed-plugins-reporting/version.rb
60
+ homepage: https://github.com/crowdworks/delayed-plugins-reporting
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A Delayed::Job plugin to report lifecycle events to log files or monitoring
84
+ services, etc.
85
+ test_files: []
86
+ has_rdoc: