Nicolas Marcq 8 роки тому
батько
коміт
e6d52238b2
2 змінених файлів з 54 додано та 7 видалено
  1. 19 6
      core/CrontabManager.py
  2. 35 1
      test.py

+ 19 - 6
core/CrontabManager.py

@@ -3,6 +3,7 @@ from crontab import CronSlices
 
 from core.ConfigurationManager.BrainLoader import BrainLoader
 from core.Models import Event
+import logging
 
 
 class InvalidCrontabPeriod(Exception):
@@ -19,16 +20,20 @@ class CrontabManager:
         self.brain = BrainLoader(filename=brain_file).get_brain()
 
     def load_events_in_crontab(self):
+        """
+        Remove all line in crontab with the CRONTAB_COMMENT
+        Then add back line from event in the brain.yml
+        :return:
+        """
         # clean the current crontab from all jarvis event
         self._remove_all_jarvis_job()
-        # # load the brain file
+        # load the brain file
         for synapse in self.brain.synapes:
             for signal in synapse.signals:
-                print signal
-                # if it's an event we add it to the crontab
+                # print signal
+                # if the signal is an event we add it to the crontab
                 if type(signal) == Event:
-                    print "is event"
-                    # for all tasks with an event, we add the task id to the crontab
+                    # for all synapse with an event, we add the task id to the crontab
                     self._add_event(period_string=signal.period, event_id=signal.identifier)
 
     def _add_event(self, period_string, event_id):
@@ -52,6 +57,14 @@ class CrontabManager:
         """
         iter = self.my_user_cron.find_comment(CRONTAB_COMMENT)
         for job in iter:
+            logging.debug("remove job %s from crontab" % job)
             self.my_user_cron.remove(job)
         # write the file
-        self.my_user_cron.write()
+        self.my_user_cron.write()
+
+        # this is a fix for the CronTab lib
+        # see https://github.com/peak6/python-crontab/issues/1
+        new_iter = self.my_user_cron.find_comment(CRONTAB_COMMENT)
+        sum_job = sum(1 for _ in new_iter)
+        while sum_job > 0:
+            self._remove_all_jarvis_job()

+ 35 - 1
test.py

@@ -11,7 +11,7 @@ from neurons.ansible_tasks.ansible_tasks import Ansible_tasks
 import logging
 
 from core import ShellGui
-from crontab import CronSlices
+from crontab import CronSlices, CronTab
 
 # oa = OrderAnalyser("dis bonjour", brain_file="test.yml")
 #
@@ -21,4 +21,38 @@ from crontab import CronSlices
 cron_manager = CrontabManager(brain_file="test.yml")
 cron_manager.load_events_in_crontab()
 
+# command = "/path/to/my/command"
+# comment = "JARVIS"
+# period_string = "* * 5 5 *"
+#
+# my_user_cron = CronTab(user=True)
+#
+# for x in range(0, 5, 1):
+#     job = my_user_cron.new(command=command, comment=comment)
+#     if CronSlices.is_valid(period_string):
+#         job.setall(period_string)
+#         job.enable()
+#     my_user_cron.write()
 
+# # here we have:
+# # * * 5 5 * /path/to/my/command # SAMECOMMENT
+# # * * 5 5 * /path/to/my/command # SAMECOMMENT
+# # * * 5 5 * /path/to/my/command # SAMECOMMENT
+#
+# iter = my_user_cron.find_comment(comment)
+# for job in iter:
+#     print "remove job %s" % job
+#     my_user_cron.remove(job)
+# my_user_cron.write()
+#
+# # now we check the content
+# new_iter = my_user_cron.find_comment(comment)
+# # for job in iter:
+# #     print "Still a job: %s" % job
+# vsum_job = sum(1 for _ in new_iter)
+# print vsum_job
+#
+# # output
+# # remove job * * 5 5 * /path/to/my/command # SAMECOMMENT
+# # remove job * * 5 5 * /path/to/my/command # SAMECOMMENT
+# # Still a job: * * 5 5 * /path/to/my/command # SAMECOMMENT