开发者

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

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 skylinesaustr开发者_StackOverflow中文版alia.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. :)


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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