Using inotify-tools and ruby to push uploads to Cloud Files

Posted by Christian on Stack Overflow See other posts from Stack Overflow or by Christian
Published on 2010-03-30T08:17:14Z Indexed on 2010/03/30 8:23 UTC
Read the original article Hit count: 500

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about bash

Related posts about ruby