Skip to content

Commit

Permalink
Added delayed uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
NoxArt committed Aug 21, 2012
1 parent 11104b2 commit caf9179
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
47 changes: 27 additions & 20 deletions FTPSync.py
Expand Up @@ -106,7 +106,7 @@
usingConnections = []
# individual folder config cache, file => config path
configs = {}
# scheduled delayed uploads, file_path => callback()
# scheduled delayed uploads, file_path => action id
scheduledUploads = {}


Expand Down Expand Up @@ -658,7 +658,7 @@ def performSync(file_path, config_file_path, onSave, disregardIgnore=False, prog

index = -1
stored = []
failed = False
delayed = False
index = -1

for name in config['connections']:
Expand All @@ -680,34 +680,41 @@ def performSync(file_path, config_file_path, onSave, disregardIgnore=False, prog
break

try:
uploaded = connections[index].put(file_path)
# action
connection = connections[index]
id = os.urandom(32)
scheduledUploads[file_path] = id

if type(uploaded) is str or type(uploaded) is unicode:
stored.append(uploaded)
printMessage("uploaded " + basename, name)
def action():
try:
if scheduledUploads[file_path] != id:
return

else:
failed = type(uploaded)
connection.put(file_path)
stored.append(name)
printMessage("uploaded " + basename, name)

except Exception, e:
failed = e
scheduledUploads.pop(file_path)
except Exception, e:
printMessage("performSync exception: " + str(e))
printMessage("upload failed: {" + basename + "} <Exception: " + str(e) + ">", name, False, True)

printMessage("performSync exception: " + str(e))

if failed:
message = "upload failed: {" + basename + "}"

if type(failed) is Exception:
message += "<Exception: " + str(failed) + ">"
# delayed
if onSave is True and config['connections'][name]['upload_delay'] > 0:
delayed = True
printMessage("delaying upload of " + basename + " by " + str(config['connections'][name]['upload_delay']) + " seconds", name, onlyVerbose=True)
sublime.set_timeout(action, config['connections'][name]['upload_delay'] * 1000)
else:
message += "<Error: " + str(failed) + ">"

printMessage(message, name, False, True)
action()
except Exception, e:
printMessage("performSync exception: " + str(e))
printMessage("upload failed: {" + basename + "} <Exception: " + str(e) + ">", name, False, True)

if len(stored) > 0:
dumpMessage(getProgressMessage(stored, progress, "uploaded", basename))

if config_hash in usingConnections:
if delayed is False and config_hash in usingConnections:
usingConnections.remove(config_hash)


Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ Format:

upload_on_save: true, // whether upload on save or manually
download_on_open: false, // overwrite protection when remote file is newer
upload_delay: {int=0}, // delays upload triggered by upload_on_save
upload_delay: {int=0}, // delays [seconds] upload triggered by upload_on_save

port: {int=21}, // remote port, pretty much always 21, unless SFTP
tls: {bool=false}, // set true to use secured transfer, recommended! (server needs to support)
Expand Down
2 changes: 1 addition & 1 deletion ftpsync.default-settings
Expand Up @@ -61,7 +61,7 @@
// if set to true it will download the file from server on opening, but only if it's newer than the current file
//"download_on_open": false,

// delay before changed file is uploaded !when using upload_on_save!
// delay [seconds] before changed file is uploaded !when using upload_on_save!
// can be used for increase of performance or to allow build scripts to finish
//"upload_delay": 0,

Expand Down

0 comments on commit caf9179

Please sign in to comment.