def add_job_in_crontab(alarm): """ use the crontab manager to add a new job :param alarm: AlarmClock :return: """ day_of_week = _get_day_of_week(alarm) current_script_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) run_script_path = get_root_path(current_script_path) + os.sep + "run_mp3_playback.py" # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <mp3playback_id> new_job = "%s %s * * %s python3.5 %s %s " % (alarm.minute, alarm.hour, day_of_week, run_script_path, alarm.mp3_playback.id) if alarm.auto_stop_seconds is not 0: # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <alarm_id> <auto_stop_seconds> new_job += "%s " % alarm.auto_stop_seconds # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <alarm_id> [<auto_stop_minute>] # alertrug<alarm_id> new_job += " > /tmp/cron.tmp 2> /tmp/cron2.tmp # alertrug%s" % alarm.id if not alarm.is_active: line = "# " new_job = line + new_job print( "Crontab job line: %s" % new_job ) CrontabManager.add_job(new_job)
def add_job_in_crontab(alarm): """ use the crontab manager to add a new job :param alarm: AlarmClock :return: """ day_of_week = _get_day_of_week(alarm) current_script_path = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) run_script_path = get_root_path( current_script_path) + os.sep + "run_web_radio.py" # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <webradio_id> new_job = "%s %s * * %s python %s %s " % (alarm.minute, alarm.hour, day_of_week, run_script_path, alarm.webradio.id) if alarm.auto_stop_minutes is not 0: # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <alarm_id> <auto_stop_minute> new_job += "%s " % alarm.auto_stop_minutes # new_job: <minute> <hour> * * <day_of_week> <run_script_path> <alarm_id> [<auto_stop_minute>] # piclodio<alarm_id> new_job += "# piclodio%s" % alarm.id if not alarm.is_active: line = "# " new_job = line + new_job print "Crontab job line: %s" % new_job CrontabManager.add_job(new_job)
def delete(self, request, pk, format=None): alarmclock = self.get_object(pk) # remove the line from the crontab job_comment = "piclodio%s" % alarmclock.id CrontabManager.remove_job(job_comment) # remove from the database alarmclock.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def delete(self, request, pk, format=None): webradio = self.get_object(pk) # when we delete a web radio, all alarm based on this on will be deleted to, remove them from the contab before all_alarms_which_use_the_web_radio_to_delete = AlarmClock.objects.filter(webradio=webradio) for alarm in all_alarms_which_use_the_web_radio_to_delete: # remove the job from the crontab job_comment = "piclodio%s" % alarm.id CrontabManager.remove_job(job_comment) # then we can safely delete the web radio webradio.delete() return Response(status=status.HTTP_204_NO_CONTENT)
def put(self, request, pk, format=None): alarmclock = self.get_object(pk) serializer = AlarmClockSerializer(alarmclock, data=request.data) if serializer.is_valid(): serializer.save() updated_alarmclock = AlarmClock.objects.get(pk=pk) # remove the job from the crontab job_comment = "piclodio%s" % updated_alarmclock.id CrontabManager.remove_job(job_comment) # add it back with new info Utils.add_job_in_crontab(updated_alarmclock) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)