Class: Proby::ProbyTask

Inherits:
ProbyHttpApi show all
Defined in:
lib/proby/proby_task.rb

Overview

Represents a task in Proby

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ProbyTask

Should not be called directly



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/proby/proby_task.rb', line 65

def initialize(attributes={})
  @name = attributes['name']
  @api_id = attributes['api_id']
  @crontab = attributes['crontab']
  @paused = attributes['paused']
  @time_zone = attributes['time_zone']
  @machine = attributes['machine']
  @finish_alarms_enabled = attributes['finish_alarms_enabled']
  @maximum_run_time = attributes['maximum_run_time']
  @start_notification_grace_period = attributes['start_notification_grace_period']
  @consecutive_alarmed_tasks = attributes['consecutive_alarmed_tasks']
  @consecutive_alarmed_tasks_required_to_trigger_alarm = attributes['consecutive_alarmed_tasks_required_to_trigger_alarm']
  @created_at = Chronic.parse attributes['created_at']
  @updated_at = Chronic.parse attributes['updated_at']
  @status = ProbyTaskStatus.new(attributes['status']) if attributes['status']
end

Instance Attribute Details

#api_idObject (readonly)

The API Task ID of the task



47
48
49
# File 'lib/proby/proby_task.rb', line 47

def api_id
  @api_id
end

#consecutive_alarmed_tasksObject (readonly)

The number of consecutive times this task has triggered an alarm



53
54
55
# File 'lib/proby/proby_task.rb', line 53

def consecutive_alarmed_tasks
  @consecutive_alarmed_tasks
end

#consecutive_alarmed_tasks_required_to_trigger_alarmObject

The number of consecutive tasks that must fail before an alarm is sent



44
45
46
# File 'lib/proby/proby_task.rb', line 44

def consecutive_alarmed_tasks_required_to_trigger_alarm
  @consecutive_alarmed_tasks_required_to_trigger_alarm
end

#created_atObject (readonly)

The date and time this task was created



56
57
58
# File 'lib/proby/proby_task.rb', line 56

def created_at
  @created_at
end

#crontabObject

The schedule for the task, specified in crontab format



23
24
25
# File 'lib/proby/proby_task.rb', line 23

def crontab
  @crontab
end

#finish_alarms_enabledObject

Should finish alarms be sent when the task runs longer than expected?



32
33
34
# File 'lib/proby/proby_task.rb', line 32

def finish_alarms_enabled
  @finish_alarms_enabled
end

#machineObject

The name of the machine that is responsible for running this task



29
30
31
# File 'lib/proby/proby_task.rb', line 29

def machine
  @machine
end

#maximum_run_timeObject

The maximum amount of time the task is allowed to run before Proby sends a finish alarm. If not specified, Proby will determine when an alarm should be sent based on past run times



37
38
39
# File 'lib/proby/proby_task.rb', line 37

def maximum_run_time
  @maximum_run_time
end

#nameObject

The name of the task



20
21
22
# File 'lib/proby/proby_task.rb', line 20

def name
  @name
end

#pausedObject (readonly)

Is the task currently paused?



50
51
52
# File 'lib/proby/proby_task.rb', line 50

def paused
  @paused
end

#start_notification_grace_periodObject

The number of minutes to wait for a task to send its start notification after it should have started before sending an alarm



41
42
43
# File 'lib/proby/proby_task.rb', line 41

def start_notification_grace_period
  @start_notification_grace_period
end

#statusObject (readonly)

The current status of the task, represented as a ProbyTaskStatus



62
63
64
# File 'lib/proby/proby_task.rb', line 62

def status
  @status
end

#time_zoneObject

The time zone of the machine executing the task



26
27
28
# File 'lib/proby/proby_task.rb', line 26

def time_zone
  @time_zone
end

#updated_atObject (readonly)

The date and time this task was updated



59
60
61
# File 'lib/proby/proby_task.rb', line 59

def updated_at
  @updated_at
end

Class Method Details

.create(attributes = {}) ⇒ ProbyTask

Create a new Proby task.

Examples:

proby_task = ProbyTask.create(:name => "My new task", :crontab => "* * * * *")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes for your task.

Options Hash (attributes):

  • :name (String)

    A name for your task.

  • :crontab (String)

    The schedule of the task, specified in cron format.

  • :time_zone (String)

    (Optional) The time zone of the machine executing the task.

  • :machine (String)

    (Optional) The name of the machine that is responsible for running this task. Will default to the default time zone configured in Proby if not specified.

  • :finish_alarms_enabled (Boolean)

    (Optional) true if you would like to receive finish alarms for this task, false otherwise (default: true).

  • :maximum_run_time (Fixnum)

    (Optional) The maximum amount of time the task is allowed to run before Proby sends a finish alarm. If not specified, Proby will determine when an alarm should be sent based on past run times.

  • :start_notification_grace_period (Fixnum)

    (Optional) The number of minutes to wait for a task to send its start notification after it should have started before sending an alarm.

  • :consecutive_alarmed_tasks_required_to_trigger_alarm (Fixnum)

    (Optional) The number of consecutive tasks that must fail before an alarm is sent.

