使用也有大半年了。稳定性没话说啊。
但有一个坑,是我以前没注意的,记录下来。
就是本来一个任务是可以异步并行执行的。、但如何需要CELERY的执行结果来作判断的话,就会变得异步串行的。
这要值得注意。
比如以下这段代码的实现,开始,我用RESULT结果判断再来更新数据库,就变成串行的,一次只能启动一个worker,
于是,我将更新数据库放到同一个deplay函数之后,就可以并行执行啦。
def rsync_dir(site_name, app_name, deploy_version): file_path = "/xxx/autodeploy/" + site_name + "/" + app_name + "/" + deploy_version + "/" src_file_name = file_count_name(file_path) file_name = os.path.basename(src_file_name) md5_value = md5sum(src_file_name) DeployPool.objects.filter(name=deploy_version).update(md5_value=md5_value) subserver_set = SubServer.objects.filter(app_name__name=app_name) for item in subserver_set: salt_cmd_run.delay(site_name, app_name, deploy_version, item.id, md5_value, file_name) @celery_app.task(time_limit=360) def salt_cmd_run(site_name, app_name, deploy_version, subserver_id, md5_value, file_name): result = "" item = SubServer.objects.get(id=subserver_id) tgt = item.server_ip.name op_name = item.op_name server_env = item.server_ip.server_env server_sys = item.server_ip.server_sys if server_sys == "WINDOWS": rsync_to_path = "D:\\var\\ops\\autodeploy\\%s\\%s\\%s" % (site_name, app_name, deploy_version) else: cmd_str = "rsync -avP --block-size=512 --timeout=600 --checksum --port=4505 " + "rsync://1//prism/%s/%s/%s/ /var/ops/autodeploy/%s/%s/%s/" % (site_name, app_name, deploy_version, site_name, app_name, deploy_version) result = saltapi_inst(server_env).cmd_run(tgt, cmd_str) print result, ‘========rsync result===============‘ if "error" not in str(result): if server_sys == "LINUX": tgt_file_name = "/xxx/autodeploy/%s/%s/%s/%s" % (site_name, app_name, deploy_version, file_name) cmd = "md5sum %s" % tgt_file_name result = saltapi_inst(server_env).cmd_run(tgt, cmd) if md5_value in str(result): item.rsync_status = True item.save() else: item.rsync_status = False item.save() cmd = "chown -R %s /var/ops" % op_name saltapi_inst(server_env).cmd_run(tgt, cmd)
时间: 2024-10-09 23:26:09