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.



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

def initialize
  @jobs = {}
  @index = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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
114
115
116
117
# File 'lib/automate-em/utilities.rb', line 71

def method_missing(name, *args, &block)
      EM.schedule do
        begin
          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
  EM.schedule 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
        rescue
        end
      end
end

Instance Method Details

#clear_jobsObject



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

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