Skip to content

Commit 45e08b4

Browse files
committed
Clean up scheduler code.
1 parent 07d1c47 commit 45e08b4

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

myDevices/cloud/scheduler.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def add_scheduled_item(self, json_data, insert = False):
155155
except:
156156
exception('Error adding scheduled item')
157157
except:
158-
exception('AddScheduledItem failed')
158+
exception('Failed to add scheduled item')
159159
return result
160160

161161
def update_scheduled_item(self, json_data):
@@ -165,20 +165,20 @@ def update_scheduled_item(self, json_data):
165165
debug('Update scheduled item')
166166
result = False
167167
try:
168-
scheduleItemNew = ScheduleItem(json_data)
168+
new_item = ScheduleItem(json_data)
169169
with self.mutex:
170170
try:
171-
scheduleItemOld = self.schedules[scheduleItemNew.id]
172-
schedule.cancel_job(scheduleItemOld.job)
171+
old_item = self.schedules[new_item.id]
172+
schedule.cancel_job(old_item.job)
173173
except KeyError:
174-
debug('Old schedule with id = {} not found'.format(scheduleItemNew.id))
175-
result = self.setup(scheduleItemNew)
174+
debug('Old schedule with id = {} not found'.format(new_item.id))
175+
result = self.setup(new_item)
176176
debug('Update scheduled item result: {}'.format(result))
177177
if result == True:
178-
self.update_database_item(dumps(json_data), scheduleItemNew.id)
179-
self.schedules[scheduleItemNew.id] = scheduleItemNew
178+
self.update_database_item(dumps(json_data), new_item.id)
179+
self.schedules[new_item.id] = new_item
180180
except:
181-
exception('UpdateScheduledItem failed')
181+
exception('Failed to update scheduled item')
182182
return result
183183

184184
def setup(self, schedule_item):
@@ -215,35 +215,33 @@ def process_action(self, schedule_item):
215215
schedule_item: a ScheduleItem instance representing the item to process and run"""
216216
debug('')
217217
if schedule_item is None:
218-
error('ProcessAction with empty schedule')
218+
error('No scheduled item to run')
219219
return
220220
# if schedule_item.job.should_run() == False:
221221
# return
222-
statusSuccess = True
222+
result = True
223223
schedule_item.last_run = datetime.strftime(datetime.utcnow(), '%Y-%m-%d %H:%M')
224224
with self.mutex:
225225
self.update_database_item(schedule_item.to_json(), schedule_item.id)
226226
#TODO
227227
#all this scheduler notification should be put in a db and if it was not possible to submit add a checker for sending notifications to cloud
228228
#right now is a workaround in order to submit
229-
debug('Notification: ' + str(schedule_item.notify))
230-
if schedule_item.notify:
231-
body = 'Scheduler ' + schedule_item.title + ' ran with success: ' + str(statusSuccess) + ' at UTC ' + str(datetime.utcnow())
232-
subject = schedule_item.title
233-
#build an array of device names
234-
#if this fails to be sent, save it in the DB and resubmit it
235-
runStatus = False #self.client.SendNotification(schedule_item.notify, subject, body)
236-
sleep(1)
237-
if runStatus == False:
238-
error('Notification ' + str(schedule_item.notify) + ' was not sent')
229+
# debug('Notification: ' + str(schedule_item.notify))
230+
# if schedule_item.notify:
231+
# body = 'Scheduler ' + schedule_item.title + ' ran with success: ' + str(status_success) + ' at UTC ' + str(datetime.utcnow())
232+
# subject = schedule_item.title
233+
# #build an array of device names
234+
# #if this fails to be sent, save it in the DB and resubmit it
235+
# notified = False #self.client.SendNotification(schedule_item.notify, subject, body)
236+
# sleep(1)
237+
# if notified == False:
238+
# error('Notification ' + str(schedule_item.notify) + ' was not sent')
239239
for action in schedule_item.actions:
240-
#call cloudserver
241-
runStatus = self.client.RunAction(action)
242-
info('Schedule executing action: ' + str(action))
243-
if runStatus == False:
244-
error('Action: ' + str(action) + ' failed')
245-
statusSuccess = False
246-
if schedule_item.type == 'date' and statusSuccess == True:
240+
info('Executing scheduled action: {}'.format(action))
241+
result = self.client.RunAction(action)
242+
if result == False:
243+
error('Failed to execute action: {}'.format(action))
244+
if schedule_item.type == 'date' and result == True:
247245
with self.mutex:
248246
schedule.cancel_job(schedule_item.job)
249247

0 commit comments

Comments
 (0)