def CreateOnceTask(self, event): """ Create and schedule task(s) according to the settings configured in the Schedule tab of the Settings dialog. """ scheduleType = "Once" logger.debug("Schedule type is Once.") def RunTaskOnce(event, jobId): """ Run a task once, on the date and time configured in the Schedule tab of the Settings dialog. """ app = wx.GetApp() wx.CallAfter(app.DisableTestAndUploadToolbarButtons) while not app.Processing(): time.sleep(0.01) needToValidateSettings = False wx.CallAfter(app.OnRefresh, event, needToValidateSettings, jobId) # Sleep this thread until the job is really # finished, so we can determine the job's # finish time. while app.Processing(): time.sleep(0.01) jobDesc = "Scan folders and upload datafiles" startTime = \ datetime.combine(self.settingsModel.GetScheduledDate(), self.settingsModel.GetScheduledTime()) if startTime < datetime.now(): delta = datetime.now() - startTime if delta.total_seconds() < 10: startTime = datetime.now() else: message = "Scheduled time is in the past." logger.error(message) if self.settingsModel.GetLastSettingsUpdateTrigger() != \ LastSettingsUpdateTrigger.READ_FROM_DISK: wx.MessageBox(message, "MyData", wx.ICON_ERROR) return timeString = startTime.strftime("%I:%M %p") dateString = \ "{d:%A} {d.day}/{d.month}/{d.year}".format(d=startTime) wx.GetApp().GetMainFrame().SetStatusMessage( "The \"%s\" task is scheduled " "to run at %s on %s" % (jobDesc, timeString, dateString)) taskDataViewId = self.tasksModel.GetMaxDataViewId() + 1 jobArgs = [event, taskDataViewId] task = TaskModel(taskDataViewId, RunTaskOnce, jobArgs, jobDesc, startTime, scheduleType=scheduleType) try: self.tasksModel.AddRow(task) except ValueError, err: wx.MessageBox(str(err), "MyData", wx.ICON_ERROR) return
def CreateTimerTask(self, event): """ Create and schedule task(s) according to the settings configured in the Schedule tab of the Settings dialog. """ scheduleType = "Timer" logger.debug("Schedule type is Timer.") def RunTaskOnTimer(event, jobId): """ Run a task every n minutes, where n is the interval specified in the Schedule tab of the Settings dialog. """ app = wx.GetApp() wx.CallAfter(app.DisableTestAndUploadToolbarButtons) while not app.Processing(): time.sleep(0.01) needToValidateSettings = False wx.CallAfter(app.OnRefresh, event, needToValidateSettings, jobId) # Sleep this thread until the job is really # finished, so we can determine the job's # finish time. while app.Processing(): time.sleep(0.01) jobDesc = "Scan folders and upload datafiles" intervalMinutes = self.settingsModel.GetTimerMinutes() if self.settingsModel.GetLastSettingsUpdateTrigger() == \ LastSettingsUpdateTrigger.READ_FROM_DISK: startTime = datetime.now() + timedelta(seconds=5) else: # LastSettingsUpdateTrigger.UI_RESPONSE startTime = datetime.now() + timedelta(seconds=1) timeString = startTime.strftime("%I:%M:%S %p") dateString = \ "{d:%A} {d.day}/{d.month}/{d.year}".format(d=startTime) wx.GetApp().GetMainFrame().SetStatusMessage( "The \"%s\" task is scheduled " "to run at %s on %s (recurring every %d minutes)" % (jobDesc, timeString, dateString, intervalMinutes)) taskDataViewId = self.tasksModel.GetMaxDataViewId() + 1 jobArgs = [event, taskDataViewId] task = TaskModel(taskDataViewId, RunTaskOnTimer, jobArgs, jobDesc, startTime, scheduleType=scheduleType, intervalMinutes=intervalMinutes) try: self.tasksModel.AddRow(task) except ValueError, err: wx.MessageBox(str(err), "MyData", wx.ICON_ERROR) return
def CreateDailyTask(self, event): """ Create and schedule task(s) according to the settings configured in the Schedule tab of the Settings dialog. """ scheduleType = "Daily" logger.debug("Schedule type is Daily.") def RunTaskDaily(event, jobId): """ Run a task every day at the time specified in the Schedule tab of the Settings dialog. """ app = wx.GetApp() wx.CallAfter(app.DisableTestAndUploadToolbarButtons) while not app.Processing(): time.sleep(0.01) needToValidateSettings = False wx.CallAfter(app.OnRefresh, event, needToValidateSettings, jobId) # Sleep this thread until the job is really # finished, so we can determine the job's # finish time. while app.Processing(): time.sleep(0.01) jobDesc = "Scan folders and upload datafiles" startTime = \ datetime.combine(datetime.date(datetime.now()), self.settingsModel.GetScheduledTime()) if startTime < datetime.now(): startTime = startTime + timedelta(days=1) timeString = startTime.strftime("%I:%M %p") dateString = \ "{d:%A} {d.day}/{d.month}/{d.year}".format(d=startTime) wx.GetApp().GetMainFrame().SetStatusMessage( "The \"%s\" task is scheduled " "to run at %s on %s (recurring daily)" % (jobDesc, timeString, dateString)) taskDataViewId = self.tasksModel.GetMaxDataViewId() + 1 jobArgs = [event, taskDataViewId] task = TaskModel(taskDataViewId, RunTaskDaily, jobArgs, jobDesc, startTime, scheduleType=scheduleType) try: self.tasksModel.AddRow(task) except ValueError, err: wx.MessageBox(str(err), "MyData", wx.ICON_ERROR) return
def CreateManualTask(self, event, needToValidateSettings=True, testRun=False): """ Create and schedule task(s) according to the settings configured in the Schedule tab of the Settings dialog. """ scheduleType = "Manual" def RunTaskManually(event, jobId, needToValidateSettings=True): """ Task to run when the user manually asks MyData to being the data folder scans and uploads, usually by clicking the Refresh toolbar icon, or by selecting the task bar icon menu's "Sync Now" menu item. """ app = wx.GetApp() wx.CallAfter(app.DisableTestAndUploadToolbarButtons) while not app.Processing(): time.sleep(0.01) wx.CallAfter(app.OnRefresh, event, needToValidateSettings, jobId, testRun) # Sleep this thread until the job is really # finished, so we can determine the job's # finish time. while app.Processing(): time.sleep(0.01) jobDesc = "Scan folders and upload datafiles" startTime = datetime.now() + timedelta(seconds=1) timeString = startTime.strftime("%I:%M %p") dateString = \ "{d:%A} {d.day}/{d.month}/{d.year}".format(d=startTime) wx.GetApp().GetMainFrame().SetStatusMessage( "The \"%s\" task is scheduled " "to run at %s on %s" % (jobDesc, timeString, dateString)) taskDataViewId = self.tasksModel.GetMaxDataViewId() + 1 jobArgs = [event, taskDataViewId, needToValidateSettings] task = TaskModel(taskDataViewId, RunTaskManually, jobArgs, jobDesc, startTime, scheduleType=scheduleType) try: self.tasksModel.AddRow(task) except ValueError, err: wx.MessageBox(str(err), "MyData", wx.ICON_ERROR) return
def CreateOnStartupTask(self, event): """ Create and schedule task(s) according to the settings configured in the Schedule tab of the Settings dialog. """ scheduleType = "On Startup" logger.debug("Schedule type is %s." % scheduleType) def OnStartup(event, jobId): """ Task to be run automatically when MyData is launched. """ app = wx.GetApp() wx.CallAfter(app.DisableTestAndUploadToolbarButtons) while not app.Processing(): time.sleep(0.01) needToValidateSettings = False wx.CallAfter(app.OnRefresh, event, needToValidateSettings, jobId) # Sleep this thread until the job is really # finished, so we can determine the job's # finish time. while app.Processing(): time.sleep(0.01) jobDesc = "Scan folders and upload datafiles" # Wait a few seconds to give the user a chance to # read the initial MyData notification before # starting the task. startTime = datetime.now() + timedelta(seconds=5) timeString = startTime.strftime("%I:%M %p") dateString = \ "{d:%A} {d.day}/{d.month}/{d.year}".format(d=startTime) wx.GetApp().GetMainFrame().SetStatusMessage( "The \"%s\" task is scheduled " "to run at %s on %s" % (jobDesc, timeString, dateString)) taskDataViewId = self.tasksModel.GetMaxDataViewId() + 1 jobArgs = [event, taskDataViewId] task = TaskModel(taskDataViewId, OnStartup, jobArgs, jobDesc, startTime, scheduleType=scheduleType) try: self.tasksModel.AddRow(task) except ValueError, err: wx.MessageBox(str(err), "MyData", wx.ICON_ERROR) return
def TaskJobFunc(): assert callable(taskModel.GetJobFunc()) title = "Starting" message = taskModel.GetJobDesc() Notification.Notify(message, title=title) taskModel.GetJobFunc()(*taskModel.GetJobArgs()) taskModel.SetFinishTime(datetime.now()) title = "Finished" message = taskModel.GetJobDesc() Notification.Notify(message, title=title) wx.CallAfter(tasksModel.TryRowValueChanged, row, col) scheduleType = taskModel.GetScheduleType() if scheduleType == "Timer": intervalMinutes = taskModel.GetIntervalMinutes() newTaskDataViewId = tasksModel.GetMaxDataViewId() + 1 newStartTime = taskModel.GetStartTime() + \ timedelta(minutes=intervalMinutes) newTaskModel = TaskModel(newTaskDataViewId, taskModel.GetJobFunc(), taskModel.GetJobArgs(), taskModel.GetJobDesc(), newStartTime, scheduleType="Timer", intervalMinutes=intervalMinutes) timeString = newStartTime.strftime("%I:%M:%S %p") dateString = "{d:%A} {d.day}/{d.month}/{d.year}"\ .format(d=newStartTime) wx.CallAfter( wx.GetApp().frame.SetStatusMessage, "The \"%s\" task is scheduled " "to run at %s on %s " "(recurring every %d minutes)" % (taskModel.GetJobDesc(), timeString, dateString, intervalMinutes)) tasksModel.AddRow(newTaskModel) elif scheduleType == "Daily": newTaskDataViewId = tasksModel.GetMaxDataViewId() + 1 newStartTime = taskModel.GetStartTime() + \ timedelta(days=1) newTaskModel = TaskModel(newTaskDataViewId, taskModel.GetJobFunc(), taskModel.GetJobArgs(), taskModel.GetJobDesc(), newStartTime, scheduleType="Daily") timeString = newStartTime.strftime("%I:%M:%S %p") dateString = "{d:%A} {d.day}/{d.month}/{d.year}"\ .format(d=newStartTime) wx.CallAfter( wx.GetApp().frame.SetStatusMessage, "The \"%s\" task is scheduled " "to run at %s on %s " "(recurring daily)" % (taskModel.GetJobDesc(), timeString, dateString)) tasksModel.AddRow(newTaskModel) elif scheduleType == "Weekly": newTaskDataViewId = tasksModel.GetMaxDataViewId() + 1 newStartTime = taskModel.GetStartTime() + \ timedelta(days=1) days = taskModel.GetDays() while not days[newStartTime.weekday()]: newStartTime = newStartTime + timedelta(days=1) newTaskModel = TaskModel(newTaskDataViewId, taskModel.GetJobFunc(), taskModel.GetJobArgs(), taskModel.GetJobDesc(), newStartTime, scheduleType="Weekly", days=days) timeString = newStartTime.strftime("%I:%M:%S %p") dateString = "{d:%A} {d.day}/{d.month}/{d.year}"\ .format(d=newStartTime) wx.CallAfter( wx.GetApp().frame.SetStatusMessage, "The \"%s\" task is scheduled " "to run at %s on %s " "(recurring on specified days)" % (taskModel.GetJobDesc(), timeString, dateString)) tasksModel.AddRow(newTaskModel)