log4r-exceptionable 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/dump.rdb
6
+ spec/redis-server.log
7
+ spec/redis.pid
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ # - rbx-18mode
9
+ # - rbx-19mode
10
+ script: bundle exec rspec spec
data/CHANGELOG ADDED
@@ -0,0 +1,5 @@
1
+ 0.5.0
2
+ -----
3
+
4
+ Initial release created by merging graylog2-resque and graylog2_exceptions and modifying to send to log4r
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in graylog2-resque.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Matt Conway (matt@conwaysplace.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ graylog2-resque
2
+ ===============
3
+
4
+ This gem provides failure handlers for [Resque][0] and [Rack][1] that logs all failures using [log4r][2]. It is expected that these logs will get sent elsewhere (e.g. [graylog][3]) by using log4r outputters (e.g. [log4r-gelf][4]). It adds contextual information to the log message using Log4r::MDC, which is useful if you are using log4r-gelf since it sends all of those to graylog as custom attributes.
5
+
6
+ [![Build Status](https://secure.travis-ci.org/wr0ngway/log4r-exceptionable.png)](http://travis-ci.org/wr0ngway/log4r-exceptionable)
7
+
8
+ Install
9
+ -------
10
+
11
+ gem install log4r-exceptionable
12
+ or add to your Gemfile
13
+
14
+ To use
15
+ ------
16
+
17
+ Add to some initializer code:
18
+
19
+ Log4rExceptionable::Configuration.configure do |config|
20
+ # at least one logger needs to be set
21
+ config.rack_failure_logger = "rails::SomeRackLogger"
22
+ config.resque_failure_logger = "rails::SomeResqueLogger"
23
+ end
24
+
25
+ Rails.application.config.middleware.use "Log4rExceptionable::RackFailureHandler"
26
+ Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
27
+
28
+ All failures will be logged using the given log4r logger name (or log4r logger instance if supplied instead)
29
+
30
+ Author
31
+ ------
32
+
33
+ Matt Conway :: matt@conwaysplace.com :: @mattconway
34
+
35
+ Copyright
36
+ ---------
37
+
38
+ Copyright (c) 2012 Matt Conway. See LICENSE for details.
39
+
40
+ [0]: http://github.com/defunkt/resque
41
+ [1]: http://rack.github.com/
42
+ [2]: http://log4r.rubyforge.org/
43
+ [3]: http://graylog2.org/
44
+ [4]: http://github.com/wr0ngway/log4r-gelf
45
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "log4r-exceptionable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "log4r-exceptionable"
7
+ s.version = Log4rExceptionable::VERSION
8
+ s.authors = ["Matt Conway"]
9
+ s.email = ["matt@conwaysplace.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Failure handlers for rack and resque that log failures using log4r}
12
+ s.description = %q{Failure handlers for rack and resque that log failures using log4r. It is expected that these logs will get sent elsewhere (e.g. graylog) by using log4r outputters (e.g. log4r-gelf)}
13
+
14
+ s.rubyforge_project = "log4r-exceptionable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency("rake")
23
+ s.add_development_dependency("rspec")
24
+ s.add_development_dependency("awesome_print")
25
+
26
+ s.add_development_dependency("rack-test")
27
+ s.add_development_dependency("resque")
28
+ s.add_dependency("log4r")
29
+ end
@@ -0,0 +1,15 @@
1
+ require "log4r"
2
+ require "log4r-exceptionable/version"
3
+ require "log4r-exceptionable/configuration"
4
+
5
+ # optional if only using resque
6
+ begin
7
+ require "log4r-exceptionable/rack_failure_handler"
8
+ rescue LoadError
9
+ end
10
+
11
+ # optional if only using rack
12
+ begin
13
+ require "log4r-exceptionable/resque_failure_handler"
14
+ rescue LoadError
15
+ end
@@ -0,0 +1,43 @@
1
+ module Log4rExceptionable
2
+
3
+ # Failure backends that log exceptions with log4r
4
+ #
5
+ # Log4rExceptionable::Configuration.configure do |config|
6
+ # # required
7
+ # config.resque_failure_logger = "rails::SomeLogger"
8
+ # config.rack_failure_logger = "rails::SomeLogger"
9
+ # end
10
+ #
11
+ # Rails.application.config.middleware.use "Log4rExceptionable::RackFailureHandler"
12
+ # Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
13
+ #
14
+ class Configuration
15
+
16
+ class << self
17
+ # required
18
+ attr_accessor :rack_failure_logger, :resque_failure_logger
19
+ end
20
+
21
+ def self.configure
22
+ yield self
23
+ raise "log4r-exceptionable requires a rack_failure_logger or resque_failure_logger" unless self.rack_failure_logger || self.resque_failure_logger
24
+
25
+ if self.rack_failure_logger
26
+ self.set_logger(:rack_failure_logger)
27
+ end
28
+
29
+ if self.resque_failure_logger
30
+ self.set_logger(:resque_failure_logger)
31
+ end
32
+ end
33
+
34
+ def self.set_logger(accessor)
35
+ if ! self.send(accessor).instance_of?(Log4r::Logger)
36
+ name = self.send(accessor).to_s
37
+ self.send("#{accessor}=", Log4r::Logger[name] || Log4r::Logger.new(name))
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,82 @@
1
+
2
+ module Log4rExceptionable
3
+
4
+ # A rack middleware handler that logs exceptions with log4r
5
+ #
6
+ class RackFailureHandler
7
+ attr_reader :args
8
+
9
+ def initialize(app, opts = {})
10
+ @app = app
11
+ end
12
+
13
+ def logger
14
+ Log4rExceptionable::Configuration.rack_failure_logger
15
+ end
16
+
17
+ def call(env)
18
+ # Make thread safe
19
+ dup._call(env)
20
+ end
21
+
22
+ def _call(env)
23
+ begin
24
+ # Call the app we are monitoring
25
+ response = @app.call(env)
26
+ rescue => exception
27
+ # An exception has been raised. Send to log4r
28
+ send_to_log4r(exception, env)
29
+
30
+ # Raise the exception again to pass back to app.
31
+ raise
32
+ end
33
+
34
+ if env['rack.exception']
35
+ send_to_log4r(env['rack.exception'], env)
36
+ end
37
+
38
+ response
39
+ end
40
+
41
+ def send_to_log4r(exception, env=nil)
42
+ begin
43
+ mdc = Log4r::MDC
44
+ original_mdc = mdc.get_context
45
+
46
+ begin
47
+ mdc.put('rack_exception', exception.class.name)
48
+ trace = Array(exception.backtrace)
49
+ if trace.size > 0
50
+ mdc.put('rack_exception_backtrace', trace.join("\n"))
51
+ file, line = trace[0].split(":")
52
+ mdc.put('rack_exception_file', file)
53
+ mdc.put('rack_exception_line', line)
54
+ end
55
+
56
+ if env and env.size > 0
57
+ env.each do |k, v|
58
+ begin
59
+ mdc.put("rack_env_#{k}", v.inspect)
60
+ rescue
61
+ end
62
+ end
63
+ end
64
+
65
+ message = "#{exception.class}: #{exception.message}"
66
+ logger.error(message)
67
+ ensure
68
+ # Since this is somewhat of a global map, clean the keys
69
+ # we put in so other log messages don't see them
70
+ mdc.get_context.keys.each do |k|
71
+ mdc.remove(k) unless original_mdc.has_key?(k)
72
+ end
73
+ end
74
+
75
+ rescue Exception => e
76
+ puts "Log4r Exceptionable could not log rack exception: " + e.message
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,56 @@
1
+ require 'resque'
2
+
3
+ module Log4rExceptionable
4
+
5
+ # A Resque Failure backend that logs exceptions with log4r
6
+ #
7
+ class ResqueFailureHandler < ::Resque::Failure::Base
8
+
9
+ def logger
10
+ Log4rExceptionable::Configuration.resque_failure_logger
11
+ end
12
+
13
+ def save
14
+ begin
15
+ mdc = Log4r::MDC
16
+ original_mdc = mdc.get_context
17
+
18
+ begin
19
+ mdc.put('rack_exception', exception.class.name)
20
+ trace = Array(exception.backtrace)
21
+ if trace.size > 0
22
+ mdc.put('resque_exception_backtrace', trace.join("\n"))
23
+ file, line = trace[0].split(":")
24
+ mdc.put('resque_exception_file', file)
25
+ mdc.put('resque_exception_line', line)
26
+ end
27
+
28
+ mdc.put("resque_worker", worker.to_s)
29
+ mdc.put("resque_queue", queue.to_s)
30
+ mdc.put("resque_class", payload['class'].to_s)
31
+ mdc.put("resque_args", payload['args'].inspect.to_s)
32
+
33
+ message = "#{exception.class}: #{exception.message}"
34
+ logger.error(message)
35
+ ensure
36
+ # Since this is somewhat of a global map, clean the keys
37
+ # we put in so other log messages don't see them
38
+ mdc.get_context.keys.each do |k|
39
+ mdc.remove(k) unless original_mdc.has_key?(k)
40
+ end
41
+ end
42
+
43
+ rescue Exception => e
44
+ puts "Log4r Exceptionable could not log resque exception: " + e.message
45
+ end
46
+ end
47
+
48
+ def self.count
49
+ # We can't get the total # of errors from graylog so we fake it
50
+ # by asking Resque how many errors it has seen.
51
+ ::Resque::Stat[:failed]
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,3 @@
1
+ module Log4rExceptionable
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Log4rExceptionable::Configuration do
4
+
5
+ context "configure" do
6
+
7
+ it "should raise if no logger in config" do
8
+ lambda {
9
+ Log4rExceptionable::Configuration.configure do |config|
10
+ end
11
+ }.should raise_error("log4r-exceptionable requires a rack_failure_logger or resque_failure_logger")
12
+ end
13
+
14
+ it "should not raise if config valid" do
15
+ lambda {
16
+ Log4rExceptionable::Configuration.configure do |config|
17
+ config.rack_failure_logger = 'mylogger'
18
+ end
19
+ }.should_not raise_exception
20
+ Log4rExceptionable::Configuration.rack_failure_logger = nil
21
+
22
+ lambda {
23
+ Log4rExceptionable::Configuration.configure do |config|
24
+ config.resque_failure_logger = 'mylogger'
25
+ end
26
+ }.should_not raise_exception
27
+ end
28
+
29
+ it "should allow setting logger to string for pre-existing logger" do
30
+ logger = Log4r::Logger.new('existinglogger')
31
+ Log4rExceptionable::Configuration.configure do |config|
32
+ config.rack_failure_logger = logger
33
+ end
34
+
35
+ Log4rExceptionable::Configuration.rack_failure_logger.should == Log4r::Logger['existinglogger']
36
+ Log4rExceptionable::Configuration.rack_failure_logger.should == logger
37
+ end
38
+
39
+ it "should allow setting logger to string for non-existing logger" do
40
+ Log4r::Logger['newlogger'].should be_nil
41
+
42
+ Log4rExceptionable::Configuration.configure do |config|
43
+ config.rack_failure_logger = "newlogger"
44
+ end
45
+
46
+ Log4rExceptionable::Configuration.rack_failure_logger.should == Log4r::Logger['newlogger']
47
+ end
48
+
49
+ it "should allow setting logger to logger instance" do
50
+ Log4rExceptionable::Configuration.configure do |config|
51
+ config.rack_failure_logger = Log4r::Logger.new('otherlogger')
52
+ end
53
+
54
+ Log4rExceptionable::Configuration.rack_failure_logger.should == Log4r::Logger['otherlogger']
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Log4rExceptionable::RackFailureHandler do
4
+ include Rack::Test::Methods
5
+
6
+ class TestApp
7
+ def call(env)
8
+ if env['PATH_INFO'] =~ /error/
9
+ raise "I failed"
10
+ else
11
+ return [200, {"Content-Type" => "text"}, ["hello"]]
12
+ end
13
+ end
14
+ end
15
+
16
+ def app
17
+ @app ||= Log4rExceptionable::RackFailureHandler.new(TestApp.new)
18
+ end
19
+
20
+ context "handling rack failures" do
21
+
22
+ before(:all) do
23
+ Log4rExceptionable::Configuration.configure do |config|
24
+ config.rack_failure_logger = 'racklogger'
25
+ end
26
+ end
27
+
28
+ it "doesn't log a failure with normal usage" do
29
+ Log4r::Logger['racklogger'].should_not_receive(:error)
30
+ get "/"
31
+ end
32
+
33
+ it "triggers failure handler" do
34
+ Log4r::Logger['racklogger'].should_receive(:error) do |msg|
35
+ msg.should == "RuntimeError: I failed"
36
+ Log4r::MDC.get('rack_exception_backtrace').should =~ /rack_failure_handler_spec.rb/
37
+ Log4r::MDC.get('rack_exception_backtrace').lines.to_a.size.should > 1
38
+ Log4r::MDC.get('rack_exception_line').should =~ /\d+/
39
+ Log4r::MDC.get('rack_exception_file').should =~ /rack_failure_handler_spec.rb/
40
+ Log4r::MDC.get('rack_env_PATH_INFO').should == '"/error"'
41
+ end
42
+
43
+ lambda {
44
+ get "/error"
45
+ }.should raise_error("I failed")
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,312 @@
1
+ # Redis configuration file example
2
+
3
+ # Note on units: when memory size is needed, it is possible to specifiy
4
+ # it in the usual form of 1k 5GB 4M and so forth:
5
+ #
6
+ # 1k => 1000 bytes
7
+ # 1kb => 1024 bytes
8
+ # 1m => 1000000 bytes
9
+ # 1mb => 1024*1024 bytes
10
+ # 1g => 1000000000 bytes
11
+ # 1gb => 1024*1024*1024 bytes
12
+ #
13
+ # units are case insensitive so 1GB 1Gb 1gB are all the same.
14
+
15
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
16
+ # Note that Redis will write a pid file in ./tmp/run/redis.pid when daemonized.
17
+ daemonize yes
18
+
19
+ # When running daemonized, Redis writes a pid file in ./tmp/run/redis.pid by
20
+ # default. You can specify a custom pid file location here.
21
+ pidfile ./redis.pid
22
+
23
+ # Accept connections on the specified port, default is 6379
24
+ port 6379
25
+
26
+ # If you want you can bind a single interface, if the bind option is not
27
+ # specified all the interfaces will listen for incoming connections.
28
+ #
29
+ bind 127.0.0.1
30
+
31
+ # Close the connection after a client is idle for N seconds (0 to disable)
32
+ timeout 300
33
+
34
+ # Set server verbosity to 'debug'
35
+ # it can be one of:
36
+ # debug (a lot of information, useful for development/testing)
37
+ # verbose (many rarely useful info, but not a mess like the debug level)
38
+ # notice (moderately verbose, what you want in production probably)
39
+ # warning (only very important / critical messages are logged)
40
+ loglevel verbose
41
+
42
+ # Specify the log file name. Also 'stdout' can be used to force
43
+ # Redis to log on the standard output. Note that if you use standard
44
+ # output for logging but daemonize, logs will be sent to /dev/null
45
+ logfile ./redis-server.log
46
+
47
+ # Set the number of databases. The default database is DB 0, you can select
48
+ # a different one on a per-connection basis using SELECT <dbid> where
49
+ # dbid is a number between 0 and 'databases'-1
50
+ databases 16
51
+
52
+ ################################ SNAPSHOTTING #################################
53
+ #
54
+ # Save the DB on disk:
55
+ #
56
+ # save <seconds> <changes>
57
+ #
58
+ # Will save the DB if both the given number of seconds and the given
59
+ # number of write operations against the DB occurred.
60
+ #
61
+ # In the example below the behaviour will be to save:
62
+ # after 900 sec (15 min) if at least 1 key changed
63
+ # after 300 sec (5 min) if at least 10 keys changed
64
+ # after 60 sec if at least 10000 keys changed
65
+ #
66
+ # Note: you can disable saving at all commenting all the "save" lines.
67
+
68
+ save 900 1
69
+ save 300 10
70
+ save 60 10000
71
+
72
+ # Compress string objects using LZF when dump .rdb databases?
73
+ # For default that's set to 'yes' as it's almost always a win.
74
+ # If you want to save some CPU in the saving child set it to 'no' but
75
+ # the dataset will likely be bigger if you have compressible values or keys.
76
+ rdbcompression yes
77
+
78
+ # The filename where to dump the DB
79
+ dbfilename dump.rdb
80
+
81
+ # The working directory.
82
+ #
83
+ # The DB will be written inside this directory, with the filename specified
84
+ # above using the 'dbfilename' configuration directive.
85
+ #
86
+ # Also the Append Only File will be created inside this directory.
87
+ #
88
+ # Note that you must specify a directory here, not a file name.
89
+ dir .
90
+
91
+ ################################# REPLICATION #################################
92
+
93
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
94
+ # another Redis server. Note that the configuration is local to the slave
95
+ # so for example it is possible to configure the slave to save the DB with a
96
+ # different interval, or to listen to another port, and so on.
97
+ #
98
+ # slaveof <masterip> <masterport>
99
+
100
+ # If the master is password protected (using the "requirepass" configuration
101
+ # directive below) it is possible to tell the slave to authenticate before
102
+ # starting the replication synchronization process, otherwise the master will
103
+ # refuse the slave request.
104
+ #
105
+ # masterauth <master-password>
106
+
107
+ ################################## SECURITY ###################################
108
+
109
+ # Require clients to issue AUTH <PASSWORD> before processing any other
110
+ # commands. This might be useful in environments in which you do not trust
111
+ # others with access to the host running redis-server.
112
+ #
113
+ # This should stay commented out for backward compatibility and because most
114
+ # people do not need auth (e.g. they run their own servers).
115
+ #
116
+ # Warning: since Redis is pretty fast an outside user can try up to
117
+ # 150k passwords per second against a good box. This means that you should
118
+ # use a very strong password otherwise it will be very easy to break.
119
+ #
120
+ # requirepass foobared
121
+
122
+ ################################### LIMITS ####################################
123
+
124
+ # Set the max number of connected clients at the same time. By default there
125
+ # is no limit, and it's up to the number of file descriptors the Redis process
126
+ # is able to open. The special value '0' means no limits.
127
+ # Once the limit is reached Redis will close all the new connections sending
128
+ # an error 'max number of clients reached'.
129
+ #
130
+ # maxclients 128
131
+
132
+ # Don't use more memory than the specified amount of bytes.
133
+ # When the memory limit is reached Redis will try to remove keys with an
134
+ # EXPIRE set. It will try to start freeing keys that are going to expire
135
+ # in little time and preserve keys with a longer time to live.
136
+ # Redis will also try to remove objects from free lists if possible.
137
+ #
138
+ # If all this fails, Redis will start to reply with errors to commands
139
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
140
+ # to reply to most read-only commands like GET.
141
+ #
142
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
143
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
144
+ # database the memory usage will grow over the weeks, it will be obvious if
145
+ # it is going to use too much memory in the long run, and you'll have the time
146
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
147
+ # errors for write operations, and this may even lead to DB inconsistency.
148
+ #
149
+ # maxmemory <bytes>
150
+
151
+ ############################## APPEND ONLY MODE ###############################
152
+
153
+ # By default Redis asynchronously dumps the dataset on disk. If you can live
154
+ # with the idea that the latest records will be lost if something like a crash
155
+ # happens this is the preferred way to run Redis. If instead you care a lot
156
+ # about your data and don't want to that a single record can get lost you should
157
+ # enable the append only mode: when this mode is enabled Redis will append
158
+ # every write operation received in the file appendonly.aof. This file will
159
+ # be read on startup in order to rebuild the full dataset in memory.
160
+ #
161
+ # Note that you can have both the async dumps and the append only file if you
162
+ # like (you have to comment the "save" statements above to disable the dumps).
163
+ # Still if append only mode is enabled Redis will load the data from the
164
+ # log file at startup ignoring the dump.rdb file.
165
+ #
166
+ # IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
167
+ # log file in background when it gets too big.
168
+
169
+ appendonly no
170
+
171
+ # The name of the append only file (default: "appendonly.aof")
172
+ # appendfilename appendonly.aof
173
+
174
+ # The fsync() call tells the Operating System to actually write data on disk
175
+ # instead to wait for more data in the output buffer. Some OS will really flush
176
+ # data on disk, some other OS will just try to do it ASAP.
177
+ #
178
+ # Redis supports three different modes:
179
+ #
180
+ # no: don't fsync, just let the OS flush the data when it wants. Faster.
181
+ # always: fsync after every write to the append only log . Slow, Safest.
182
+ # everysec: fsync only if one second passed since the last fsync. Compromise.
183
+ #
184
+ # The default is "everysec" that's usually the right compromise between
185
+ # speed and data safety. It's up to you to understand if you can relax this to
186
+ # "no" that will will let the operating system flush the output buffer when
187
+ # it wants, for better performances (but if you can live with the idea of
188
+ # some data loss consider the default persistence mode that's snapshotting),
189
+ # or on the contrary, use "always" that's very slow but a bit safer than
190
+ # everysec.
191
+ #
192
+ # If unsure, use "everysec".
193
+
194
+ # appendfsync always
195
+ appendfsync everysec
196
+ # appendfsync no
197
+
198
+ ################################ VIRTUAL MEMORY ###############################
199
+
200
+ # Virtual Memory allows Redis to work with datasets bigger than the actual
201
+ # amount of RAM needed to hold the whole dataset in memory.
202
+ # In order to do so very used keys are taken in memory while the other keys
203
+ # are swapped into a swap file, similarly to what operating systems do
204
+ # with memory pages.
205
+ #
206
+ # To enable VM just set 'vm-enabled' to yes, and set the following three
207
+ # VM parameters accordingly to your needs.
208
+
209
+ vm-enabled no
210
+ # vm-enabled yes
211
+
212
+ # This is the path of the Redis swap file. As you can guess, swap files
213
+ # can't be shared by different Redis instances, so make sure to use a swap
214
+ # file for every redis process you are running. Redis will complain if the
215
+ # swap file is already in use.
216
+ #
217
+ # The best kind of storage for the Redis swap file (that's accessed at random)
218
+ # is a Solid State Disk (SSD).
219
+ #
220
+ # *** WARNING *** if you are using a shared hosting the default of putting
221
+ # the swap file under /tmp is not secure. Create a dir with access granted
222
+ # only to Redis user and configure Redis to create the swap file there.
223
+ vm-swap-file ./tmp/redis.swap
224
+
225
+ # vm-max-memory configures the VM to use at max the specified amount of
226
+ # RAM. Everything that deos not fit will be swapped on disk *if* possible, that
227
+ # is, if there is still enough contiguous space in the swap file.
228
+ #
229
+ # With vm-max-memory 0 the system will swap everything it can. Not a good
230
+ # default, just specify the max amount of RAM you can in bytes, but it's
231
+ # better to leave some margin. For instance specify an amount of RAM
232
+ # that's more or less between 60 and 80% of your free RAM.
233
+ vm-max-memory 0
234
+
235
+ # Redis swap files is split into pages. An object can be saved using multiple
236
+ # contiguous pages, but pages can't be shared between different objects.
237
+ # So if your page is too big, small objects swapped out on disk will waste
238
+ # a lot of space. If you page is too small, there is less space in the swap
239
+ # file (assuming you configured the same number of total swap file pages).
240
+ #
241
+ # If you use a lot of small objects, use a page size of 64 or 32 bytes.
242
+ # If you use a lot of big objects, use a bigger page size.
243
+ # If unsure, use the default :)
244
+ vm-page-size 32
245
+
246
+ # Number of total memory pages in the swap file.
247
+ # Given that the page table (a bitmap of free/used pages) is taken in memory,
248
+ # every 8 pages on disk will consume 1 byte of RAM.
249
+ #
250
+ # The total swap size is vm-page-size * vm-pages
251
+ #
252
+ # With the default of 32-bytes memory pages and 134217728 pages Redis will
253
+ # use a 4 GB swap file, that will use 16 MB of RAM for the page table.
254
+ #
255
+ # It's better to use the smallest acceptable value for your application,
256
+ # but the default is large in order to work in most conditions.
257
+ vm-pages 134217728
258
+
259
+ # Max number of VM I/O threads running at the same time.
260
+ # This threads are used to read/write data from/to swap file, since they
261
+ # also encode and decode objects from disk to memory or the reverse, a bigger
262
+ # number of threads can help with big objects even if they can't help with
263
+ # I/O itself as the physical device may not be able to couple with many
264
+ # reads/writes operations at the same time.
265
+ #
266
+ # The special value of 0 turn off threaded I/O and enables the blocking
267
+ # Virtual Memory implementation.
268
+ vm-max-threads 4
269
+
270
+ ############################### ADVANCED CONFIG ###############################
271
+
272
+ # Glue small output buffers together in order to send small replies in a
273
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
274
+ # in terms of number of queries per second. Use 'yes' if unsure.
275
+ glueoutputbuf yes
276
+
277
+ # Hashes are encoded in a special way (much more memory efficient) when they
278
+ # have at max a given numer of elements, and the biggest element does not
279
+ # exceed a given threshold. You can configure this limits with the following
280
+ # configuration directives.
281
+ hash-max-zipmap-entries 64
282
+ hash-max-zipmap-value 512
283
+
284
+ # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
285
+ # order to help rehashing the main Redis hash table (the one mapping top-level
286
+ # keys to values). The hash table implementation redis uses (see dict.c)
287
+ # performs a lazy rehashing: the more operation you run into an hash table
288
+ # that is rhashing, the more rehashing "steps" are performed, so if the
289
+ # server is idle the rehashing is never complete and some more memory is used
290
+ # by the hash table.
291
+ #
292
+ # The default is to use this millisecond 10 times every second in order to
293
+ # active rehashing the main dictionaries, freeing memory when possible.
294
+ #
295
+ # If unsure:
296
+ # use "activerehashing no" if you have hard latency requirements and it is
297
+ # not a good thing in your environment that Redis can reply form time to time
298
+ # to queries with 2 milliseconds delay.
299
+ #
300
+ # use "activerehashing yes" if you don't have such hard requirements but
301
+ # want to free memory asap when possible.
302
+ activerehashing yes
303
+
304
+ ################################## INCLUDES ###################################
305
+
306
+ # Include one or more other config files here. This is useful if you
307
+ # have a standard template that goes to all redis server but also need
308
+ # to customize a few per-server settings. Include files can include
309
+ # other files, so use this wisely.
310
+ #
311
+ # include /path/to/local.conf
312
+ # include /path/to/other.conf
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe Log4rExceptionable::ResqueFailureHandler do
4
+ include PerformJob
5
+
6
+ context "handling resque failures" do
7
+
8
+ class SomeJob
9
+ def self.perform(*args)
10
+ raise "I failed"
11
+ end
12
+ end
13
+
14
+ before(:all) do
15
+ Log4rExceptionable::Configuration.configure do |config|
16
+ config.resque_failure_logger = 'resquelogger'
17
+ end
18
+
19
+ Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
20
+ end
21
+
22
+ it "triggers failure handler" do
23
+
24
+ Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
25
+ msg.should == "RuntimeError: I failed"
26
+ Log4r::MDC.get('resque_exception_backtrace').should =~ /resque_failure_handler_spec.rb/
27
+ Log4r::MDC.get('resque_exception_backtrace').lines.to_a.size.should > 1
28
+ Log4r::MDC.get('resque_exception_line').should =~ /\d+/
29
+ Log4r::MDC.get('resque_exception_file').should =~ /resque_failure_handler_spec.rb/
30
+ Log4r::MDC.get('resque_worker').should == ""
31
+ Log4r::MDC.get('resque_queue').should == "somequeue"
32
+ Log4r::MDC.get('resque_class').should == "SomeJob"
33
+ Log4r::MDC.get('resque_args').should == "[\"foo\"]"
34
+ end
35
+
36
+ run_resque_job(SomeJob, 'foo', :queue => :somequeue, :inline => true)
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,78 @@
1
+ require "rack/test"
2
+ require 'rspec'
3
+ require 'resque'
4
+ require 'log4r-exceptionable'
5
+ require 'ap'
6
+
7
+ # No need to start redis when running in Travis
8
+ unless ENV['CI']
9
+
10
+ begin
11
+ Resque.queues
12
+ rescue Errno::ECONNREFUSED
13
+ spec_dir = File.dirname(File.expand_path(__FILE__))
14
+ REDIS_CMD = "redis-server #{spec_dir}/redis-test.conf"
15
+
16
+ puts "Starting redis for testing at localhost..."
17
+ puts `cd #{spec_dir}; #{REDIS_CMD}`
18
+
19
+ # Schedule the redis server for shutdown when tests are all finished.
20
+ at_exit do
21
+ puts 'Stopping redis'
22
+ pid = File.read("#{spec_dir}/redis.pid").to_i rescue nil
23
+ system ("kill -9 #{pid}") if pid.to_i != 0
24
+ File.delete("#{spec_dir}/redis.pid") rescue nil
25
+ File.delete("#{spec_dir}/redis-server.log") rescue nil
26
+ File.delete("#{spec_dir}/dump.rdb") rescue nil
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ ##
33
+ # Helper to perform job classes
34
+ #
35
+ module PerformJob
36
+
37
+ def run_resque_job(job_class, *job_args)
38
+ opts = job_args.last.is_a?(Hash) ? job_args.pop : {}
39
+ queue = opts[:queue] || Resque.queue_from_class(job_class)
40
+
41
+ Resque::Job.create(queue, job_class, *job_args)
42
+
43
+ run_resque_queue(queue, opts)
44
+ end
45
+
46
+ def run_resque_queue(queue, opts={})
47
+ worker = Resque::Worker.new(queue)
48
+ worker.very_verbose = true if opts[:verbose]
49
+
50
+ # do a single job then shutdown
51
+ def worker.done_working
52
+ super
53
+ shutdown
54
+ end
55
+
56
+ if opts[:inline]
57
+ job = worker.reserve
58
+ worker.perform(job)
59
+ else
60
+ worker.work(0)
61
+ end
62
+ end
63
+
64
+ def dump_redis
65
+ result = {}
66
+ Resque.redis.keys("*").each do |key|
67
+ type = Resque.redis.type(key)
68
+ result[key] = case type
69
+ when 'string' then Resque.redis.get(key)
70
+ when 'list' then Resque.redis.lrange(key, 0, -1)
71
+ when 'set' then Resque.redis.smembers(key)
72
+ else type
73
+ end
74
+ end
75
+ return result
76
+ end
77
+
78
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log4r-exceptionable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt Conway
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
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
+ - !ruby/object:Gem::Dependency
47
+ name: awesome_print
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: resque
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: log4r
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Failure handlers for rack and resque that log failures using log4r. It
111
+ is expected that these logs will get sent elsewhere (e.g. graylog) by using log4r
112
+ outputters (e.g. log4r-gelf)
113
+ email:
114
+ - matt@conwaysplace.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .travis.yml
121
+ - CHANGELOG
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - graylog2-resque.gemspec
127
+ - lib/log4r-exceptionable.rb
128
+ - lib/log4r-exceptionable/configuration.rb
129
+ - lib/log4r-exceptionable/rack_failure_handler.rb
130
+ - lib/log4r-exceptionable/resque_failure_handler.rb
131
+ - lib/log4r-exceptionable/version.rb
132
+ - spec/configuration_spec.rb
133
+ - spec/rack_failure_handler_spec.rb
134
+ - spec/redis-test.conf
135
+ - spec/resque_failure_handler_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: ''
138
+ licenses: []
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project: log4r-exceptionable
157
+ rubygems_version: 1.8.21
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Failure handlers for rack and resque that log failures using log4r
161
+ test_files:
162
+ - spec/configuration_spec.rb
163
+ - spec/rack_failure_handler_spec.rb
164
+ - spec/redis-test.conf
165
+ - spec/resque_failure_handler_spec.rb
166
+ - spec/spec_helper.rb
167
+ has_rdoc: