forked from tobi/delayed_job
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace custom serialization code with YAML magic
- Loading branch information
Showing
10 changed files
with
108 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'yaml' | ||
|
||
class Module | ||
yaml_as "tag:ruby.yaml.org,2002:module" | ||
|
||
def Module.yaml_new( klass, tag, val ) | ||
if String === val | ||
val.split(/::/).inject(Object) {|m, n| m.const_get(n)} | ||
else | ||
raise YAML::TypeError, "Invalid Module: " + val.inspect | ||
end | ||
end | ||
|
||
def to_yaml( opts = {} ) | ||
YAML::quick_emit( nil, opts ) { |out| | ||
out.scalar( "tag:ruby.yaml.org,2002:module", self.name, :plain ) | ||
} | ||
end | ||
end | ||
|
||
class Class | ||
yaml_as "tag:ruby.yaml.org,2002:class" | ||
|
||
def Class.yaml_new( klass, tag, val ) | ||
if String === val | ||
val.split(/::/).inject(Object) {|m, n| m.const_get(n)} | ||
else | ||
raise YAML::TypeError, "Invalid Class: " + val.inspect | ||
end | ||
end | ||
|
||
def to_yaml( opts = {} ) | ||
YAML::quick_emit( nil, opts ) { |out| | ||
out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain ) | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,19 @@ | ||
class Class | ||
def load_for_delayed_job(arg) | ||
self | ||
end | ||
|
||
def dump_for_delayed_job | ||
name | ||
end | ||
end | ||
|
||
module Delayed | ||
class PerformableMethod < Struct.new(:object, :method, :args) | ||
STRING_FORMAT = /^LOAD\;([A-Z][\w\:]+)(?:\;(\w+))?$/ | ||
|
||
class LoadError < StandardError | ||
end | ||
|
||
def initialize(object, method, args) | ||
raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method) | ||
|
||
self.object = dump(object) | ||
self.args = args.map { |a| dump(a) } | ||
self.object = object | ||
self.args = args | ||
self.method = method.to_sym | ||
end | ||
|
||
def display_name | ||
if STRING_FORMAT === object | ||
"#{$1}#{$2 ? '#' : '.'}#{method}" | ||
else | ||
"#{object.class}##{method}" | ||
end | ||
"#{object.class}##{method}" | ||
end | ||
|
||
def perform | ||
load(object).send(method, *args.map{|a| load(a)}) | ||
rescue PerformableMethod::LoadError | ||
# We cannot do anything about objects that can't be loaded | ||
true | ||
end | ||
|
||
private | ||
|
||
def load(obj) | ||
if STRING_FORMAT === obj | ||
$1.constantize.load_for_delayed_job($2) | ||
else | ||
obj | ||
end | ||
rescue => e | ||
Delayed::Worker.logger.warn "Could not load object for job: #{e.message}" | ||
raise PerformableMethod::LoadError | ||
end | ||
|
||
def dump(obj) | ||
if obj.respond_to?(:dump_for_delayed_job) | ||
"LOAD;#{obj.dump_for_delayed_job}" | ||
else | ||
obj | ||
end | ||
object.send(method, *args) if object | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters