background_lite 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'logger'
|
3
|
+
|
1
4
|
# This module holds methods for background handling
|
2
5
|
module BackgroundLite
|
3
6
|
# This class is for configuring defaults of the background processing
|
@@ -14,6 +17,9 @@ module BackgroundLite
|
|
14
17
|
@@default_error_reporter = :stdout
|
15
18
|
cattr_accessor :default_error_reporter
|
16
19
|
|
20
|
+
# Logger for debugging purposes
|
21
|
+
cattr_writer :default_logger
|
22
|
+
|
17
23
|
def self.config #:nodoc:
|
18
24
|
@config ||= YAML.load(File.read("#{RAILS_ROOT}/config/background.yml")) rescue { RAILS_ENV => {} }
|
19
25
|
end
|
@@ -27,6 +33,17 @@ module BackgroundLite
|
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
36
|
+
|
37
|
+
def self.default_logger #:nodoc:
|
38
|
+
fallback_logger = if Object.const_defined?("RAILS_DEFAULT_LOGGER")
|
39
|
+
RAILS_DEFAULT_LOGGER
|
40
|
+
else
|
41
|
+
logger = Logger.new(STDOUT)
|
42
|
+
logger.level = Logger::WARN
|
43
|
+
logger
|
44
|
+
end
|
45
|
+
@@default_logger ||= config['logger'] || fallback_logger
|
46
|
+
end
|
30
47
|
|
31
48
|
def self.load(configuration) #:nodoc:
|
32
49
|
if configuration.blank?
|
@@ -83,17 +100,25 @@ module BackgroundLite
|
|
83
100
|
[options.delete(:handler) || config[:handler] || BackgroundLite::Config.default_handler].flatten
|
84
101
|
end
|
85
102
|
reporter = options.delete(:reporter) || config[:reporter] || BackgroundLite::Config.default_error_reporter
|
103
|
+
logger = options.delete(:logger) || config[:logger] || BackgroundLite::Config.default_logger
|
86
104
|
|
87
105
|
handler.each do |hand|
|
88
|
-
options = {}
|
89
106
|
if hand.is_a? Hash
|
90
107
|
raise "Malformed handler options Hash" if hand.keys.size != 1
|
91
108
|
options = hand.values.first
|
92
109
|
hand = hand.keys.first
|
93
110
|
end
|
111
|
+
options ||= {}
|
94
112
|
|
95
113
|
begin
|
96
114
|
BackgroundLite.disable do
|
115
|
+
if logger.debug?
|
116
|
+
# Transaction ID is currently only used for debugging to find corresponding
|
117
|
+
# messages on both sides of the queue. It is optional and should be expected
|
118
|
+
# to be nil
|
119
|
+
options[:transaction_id] = Digest::SHA1.hexdigest(object.to_s + method.to_s + args.inspect + Time.now.to_s)
|
120
|
+
logger.debug("Sending to background: Object: #{object.inspect} Method: #{method} Args: #{args.inspect} Options: #{options.inspect}")
|
121
|
+
end
|
97
122
|
"BackgroundLite::#{hand.to_s.camelize}Handler".constantize.handle(object, method, args, options)
|
98
123
|
end
|
99
124
|
|
@@ -28,7 +28,7 @@ module BackgroundLite
|
|
28
28
|
# queue:: The name of the queue to use to send the message to the background
|
29
29
|
# process.
|
30
30
|
def self.handle(object, method, args, options = {})
|
31
|
-
ActiveMessaging::Gateway.publish((options[:queue] || self.queue_name).to_sym, Marshal.dump([object, method, args]))
|
31
|
+
ActiveMessaging::Gateway.publish((options[:queue] || self.queue_name).to_sym, Marshal.dump([object, method, args, options[:transaction_id]]))
|
32
32
|
end
|
33
33
|
|
34
34
|
# Decodes a marshalled message which was previously sent over
|
@@ -36,14 +36,14 @@ module BackgroundLite
|
|
36
36
|
# as a string, and the method arguments.
|
37
37
|
def self.decode(message)
|
38
38
|
begin
|
39
|
-
object, method, args = Marshal.load(message)
|
39
|
+
object, method, args, transaction_id = Marshal.load(message)
|
40
40
|
rescue ArgumentError => e
|
41
41
|
# Marshal.load does not trigger const_missing, so we have to do this
|
42
42
|
# ourselves.
|
43
43
|
e.message.split(' ').last.constantize
|
44
44
|
retry
|
45
45
|
end
|
46
|
-
[object, method, args]
|
46
|
+
[object, method, args, transaction_id]
|
47
47
|
end
|
48
48
|
|
49
49
|
# Executes a marshalled message which was previously sent over
|
@@ -51,14 +51,19 @@ module BackgroundLite
|
|
51
51
|
# passed.
|
52
52
|
def self.execute(message)
|
53
53
|
begin
|
54
|
-
object, method, args = self.decode(message)
|
55
|
-
|
56
|
-
|
54
|
+
object, method, args, transaction_id = self.decode(message)
|
55
|
+
logger = BackgroundLite::Config.default_logger
|
56
|
+
if logger.debug?
|
57
|
+
logger.debug "--- executing method: #{method}"
|
58
|
+
logger.debug "--- with variables: #{args.inspect}"
|
59
|
+
logger.debug "--- in object: #{object.class.name}, #{object.id}"
|
60
|
+
logger.debug "--- Transaction ID: #{transaction_id}"
|
61
|
+
end
|
57
62
|
object.send(method, *args)
|
58
|
-
|
63
|
+
logger.debug "--- it happened!" if logger.debug?
|
59
64
|
rescue Exception => e
|
60
|
-
|
61
|
-
|
65
|
+
logger.fatal e.message
|
66
|
+
logger.fatal e.backtrace
|
62
67
|
end
|
63
68
|
end
|
64
69
|
end
|
@@ -3,12 +3,16 @@ module BackgroundLite
|
|
3
3
|
# Resque to the background process.
|
4
4
|
class ResqueHandler
|
5
5
|
@queue = :background
|
6
|
-
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :queue
|
9
|
+
end
|
10
|
+
|
7
11
|
# Marshals the method and the arguments and sends it through Resque
|
8
12
|
# to the background process.
|
9
13
|
def self.handle(object, method, args, options = {})
|
10
14
|
require 'resque'
|
11
|
-
Resque.enqueue(self, Base64.encode64(Marshal.dump([object, method, args])))
|
15
|
+
Resque.enqueue(self, Base64.encode64(Marshal.dump([object, method, args, options[:transaction_id]])))
|
12
16
|
end
|
13
17
|
|
14
18
|
# Decodes a marshalled message which was previously sent over
|
@@ -16,14 +20,14 @@ module BackgroundLite
|
|
16
20
|
# as a string, and the method arguments.
|
17
21
|
def self.decode(message)
|
18
22
|
begin
|
19
|
-
object, method, args = Marshal.load(Base64.decode64(message))
|
23
|
+
object, method, args, transaction_id = Marshal.load(Base64.decode64(message))
|
20
24
|
rescue ArgumentError => e
|
21
25
|
# Marshal.load does not trigger const_missing, so we have to do this
|
22
26
|
# ourselves.
|
23
27
|
e.message.split(' ').last.constantize
|
24
28
|
retry
|
25
29
|
end
|
26
|
-
[object, method, args]
|
30
|
+
[object, method, args, transaction_id]
|
27
31
|
end
|
28
32
|
|
29
33
|
# Executes a marshalled message which was previously sent over
|
@@ -31,14 +35,19 @@ module BackgroundLite
|
|
31
35
|
# passed.
|
32
36
|
def self.perform(message)
|
33
37
|
begin
|
34
|
-
object, method, args = self.decode(message)
|
35
|
-
|
36
|
-
|
38
|
+
object, method, args, transaction_id = self.decode(message)
|
39
|
+
logger = BackgroundLite::Config.default_logger
|
40
|
+
if logger.debug?
|
41
|
+
logger.debug "--- executing method: #{method}"
|
42
|
+
logger.debug "--- with variables: #{args.inspect}"
|
43
|
+
logger.debug "--- in object: #{object.class.name}, #{object.id}"
|
44
|
+
logger.debug "--- Transaction ID: #{transaction_id}"
|
45
|
+
end
|
37
46
|
object.send(method, *args)
|
38
|
-
|
47
|
+
logger.debug "--- it happened!" if logger.debug?
|
39
48
|
rescue Exception => e
|
40
|
-
|
41
|
-
|
49
|
+
logger.fatal e.message
|
50
|
+
logger.fatal e.backtrace
|
42
51
|
end
|
43
52
|
end
|
44
53
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: background_lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thomas Kadauke
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-10 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|