Пример #1
0
 def celery_task_add(self):
     content_str = "INFO celery tast commit ... \n\r"
     msg = json.dumps("%s %s" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'),content_str),ensure_ascii=False).encode('utf-8')
     self.request.websocket.send(msg)
     time01 = self.message_dic_format["celery_schedule_time"]
     if time01 == "":
         self.sendmsg("warning:请输入计划执行时间   \n\r")
     else:
         time01 = datetime.strptime(time01, "%Y-%m-%d-%H:%M:%S")
         local_tz = pytz.timezone(celery_app.conf['CELERY_TIMEZONE'])
         eta_time = local_tz.localize(datetime.strptime(str(time01).strip(), '%Y-%m-%d %H:%M:%S'))
         taskname_dic = {"main_task":self.obj.main_task,"post_task":self.obj.post_task}
         taskname_dic = json.dumps(taskname_dic)
         if self.obj.var_built_in:
             var_built_in_dic = eval(self.obj.var_built_in) 
         else:
             var_built_in_dic = {}
         var_built_in_dic = json.dumps(var_built_in_dic)
         user_vars_dic = json.dumps(self.user_vars_dic)
         try:
             task01 = schedule_task.apply_async((taskname_dic,var_built_in_dic,user_vars_dic,self.config_center_dic), eta=eta_time)
             self.message_dic_format["celery_task_id"]=task01.id    
             self.message_dic_format["status"] = "CREATED"
             self.message_dic_format["celery_schedule_time"] = time01
             WorkOrderFlow.objects.create(**self.message_dic_format)
             content_str = "INFO The Job Finished:The scheduled task is created successfully \n\r"
             self.sendmsg(content_str)
             self.log("info", content_str)
         except Exception as e:
             self.request.websocket.send("%s %s" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'),e))
             self.log("info", e)
             content_str = "ERROR The Job Failed:the scheduled-task created fialed,for more details,pls see the celery log \n\r"
             self.sendmsg(content_str)
             self.log("info", content_str)
Пример #2
0
 def celery_bgtask_add(self):
     self.sendmsg("INFO celery tast commit ... \n\r")
     time_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
   
     eta_time = format_celery_eta_time(str(time_now))
     taskname_dic = {"main_task":self.obj.main_task,"post_task":self.obj.post_task}
     taskname_dic = json.dumps(taskname_dic)
     if self.obj.var_built_in:
         var_built_in_dic = eval(self.obj.var_built_in) 
     else:
         var_built_in_dic = {}
     var_built_in_dic = json.dumps(var_built_in_dic)
     user_vars_dic = json.dumps(self.user_vars_dic)
     try:
         task01 = schedule_task.apply_async((taskname_dic,var_built_in_dic,user_vars_dic,self.config_center_dic), eta=eta_time,serializer='json')
         self.message_dic_format["celery_task_id"]=task01.id    
         self.message_dic_format["status"] = "CREATED"
         self.message_dic_format["celery_schedule_time"] = time_now
         WorkOrderFlow.objects.create(**self.message_dic_format)
         content_str = "INFO The Job Finished:The backgroud-task is created successfully \n\r"
         self.sendmsg(content_str)
         self.log("info", content_str)
     except Exception as e:
         self.request.websocket.send("%s %s" % (datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'),e))
         self.log("info", e)
         content_str = "ERROR The Job Failed:the background-task created faled,for more details,pls see the celery log \n\r"
         self.sendmsg(content_str)
         self.log("info", content_str)
Пример #3
0
    def celery_task_add(self):
        obj_WorkOrder = WorkOrder.objects.get(id=self.obj.workorder_id)
        taskname_dic = {
            "main_task": obj_WorkOrder.main_task,
            "post_task": obj_WorkOrder.post_task
        }
        taskname_dic = json.dumps(taskname_dic)
        if obj_WorkOrder.var_built_in:
            var_built_in_dic = json.dumps(eval(obj_WorkOrder.var_built_in))
        else:
            var_built_in_dic = {}
            var_built_in_dic = json.dumps(var_built_in_dic)
        user_vars_dic = eval(self.obj.user_vars)
        user_vars_dic = json.dumps(user_vars_dic)
        if self.obj.celery_schedule_time:
            eta_time = format_celery_eta_time(
                str(self.obj.celery_schedule_time))
        else:
            eta_time = format_celery_eta_time(
                str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        task01 = schedule_task.apply_async(
            (taskname_dic, var_built_in_dic, user_vars_dic,
             self.config_center_dic),
            eta=eta_time)

        self.obj.celery_task_id = task01.id
        self.obj.status = "CREATED"
        self.obj.save()
        content_str = "添加celery任务成功"
        self.log_celery_id("info", content_str)