views:

281

answers:

2

Hi Guys,

I wrote a few scripts to monitor an uploads directory for changes, then capture the file uploaded/changed and push it to cloud files using a ruby script. This all works well 95% of the time, the only exception is that occasionally, ruby fails with a 'file does not exist' exception.

I am assuming that the ruby 'push' script is being called before the file is 100% in its new location, so the script is being called a little prematurely.

I tried adding a little function to my script to check if the file exists, if it doesn't, sleep 5 then try again, but this seems to snowball and eventually dies. I then just added a sleep 2 to all calls, but it hasn't helped as I now get the 'file does not exist' error again.

#!/bin/sh

function checkExists {
    if [ ! -e "$1" ]
    then
            sleep 5
            checkExists $1
    fi
}

inotifywait -mr --timefmt '%d/%m/%y-%H:%M' --format '%T %w %f' -e modify,moved_to,create,delete /home/skylines/html/forums/uploads | while read date dir file; do
    cloudpath=${dir:20}${file}
    localpath=${dir}${file}
    #checkExists $localpath
    sleep 2
    ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath
    echo "${date} ruby /home/cbiggins/bin/pushToCloud.rb skylinesaustralia.com $cloudpath $localpath" >> /var/log/pushToCloud.log
done

I am looking for any suggestions to help me make this 100% stable (eventually, I'll serve the uploaded files from Cloud FIles, so I need to make sure its perfect)

Thanks in advance!

EDIT - solved!! - I am watching the 'delete' event... So, this script is firing when local files have been deleted, which dont exist. Derr. :)

+1  A: 

Have you made sure it's not something silly as a space in the uploaded file? Try adding quotes around the parameters passed to your script!

ba
Good call, but the file names are pre processed by the upload script, so I know the input is good.
Christian
+1  A: 

Your function calls itself recursively with no exit. Try changing it to use a while loop instead.

Dennis Williamson
Great idea, will give it a go.
Christian