Commit 01f9125a authored by Daniel Schreiber's avatar Daniel Schreiber

fix issue with remote sync failure

when remote host responds with an error code, sync will be tried later again.
Sometimes remotes still store the databut a duplicate entry is prevented by the
database. This fix handles this case by requesting the resource from remote. If
it exists, everything is fine
parent 0fe59b5b
Pipeline #27445 failed with stage
in 18 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json
import logging
import requests
......@@ -38,6 +39,24 @@ def sync_resource(sync_user, resource_json):
r = requests.post(url, data=resource_json, headers=headers, timeout=5)
if r.status_code == 400:
# status code could mean, resource exists on remote side
# try to fetch it an compare.
uuid = json.loads(resource_json)['uuid']
detail_url = sync_user.remote_reverse("resource_detail", args=(uuid,))
r2 = requests.get(detail_url, headers=headers)
if r2.status_code == 200:
logger.info("Resource {} already exists on remote side".format(uuid))
return
else:
msg = "Sync failed: Host responded with {code}, this could mean the host already knows the resource, but fetching of resource failed: {code_fetch}. Message: {msg}".format(
code=r.status_code,
code_fetch=r2.status_code,
msg=r2.content,
)
logger.info(msg)
raise SyncException(msg)
if not r.status_code == 201:
logger.debug("Unexpected response: {}".format(r.content))
raise SyncException("Sync failed: Host responded with {code}".format(code=r.status_code))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment