From 126a3415e57437922f70c79e5a3ff35b414b8c01 Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Mon, 24 Jun 2019 17:21:18 +0500 Subject: [PATCH] race condition removed + return message on success updated --- main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 908392b..55f1be0 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ app = Flask(__name__) api = Api(app) etcd_client = etcd.Client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT"))) +etcd_lock = etcd.Lock(etcd_client, 'etcd_lock') createvm_argparser = reqparse.RequestParser() createvm_argparser.add_argument("name", type=str, required=True) @@ -30,9 +31,10 @@ class CreateVM(Resource): vm_entry = {"owner": name, "specs": specs} - etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry) + with etcd_lock: + etcd_client.write(f"/v1/vm/{next_vm_id}", vm_entry) - return {'message': "VM Created"}, 200 + return {'message': "VM Creation Queued"}, 200 else: return {'message': 'Invalid Credentials'}, 400