Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created January 21, 2011 13:43
Show Gist options
  • Save dongyuwei/789690 to your computer and use it in GitHub Desktop.
Save dongyuwei/789690 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'net/ssh'
require 'net/scp'
require 'ruby-growl'
#http://codeforpeople.rubyforge.org/directory_watcher/classes/DirectoryWatcher.html
module Handler
def file_modified
puts "#{path} modified"
host = "10.210.74.63"
username = "my name"
password = "my password"
source = "/Users/dyw/test.js"
target = "/opt/"
recursive = false
self.upload(host, username,password,source, target,recursive)
end
def file_moved
puts "#{path} moved"
end
def file_deleted
puts "#{path} deleted"
end
def unbind
puts "#{path} monitoring ceased"
end
def upload(host, username,password,source, target,recursive)
Net::SCP.start(host, username, :password => password) do |scp|
scp.upload!(source, target,:recursive => recursive)do |ch, name, sent, total|
#progress reports
puts "#{name}: #{sent}/#{total}"
end
g = Growl.new "127.0.0.1", "ruby-growl", ["ruby-growl Notification"]
g.notify "ruby-growl Notification", "File monitor", "#{path} modified,synced to server !"
end
end
end
EM.kqueue = true if EM.kqueue? # file watching requires kqueue on OSX
EM.run {
EM.watch_file("/Users/dyw/test.js", Handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment