Browse Source

HUE-3797 [scheduler] Update previous task is there when submitting a beat

Romain 6 years ago
parent
commit
5cba6a1131

+ 14 - 9
desktop/core/src/desktop/lib/scheduler/lib/beat.py

@@ -15,6 +15,8 @@
 # See the License for the specific language governing permissions and
 # See the License for the specific language governing permissions and
 # limitations under the License.
 # limitations under the License.
 
 
+import json
+
 from django_celery_beat.models import PeriodicTask, CrontabSchedule, IntervalSchedule
 from django_celery_beat.models import PeriodicTask, CrontabSchedule, IntervalSchedule
 
 
 from desktop.lib.scheduler.lib.api import Api
 from desktop.lib.scheduler.lib.api import Api
@@ -23,29 +25,32 @@ from desktop.lib.scheduler.lib.api import Api
 class CeleryBeatApi(Api):
 class CeleryBeatApi(Api):
 
 
   def submit_schedule(self, request, coordinator, mapping):
   def submit_schedule(self, request, coordinator, mapping):
-    is_cron = True
+    is_cron = True # IntervalSchedule is buggy https://github.com/celery/django-celery-beat/issues/279
 
 
-    if True:
+    if is_cron:
       schedule, created = CrontabSchedule.objects.get_or_create(
       schedule, created = CrontabSchedule.objects.get_or_create(
-        minute='*',
-        hour='*',
-        day_of_week='*',
-        day_of_month='*',
-        month_of_year='*'
+          minute='*',
+          hour='*',
+          day_of_week='*',
+          day_of_month='*',
+          month_of_year='*'
       )
       )
 
 
-      task, created = PeriodicTask.objects.get_or_create(
+      task = PeriodicTask.objects.update_or_create(
         crontab=schedule,
         crontab=schedule,
         name='Scheduled query N',
         name='Scheduled query N',
         task='notebook.tasks.run_sync_query',
         task='notebook.tasks.run_sync_query',
+        defaults={"args": json.dumps(['a7428a99-2f77-cf3a-ebbd-460f19ba46cc', request.user.username])},
       )
       )
+      task.enabled=True
+      task.save()
     else:
     else:
       schedule, created = IntervalSchedule.objects.get_or_create(
       schedule, created = IntervalSchedule.objects.get_or_create(
         every=15,
         every=15,
         period=IntervalSchedule.SECONDS,
         period=IntervalSchedule.SECONDS,
       )
       )
 
 
-      task, created = PeriodicTask.objects.get_or_create(
+      task, created = PeriodicTask.objects.update_or_create(
         interval=schedule,
         interval=schedule,
         name='Scheduled query',
         name='Scheduled query',
         task='notebook.tasks.run_sync_query',
         task='notebook.tasks.run_sync_query',

+ 3 - 0
desktop/libs/notebook/src/notebook/tasks.py

@@ -152,6 +152,9 @@ def run_sync_query(doc_id, user):
   # Add INSERT INTO table if persit result
   # Add INSERT INTO table if persit result
   # Add variables
   # Add variables
   # Return when done. send email notification. get taskid.
   # Return when done. send email notification. get taskid.
+  if type(user) is str:
+    user = User.objects.get(username=user)
+
   query_document = Document2.objects.document(user=user, doc_id=doc_id)
   query_document = Document2.objects.document(user=user, doc_id=doc_id)
   notebook = Notebook(document=query_document).get_data()
   notebook = Notebook(document=query_document).get_data()
   snippet = notebook['snippets'][0]
   snippet = notebook['snippets'][0]