Returns:

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/proby/proby_task.rb', line 119

def self.create(attributes={})
  ensure_api_key_set
  raise InvalidParameterException.new("attributes are required") if attributes.nil? || attributes.empty?
  raise InvalidParameterException.new("name is required") unless !blank?(attributes[:name]) || !blank?(attributes['name'])
  raise InvalidParameterException.new("crontab is required") unless !blank?(attributes[:crontab]) || !blank?(attributes['crontab'])

  Proby.logger.info "Creating task with attributes: #{attributes.inspect}"
  response = post("/api/v1/tasks.json",
                  :format => :json,
                  :body => MultiJson.encode(:task => attributes),
                  :headers => default_headers)

  if response.code == 201 
    new(response.parsed_response['task'])
  else
    handle_api_failure(response)
  end 
end

.find(param) ⇒ Object

Get a single, or all tasks from Proby.

Examples:

all_of_my_tasks = ProbyTask.find(:all)
my_task = ProbyTask.find('my_proby_task_api_id')

Parameters:

  • param (Object)

    :all if you are fetching all tasks, or the api_id of the Proby task you would like to fetch.

Returns:

  • If requesting all tasks, an [Array<ProbyTask>] will be returned. If an api_id was provided, the ProbyTask with that api_id will be returned if it exists, or nil if it could not be found.



92
93
94
95
# File 'lib/proby/proby_task.rb', line 92

def self.find(param)
  ensure_api_key_set
  param == :all ? list : fetch(param)
end

Instance Method Details

#deleteObject

Delete a Proby task. The object will be frozen after the delete.

Examples:

proby_task = ProbyTask.get('my_proby_task_api_id')
proby_task.delete


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/proby/proby_task.rb', line 180

def delete
  self.class.ensure_api_key_set

  Proby.logger.info "Deleting task #{@api_id}"
  response = self.class.delete("/api/v1/tasks/#{@api_id}.json",
                               :format => :json,
                               :headers => self.class.default_headers)

  if response.code == 200 
    self.freeze
    true
  else
    self.class.handle_api_failure(response)
  end 
end

#pauseObject

Pause a Proby task.

Examples:

proby_task = ProbyTask.get('my_proby_task_api_id')
proby_task.pause


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/proby/proby_task.rb', line 201

def pause
  self.class.ensure_api_key_set

  Proby.logger.info "Pausing task #{@api_id}"
  response = self.class.post("/api/v1/tasks/#{@api_id}/pause.json",
                             :format => :json,
                             :headers => self.class.default_headers)

  if response.code == 200 
    @paused = true
    true
  else
    self.class.handle_api_failure(response)
  end 
end

#saveObject

Saves the task in Proby, updating all attributes to the values stored in the object. Only the attributes specified in the ProbyTask.create documentation can be updated.

Examples:

proby_task = ProbyTask.get('my_proby_task_api_id')
proby_task.name = "Some other name"
proby_task.crontab = "1 2 3 4 5"
proby_task.save

Raises:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/proby/proby_task.rb', line 146

def save
  self.class.ensure_api_key_set
  raise InvalidParameterException.new("name is required") if self.class.blank?(@name)
  raise InvalidParameterException.new("crontab is required") if self.class.blank?(@crontab)

  attributes = {
    :name => @name,
    :crontab => @crontab,
    :time_zone => @time_zone,
    :machine => @machine,
    :finish_alarms_enabled => @finish_alarms_enabled,
    :maximum_run_time => @maximum_run_time,
    :start_notification_grace_period => @start_notification_grace_period,
    :consecutive_alarmed_tasks_required_to_trigger_alarm => @consecutive_alarmed_tasks_required_to_trigger_alarm
  }

  Proby.logger.info "Updating task #{@api_id} with attributes: #{attributes.inspect}"
  response = self.class.put("/api/v1/tasks/#{@api_id}.json",
                            :format => :json,
                            :body => MultiJson.encode(:task => attributes),
                            :headers => self.class.default_headers)

  if response.code == 200
    true
  else
    self.class.handle_api_failure(response)
  end 
end

#unpauseObject

Unpause a Proby task.

Examples:

proby_task = ProbyTask.get('my_proby_task_api_id')
proby_task.unpause


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/proby/proby_task.rb', line 222

def unpause
  self.class.ensure_api_key_set

  Proby.logger.info "Unpausing task #{@api_id}"
  response = self.class.post("/api/v1/tasks/#{@api_id}/unpause.json",
                             :format => :json,
                             :headers => self.class.default_headers)

  if response.code == 200 
    @paused = false
    true
  else
    self.class.handle_api_failure(response)
  end 
end