resque-job_logger 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +1 -0
- data/lib/resque/plugins/job_logger/formatter.rb +11 -0
- data/lib/resque/plugins/job_logger/job_logger.rb +46 -0
- data/lib/resque/plugins/job_logger/version.rb +7 -0
- data/lib/resque-job_logger.rb +1 -0
- data/resque-job_logger.gemspec +23 -0
- data/spec/resque-job_logger_spec.rb +42 -0
- data/spec/spec_helper.rb +21 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Todd Thomas
|
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,49 @@
|
|
1
|
+
# Resque Job Logger
|
2
|
+
|
3
|
+
Resque Job Logger adds log entries which indicate the start and elapsed
|
4
|
+
run time of each job's perform method, like so:
|
5
|
+
|
6
|
+
[2013-03-20T17:28:34.316202000] 77415: [INFO] LoggedJob([1127520]) started
|
7
|
+
[2013-03-20T17:28:34.316268000] 77415: [INFO] summing 1..1127520
|
8
|
+
[2013-03-20T17:28:34.357921000] 77415: [INFO] LoggedJob([1127520]) completed in 0.041629 seconds
|
9
|
+
|
10
|
+
(The middle entry is supplied by the example job's perform method.)
|
11
|
+
|
12
|
+
This example also shows the style of resque-job_logger's own formatter,
|
13
|
+
Resque::Plugins::JobLogger::Formatter (see Usage below).
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'resque-job_logger'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install resque-job_logger
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Just bundle it! Or if you're not using bundler, just require it:
|
32
|
+
|
33
|
+
require 'resque-job_logger'
|
34
|
+
|
35
|
+
resque-job_logger includes a resque before_fork hook which automatically adds its
|
36
|
+
features to each of your jobs. This may be a bad thing.
|
37
|
+
|
38
|
+
resque-job_logger also has its own formatter which it sets on
|
39
|
+
Resque.logger by default. To use another formatter do
|
40
|
+
|
41
|
+
Resque.logger.formatter = MyFavoriteFormatter.new
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'formatter'
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
module Resque::Plugins
|
5
|
+
module JobLogger
|
6
|
+
def around_perform_log_job(*args)
|
7
|
+
logger.info "#{self}(#{args}) started"
|
8
|
+
time = Benchmark.realtime do
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
logger.info "#{self}(#{args}) completed in #{time} seconds"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.enabled?
|
15
|
+
@enabled ||= true
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.enabled=(enabled)
|
19
|
+
@enabled = enabled
|
20
|
+
end
|
21
|
+
|
22
|
+
def logger=(logger)
|
23
|
+
@@logger = logger
|
24
|
+
end
|
25
|
+
|
26
|
+
def logger
|
27
|
+
@@logger ||= Resque.logger
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Resque.before_first_fork do
|
32
|
+
# You get our awesome formatter by default if you're using Resque.logger.
|
33
|
+
Resque.logger.formatter = Resque::Plugins::JobLogger::Formatter.new
|
34
|
+
end
|
35
|
+
|
36
|
+
# Extend job class dynamically for each instance (if enabled).
|
37
|
+
# For some reason extending the job class when it is defined does not
|
38
|
+
# seem to be effective at the point at which the worker runs an instance
|
39
|
+
# of the job.
|
40
|
+
Resque.before_fork do |job|
|
41
|
+
if Resque::Plugins::JobLogger.enabled? &&
|
42
|
+
!(job.payload_class.kind_of?(Resque::Plugins::JobLogger))
|
43
|
+
job.payload_class.send(:extend, Resque::Plugins::JobLogger)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'resque/plugins/job_logger/job_logger'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resque/plugins/job_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "resque-job_logger"
|
8
|
+
gem.version = Resque::Plugins::JobLogger::VERSION
|
9
|
+
gem.authors = ["Todd Thomas"]
|
10
|
+
gem.email = ["todd.thomas@openlogic.com"]
|
11
|
+
gem.description = %q{A resque plugin which logs the time it takes a job's perform method to run.}
|
12
|
+
gem.summary = %q{A resque plugin which logs the time it takes a job's perform method to run.}
|
13
|
+
gem.homepage = "https://github.com/toddthomas/resque-job_logger"
|
14
|
+
|
15
|
+
gem.add_dependency "resque", "~> 1.23.1"
|
16
|
+
|
17
|
+
gem.add_development_dependency "rspec"
|
18
|
+
|
19
|
+
gem.files = `git ls-files`.split($/)
|
20
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
21
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Resque::Plugins::JobLogger do
|
4
|
+
include PerformJob
|
5
|
+
|
6
|
+
it "complies with recommended practices" do
|
7
|
+
expect { Resque::Plugin.lint(Resque::Plugins::JobLogger) }.to_not raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "supplies the around_perform_log_job hook" do
|
11
|
+
LoggedJob.methods.should include :around_perform_log_job
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:logger) { Logger.new('log') }
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
LoggedJob.logger = logger
|
18
|
+
LoggedJob.logger.formatter = Resque::Plugins::JobLogger::Formatter.new
|
19
|
+
end
|
20
|
+
|
21
|
+
after(:each) do
|
22
|
+
logger.close
|
23
|
+
File.unlink('log')
|
24
|
+
end
|
25
|
+
|
26
|
+
let(:last) { rand(1_000_000..1_500_000) }
|
27
|
+
|
28
|
+
it "logs job class and args at start of perform" do
|
29
|
+
perform_job(LoggedJob, last)
|
30
|
+
IO.read('log').should match /LoggedJob\(\[#{last}\]\) started/
|
31
|
+
end
|
32
|
+
|
33
|
+
it "writes elapsed run time to log" do
|
34
|
+
perform_job(LoggedJob, last)
|
35
|
+
IO.read('log').should match /LoggedJob\(\[#{last}\]\) completed in [0-9\.e-]+ seconds/
|
36
|
+
end
|
37
|
+
|
38
|
+
it "allows arbitrary log messages to be written via the logger method" do
|
39
|
+
perform_job(LoggedJob, last)
|
40
|
+
IO.read('log').should match /summing 1..#{last}/
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'resque'
|
2
|
+
require 'resque-job_logger'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Helper to perform job classes (borrowed from resque's test_helper.rb).
|
6
|
+
#
|
7
|
+
module PerformJob
|
8
|
+
def perform_job(klass, *args)
|
9
|
+
resque_job = Resque::Job.new(:testqueue, 'class' => klass, 'args' => args)
|
10
|
+
resque_job.perform
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class LoggedJob
|
15
|
+
extend Resque::Plugins::JobLogger
|
16
|
+
|
17
|
+
def self.perform(last)
|
18
|
+
logger.info "summing 1..#{last}"
|
19
|
+
(1..last).reduce(:+)
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-job_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Todd Thomas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: resque
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.23.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.23.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A resque plugin which logs the time it takes a job's perform method to
|
47
|
+
run.
|
48
|
+
email:
|
49
|
+
- todd.thomas@openlogic.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/resque-job_logger.rb
|
60
|
+
- lib/resque/plugins/job_logger/formatter.rb
|
61
|
+
- lib/resque/plugins/job_logger/job_logger.rb
|
62
|
+
- lib/resque/plugins/job_logger/version.rb
|
63
|
+
- resque-job_logger.gemspec
|
64
|
+
- spec/resque-job_logger_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: https://github.com/toddthomas/resque-job_logger
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.25
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A resque plugin which logs the time it takes a job's perform method to run.
|
90
|
+
test_files:
|
91
|
+
- spec/resque-job_logger_spec.rb
|
92
|
+
- spec/spec_helper.rb
|