Class: Hystrix::Command

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/hystrix/command.rb

Direct Known Subclasses

InlineCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Returns a new instance of Command.



17
18
19
20
# File 'lib/hystrix/command.rb', line 17

def initialize(*args)
	self.executor_pool = CommandExecutorPools.instance.get_pool(executor_pool_name, self.class.default_pool_size)
	self.circuit = self.executor_pool.circuit_supervisor.actors.first
end

Instance Attribute Details

#circuitObject

Returns the value of attribute circuit.



10
11
12
# File 'lib/hystrix/command.rb', line 10

def circuit
  @circuit
end

#executor_poolObject

Returns the value of attribute executor_pool.



10
11
12
# File 'lib/hystrix/command.rb', line 10

def executor_pool
  @executor_pool
end

Class Method Details

.default_pool_sizeObject



13
14
15
# File 'lib/hystrix/command.rb', line 13

def self.default_pool_size
	@default_pool_size
end

.pool_size(size) ⇒ Object



75
76
77
# File 'lib/hystrix/command.rb', line 75

def self.pool_size(size)
	@default_pool_size = size
end

Instance Method Details

#executeObject

Run the command synchronously



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hystrix/command.rb', line 23

def execute
	raise 'No executor pool found! Did you forget to call super in your initialize method?' unless executor_pool

	executor = nil
	start_time = Time.now

	begin
		raise CircuitClosedError unless self.circuit.is_closed?

		executor = executor_pool.take
		
		result = executor.run(self)
		duration = Time.now - start_time

		Configuration.notify_success(executor_pool_name, duration)
	rescue Exception => main_error
		duration = Time.now - start_time

		begin
			if main_error.respond_to?(:cause)
				result = fallback(main_error.cause)
				Configuration.notify_fallback(executor_pool_name, duration, main_error.cause)
			else
				result = fallback(main_error)
				Configuration.notify_fallback(executor_pool_name, duration, main_error)
			end
		rescue NotImplementedError => fallback_error
			Configuration.notify_failure(executor_pool_name, duration, main_error)
			raise main_error
		end
	ensure
		executor.unlock if executor
		self.terminate
	end

	return result
end

#executor_pool_nameObject

Commands which share the value of executor_pool_name will use the same pool



62
63
64
# File 'lib/hystrix/command.rb', line 62

def executor_pool_name
	@executor_pool_name || self.class.name
end

#fallback(error) ⇒ Object

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/hystrix/command.rb', line 71

def fallback(error)
	raise NotImplementedError
end

#queueObject

Run the command asynchronously



67
68
69
# File 'lib/hystrix/command.rb', line 67

def queue
	future.execute
end