Class: Pushpop::Job
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
        show all
      
    
    - Defined in:
- lib/pushpop/job.rb
 
  
    
      Constant Summary
      collapse
    
    
      
        - @@plugins =
          
        
- {}
Instance Attribute Summary collapse
  
  
    
      Class Method Summary
      collapse
    
    
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(name = nil, &block)  ⇒ Job 
  
  
  
  
    
Returns a new instance of Job.
   
 
  
  
    | 
27
28
29
30
31
32 | # File 'lib/pushpop/job.rb', line 27
def initialize(name=nil, &block)
  self.name = name || Pushpop.random_name
  self.steps = []
  self.every_options = {}
  self.instance_eval(&block)
end | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(method, *args, &block)  ⇒ Object 
  
  
  
  
    | 
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 | # File 'lib/pushpop/job.rb', line 93
def method_missing(method, *args, &block)
  plugin_class = self.class.plugins[method.to_s]
  unless plugin_class
    begin
      Pushpop.load_plugin method.to_s
      plugin_class = self.class.plugins[method.to_s]
      raise "Plugin not loaded: #{method.to_s}" if plugin_class.nil?
    rescue LoadError
      Pushpop.logger.warn("Could not find plugin #{method.to_s}")
    end
  end
  name = args[0]
  plugin = method.to_s
  if plugin_class
    step(name, plugin, &block)
  else
    super
  end
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #every_options  ⇒ Object 
  
  
  
  
    
Returns the value of attribute every_options.
   
 
  
  
    | 
24
25
26 | # File 'lib/pushpop/job.rb', line 24
def every_options
  @every_options
end | 
 
    
      
      
      
  
  
    #name  ⇒ Object 
  
  
  
  
    
Returns the value of attribute name.
   
 
  
  
    | 
20
21
22 | # File 'lib/pushpop/job.rb', line 20
def name
  @name
end | 
 
    
      
      
      
  
  
    #period  ⇒ Object 
  
  
  
  
    
Returns the value of attribute period.
   
 
  
  
    | 
21
22
23 | # File 'lib/pushpop/job.rb', line 21
def period
  @period
end | 
 
    
      
      
      
  
  
    #steps  ⇒ Object 
  
  
  
  
    
Returns the value of attribute steps.
   
 
  
  
    | 
25
26
27 | # File 'lib/pushpop/job.rb', line 25
def steps
  @steps
end | 
 
    
      
      
      
  
  
    #webhook_proc  ⇒ Object 
  
  
  
  
    
Returns the value of attribute webhook_proc.
   
 
  
  
    | 
23
24
25 | # File 'lib/pushpop/job.rb', line 23
def webhook_proc
  @webhook_proc
end | 
 
    
      
      
      
  
  
    #webhook_url  ⇒ Object 
  
  
  
  
    
Returns the value of attribute webhook_url.
   
 
  
  
    | 
22
23
24 | # File 'lib/pushpop/job.rb', line 22
def webhook_url
  @webhook_url
end | 
 
    
   
  
    Class Method Details
    
      
  
  
    .plugins  ⇒ Object 
  
  
  
  
    | 
9
10
11 | # File 'lib/pushpop/job.rb', line 9
def plugins
  @@plugins
end | 
 
    
      
  
  
    .register_plugin(name, klass)  ⇒ Object 
  
  
  
  
    | 
13
14
15
16 | # File 'lib/pushpop/job.rb', line 13
def register_plugin(name, klass)
  self.plugins ||= {}
  self.plugins[name.to_s] = klass
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #add_step(step)  ⇒ Object 
  
  
  
  
    | 
61
62
63 | # File 'lib/pushpop/job.rb', line 61
def add_step(step)
  self.steps.push(step)
end | 
 
    
      
  
  
    #every(period, options = {})  ⇒ Object 
  
  
  
  
    | 
34
35
36
37 | # File 'lib/pushpop/job.rb', line 34
def every(period, options={})
  self.period = period
  self.every_options = options
end | 
 
    
      
  
  
    #run(last_response = nil, step_responses = {})  ⇒ Object 
  
  
  
  
    | 
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 | # File 'lib/pushpop/job.rb', line 75
def run(last_response = nil, step_responses = {})
  self.steps.each do |step|
        last_response = step.run(last_response, step_responses)
    step_responses[step.name] = last_response
        return unless last_response
  end
    Pushpop.logger.debug("#{name}: #{step_responses}")
    [last_response, step_responses]
end | 
 
    
      
  
  
    #schedule  ⇒ Object 
  
  
  
  
    | 
65
66
67
68
69
70
71
72
73 | # File 'lib/pushpop/job.rb', line 65
def schedule
  raise 'Set job period via "every"' unless self.period || @webhook_url
  if self.period
    Clockwork.manager.every(period, name, every_options) do
      run
    end
  end
end | 
 
    
      
  
  
    #step(name = nil, plugin = nil, &block)  ⇒ Object 
  
  
  
  
    | 
49
50
51
52
53
54
55
56
57
58
59 | # File 'lib/pushpop/job.rb', line 49
def step(name=nil, plugin=nil, &block)
  if plugin
    plugin_klass = self.class.plugins[plugin]
    raise "No plugin configured for #{plugin}" unless plugin_klass
    self.add_step(plugin_klass.new(name, plugin, &block))
  else
    self.add_step(Step.new(name, plugin, &block))
  end
end | 
 
    
      
  
  
    #webhook(url, &block)  ⇒ Object 
  
  
  
  
    | 
39
40
41
42
43
44
45
46
47 | # File 'lib/pushpop/job.rb', line 39
def webhook(url, &block)
  raise 'Webhook is already set' if @webhook_url
  raise 'Webhook must be set before steps' if self.steps.length > 0
  
  self.webhook_url = url
  self.webhook_proc = block
  Pushpop.web.add_route url, self
end |