syncthing sync completed or not #1
No reviewers
Labels
No Label
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: Sysdeploy/WaitForSyncthing#1
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "shailaja/WaitForSyncthing:sync"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
As we discussed, this needs to be refactored so that it accesses the syncthing API directly. I recommend the following code flow:
This way there shouldn't be a gap that could cause problems, and it also shouldn't be necessary in each loop to wait for an event AND separately query the folder status. Also an events API callis automatically long poll with a timeout of 60 seconds, so that simplifies the code, the long poll is implemented on the server side.
We also need to handle a situation where the folder doesn't exist at all and therefore doesn't have a status. That should also be interpreted as non-idle, rather than throwing an exception.
Folder name can be passed as an environment variable, and the API and authentication can be passed as a volume mount from the host. There are probably other options but this one looks the simplest, and you can just copy parts of the code from https://git.bitmessage.org/Sysdeploy/ansible-modules-syncthing.
9009f205a8
toda5c266da9
Overall it appears to be correct, but it could be nicer.
@ -0,0 +7,4 @@
""" Retrieve the API key from the local Syncthing configuration file. """
try:
with open(config_file, 'r') as file:
config_data = json.load(file)
Check here:
The config file is XML, not JSON.
@ -0,0 +19,4 @@
headers = {'X-Api-Key': api_key}
if method == 'GET':
response = requests.get(url, headers=headers)
elif method == 'POST':
I think we only need
GET
.@ -0,0 +52,4 @@
current_events = syncthing_request(f"{api_url}/rest/events?limit=1", api_key)
last_event_id = current_events[0]['id'] if current_events else 0
while True:
It unnecessarily queries and sleeps. I'd prefer something like this:
I.e. it's not necessary to query the folder state in a loop, as we have the value in the event already. Long poll has a timeout so there's no need for a separate sleep.
We could also make the
api_url
andapi_key
variables global, or a class variable, or a module variable, or a class property, because it doesn't change, it's only set once, when the app starts.@ -0,0 +35,4 @@
def long_poll_events(api_url, last_event_id, api_key):
""" Long poll the events endpoint filtering by the last known event ID. """
events_url = f"{api_url}/rest/events?since={last_event_id}&timeout=60"
One more thing, we can filter the events and then the code looks nicer:
events_url = f"{api_url}/rest/events?since={last_event_id}&events=StateChanged&timeout=60"
and then you don't need to check inside
main
forevent['type']
anymore.0c5683c38b
to037431821b
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Gitea.