Class: AutomateEm::ScheduleProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/automate-em/utilities.rb

Instance Method Summary collapse

Constructor Details

#initializeScheduleProxy

Returns a new instance of ScheduleProxy.



51
52
53
54
55
# File 'lib/automate-em/utilities.rb', line 51

def initialize
	@jobs = {}
	@index = 0
	@job_lock = Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



70
71
72
73
74
75
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
# File 'lib/automate-em/utilities.rb', line 70

def method_missing(name, *args, &block)
			@job_lock.synchronize do
				if block.present?
					job = nil
					
					if [:in, :at].include?(name)
						index = @index				# local variable for the block
						
						job = AutomateEm::scheduler.send(name, *args) do
							begin
block.call
							rescue => e
AutomateEm.print_error(System.logger, e, :message => "Error in one off scheduled event")
							ensure
@job_lock.synchronize do
	@jobs.delete(index)
end
							end
						end
					else
						job = AutomateEm::scheduler.send(name, *args) do
							begin
block.call
							rescue => e
AutomateEm.print_error(System.logger, e, :message => "Error in repeated scheduled event")
							end
						end
					end
					
					if job.present?
						@jobs[@index] = job
						job = JobProxy.new(@jobs, @index, @job_lock)
						
						@index += 1
						
						return job
					end
					
					return nil
				else
					AutomateEm::scheduler.send(name, *args, &block)
				end
			end
end

Instance Method Details

#clear_jobsObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/automate-em/utilities.rb', line 57

def clear_jobs
	@job_lock.synchronize do
		@jobs.each_value do |job|
			job.unschedule
		end
		
		@jobs = {}
		@index = 0
	end
end