Module: Redis::EM::Mutex::Macro::ClassMethods
- Defined in:
- lib/redis/em-mutex/macro.rb
Instance Method Summary collapse
-
#auto_mutex(*args) ⇒ Object
auto_mutex [*method_names], [options].
- #auto_mutex_method_added(target) ⇒ Object
- #method_added(target) ⇒ Object
-
#no_auto_mutex ⇒ Object
Switch off implicit auto_mutex.
Instance Method Details
#auto_mutex(*args) ⇒ Object
auto_mutex [*method_names], [options]
options are:
-
:expire - see Mutex.new
-
:block - see Mutex.new
-
:ns - custom namespace, if not present name of the class that includes Macro is used
-
:on_timeout - if defined, this proc/method will be called instead of raising MutexTimeout error
If method_names are provided (names of already defined methods or defined in the future) they become protected with mutex.
If options are provided wihout method_names, they will become default options for subsequent auto_mutex calls.
If auto_mutex is called without arguments then any method further defined will also be protected. To disable implicit auto_mutex use no_auto_mutex.
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/redis/em-mutex/macro.rb', line 49 def auto_mutex(*args) = args.last.kind_of?(Hash) ? args.pop : {} if args.each {|target| self.auto_mutex_methods[target] = self..merge() auto_mutex_method_added(target) if method_defined? target }.empty? if .empty? self.auto_mutex_enabled = true else self..update() end end end |
#auto_mutex_method_added(target) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/redis/em-mutex/macro.rb', line 76 def auto_mutex_method_added(target) without_method = "#{target}_without_auto_mutex" with_method = "#{target}_with_auto_mutex" timeout_method = "#{target}_on_timeout_auto_mutex" return if method_defined?(without_method) = self.auto_mutex_methods[target] || self. mutex = nil on_timeout = [:on_timeout] || [:after_failure] if on_timeout.respond_to?(:call) define_method(timeout_method, &on_timeout) elsif on_timeout.is_a?(Symbol) timeout_method = on_timeout end define_method(with_method) do |*args, &blk| mutex||= Redis::EM::Mutex.new target, response = nil begin if mutex.refresh response = __send__(without_method, *args, &blk) else mutex.synchronize do response = __send__(without_method, *args, &blk) end end rescue Redis::EM::Mutex::MutexTimeout => e if respond_to?(timeout_method) response = __send__(timeout_method, *args, &blk) else raise e end end response end alias_method without_method, target alias_method target, with_method end |
#method_added(target) ⇒ Object
70 71 72 73 74 |
# File 'lib/redis/em-mutex/macro.rb', line 70 def method_added(target) return if target.to_s =~ /_(?:with|without|on_timeout)_auto_mutex$/ return unless self.auto_mutex_methods[target] || self.auto_mutex_enabled auto_mutex_method_added(target) end |
#no_auto_mutex ⇒ Object
Switch off implicit auto_mutex. After calling no_auto_mutex if any new method is defined it won’t be protected unless explicitely specified with auto_mutex.
66 67 68 |
# File 'lib/redis/em-mutex/macro.rb', line 66 def no_auto_mutex self.auto_mutex_enabled = false end |