Skip to content

Commit f9f653f

Browse files
authored
Merge pull request #18 from aprajshekhar/ap_CCS-4459_clone_async_pr
CCS-4459: Make repo clone function in git2pantheon async
2 parents d97a682 + e1c14aa commit f9f653f

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,32 @@ cd git2pantheon
1515
```
1616
3. Create and activate a virtual environment.
1717
```
18-
python3 -m venv .
18+
python3 -m venv venv
1919
```
2020
```
21-
venv/bin/activate
21+
source venv/bin/activate
2222
```
2323

2424
4. Install the requirements.
2525
```
26-
pip3 install --user -r requirements.txt
26+
pip3 install .
2727
```
2828
5. Set and export the following environment variables
2929
a. PANTHEON_SERVER
3030
b. UPLOADER_PASSWORD
3131
c. UPLOADER_USER
3232

33-
6. Run the service.
33+
34+
6. Set the app name for flask run command
35+
```
36+
export FLASK_APP=git2pantheon
37+
```
38+
39+
7. Run the service.
3440
```
35-
python3 git2panthenon/wsgi.py
41+
flask run
3642
```
3743

38-
7. The swagger docs can be found at:
44+
8. The swagger docs can be found at:
3945
http://localhost:5000/apidocs/
4046

git2pantheon/api/upload.py

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,37 @@ def push_repo():
3434
raise ApiError(message="Error in cloning the repo", status_code=400,
3535
details="Error cloning the repo due to invalid repo URL")
3636
parsed_url = GitHelper.parse_git_url(repo.repo)
37+
# if the key is already present, status API could give stale data
38+
# resetting the keys' values mitigates the issue
39+
reset_if_exists(parsed_url.name)
3740

38-
cloned_repo = clone_repo(parsed_url, repo)
39-
40-
logger.info("starting upload of repo=" + repo.repo + " (local dir=" + cloned_repo.working_dir)
41-
executor.submit(upload_repo, cloned_repo, parsed_url.repo)
42-
41+
executor.submit(clone_repo, parsed_url.name, parsed_url.url, repo.branch)
4342
return jsonify({"status_key": parsed_url.repo}), 202
4443

4544

46-
def clone_repo(parsed_url, repo):
45+
def clone_repo(repo_name, repo_url, branch):
4746
try:
48-
MessageHelper.publish(parsed_url.repo, json.dumps({"current_status": "Cloning repo " + repo.repo + ""}))
49-
logger.info("Cloning repo=" + repo.repo + " and branch=" + repo.branch)
47+
48+
MessageHelper.publish(repo_name + "-clone",
49+
json.dumps(dict(current_status="cloning", details="Cloning repo " + repo_name + "")))
50+
logger.info("Cloning repo=" + repo_url + " and branch=" + branch)
5051

51-
cloned_repo = GitHelper.clone(repo_url=repo.repo, branch=repo.branch,
52+
cloned_repo = GitHelper.clone(repo_url=repo_url, branch=branch,
5253
clone_dir=expanduser("~") + "/temp/" + FileHelper.get_random_name(10))
5354
except BaseException as e:
54-
logger.error("Cloning of repo=" + repo.repo + " with branch=" + repo.branch + " failed due to error=" + str(e))
55+
logger.error("Cloning of repo=" + repo_url + " with branch=" + branch + " failed due to error=" + str(e))
5556
# return jsonify({"error": "Error cloning " + repo.repo + " with branch due to " + str(e)}), 500
56-
raise ApiError(message="Error in cloning " + parsed_url.repo, status_code=503,
57-
details="Error cloning " + repo.repo + " with branch " + repo.branch + " due to " + str(e))
58-
return cloned_repo
57+
MessageHelper.publish(repo_name + "-clone",
58+
json.dumps(dict(current_status="error",
59+
details="Cloning repo " + repo_name + " with branch=" + branch +
60+
"failed due to error=" + str(e))))
61+
return
62+
63+
MessageHelper.publish(repo_name + "-clone",
64+
json.dumps(dict(current_status="success",
65+
details="Cloning of repo=" + repo_url + " with branch=" +
66+
branch + " is successful")))
67+
upload_repo(cloned_repo, repo_name)
5968

6069

6170
def create_repo_object(data):
@@ -90,27 +99,38 @@ def info():
9099
@swag_from(get_docs_path_for('status_api.yaml'))
91100
@api_blueprint.route('/status', methods=['POST'])
92101
def status():
93-
status_data = get_upload_data()
94-
95-
status_message = Status(current_status=status_data['current_status'],
102+
status_data, clone_data = get_upload_data()
103+
current_status = get_current_status(clone_data, status_data)
104+
logger.debug("current status="+current_status)
105+
status_message = Status(clone_status=clone_data.get('current_status', ""),
106+
current_status=current_status,
96107
file_type=status_data.get('type_uploading', ""),
97108
last_uploaded_file=status_data.get('last_file_uploaded'),
98109
total_files_uploaded=status_data.get('total_files_uploaded'))
99110
return jsonify(
100111
dict(status=status_message.current_status,
112+
clone_status=status_message.clone_status,
101113
currently_uploading=status_message.processing_file_type,
102114
last_uploaded_file=status_message.last_uploaded_file,
103115
total_files_uploaded=status_message.total_files_uploaded)), 200
104116

105117

118+
def get_current_status(clone_data, status_data):
119+
logger.debug('upload status data='+json.dumps(status_data))
120+
logger.debug('clone status data='+json.dumps(clone_data))
121+
return status_data.get('current_status') if status_data.get('current_status', "") != "" else clone_data[
122+
'current_status']
123+
124+
106125
def get_upload_data():
107126
request_data = get_request_data()
108-
if not 'status_key' in request_data.keys():
127+
if 'status_key' not in request_data.keys():
109128
raise ApiError(message="Incorrect status key", status_code=400,
110129
details="The request did not contain key named status_key ")
111130

112131
status_data = json.loads(broker.get(request_data.get('status_key')))
113-
return status_data
132+
clone_data = json.loads(broker.get(request_data.get('status_key') + "-clone"))
133+
return status_data, clone_data
114134

115135

116136
def get_request_data():
@@ -122,10 +142,15 @@ def get_request_data():
122142
return request_data
123143

124144

145+
def reset_if_exists(repo_name):
146+
MessageHelper.publish(repo_name +"-clone", json.dumps(dict(current_status='')))
147+
MessageHelper.publish(repo_name, json.dumps(dict(current_status='')))
148+
149+
125150
@swag_from(get_docs_path_for('progress_update_api_all.yaml'))
126151
@api_blueprint.route('/progress-update/all', methods=['POST'])
127152
def progress_update():
128-
status_data = get_upload_data()
153+
status_data, clone_data = get_upload_data()
129154
# status_progress: UploadStatus = upload_status_from_dict(status_data)
130155
if status_data["server"] and status_data["server"]["response_code"] and not 200 <= int(status_data["server"][
131156
"response_code"]) <= 400:
@@ -161,7 +186,7 @@ def progress_update():
161186
@swag_from(get_docs_path_for('progress_update_api_modules.yaml'))
162187
@api_blueprint.route('/progress-update/modules', methods=['POST'])
163188
def progress_update_modules():
164-
status_data = get_upload_data()
189+
status_data, clone_data = get_upload_data()
165190
if status_data["server"] and status_data["server"]["response_code"] and not 200 <= int(status_data["server"][
166191
"response_code"]) <= 400:
167192
return jsonify(
@@ -186,7 +211,7 @@ def progress_update_modules():
186211
@swag_from(get_docs_path_for('progress_update_api_assemblies.yaml'))
187212
@api_blueprint.route('/progress-update/assemblies', methods=['POST'])
188213
def progress_update_assemblies():
189-
status_data = get_upload_data()
214+
status_data, clone_data = get_upload_data()
190215

191216
if status_data["server"] and status_data["server"]["response_code"] and not 200 <= int(status_data["server"][
192217
"response_code"]) <= 400:
@@ -214,7 +239,7 @@ def progress_update_assemblies():
214239
@swag_from(get_docs_path_for('progress_update_api_resources.yaml'))
215240
@api_blueprint.route('/progress-update/resources', methods=['POST'])
216241
def progress_update_resources():
217-
status_data = get_upload_data()
242+
status_data, clone_data = get_upload_data()
218243
if status_data["server"] and status_data["server"]["response_code"] and not 200 <= int(status_data["server"][
219244
"response_code"]) <= 400:
220245
return jsonify(

git2pantheon/models/response_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33

44
class Status():
5-
def __init__(self, current_status, file_type, last_uploaded_file, total_files_uploaded):
5+
def __init__(self, clone_status, current_status, file_type, last_uploaded_file, total_files_uploaded):
6+
self.clone_status = clone_status
67
self.current_status = current_status
78
self.processing_file_type = file_type
89
self.last_uploaded_file = last_uploaded_file

0 commit comments

Comments
 (0)