-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.rb
61 lines (53 loc) · 1.7 KB
/
updater.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'directory_watcher'
require 'selenium/server'
# dodgy - loading webdriver to get Dir.mktmpdir method in 1.8.7
require 'selenium-webdriver'
module AutoDriver
class Updater
def initialize(options)
@latest_binary = options.delete('latest-binary')
@arguments = options.delete('arguments')
@executable_name = @latest_binary.split('/').last
end
def update
FileUtils.mkdir @execute_in_folder if not File.exists? @execute_in_folder
FileUtils.cp_r @latest_binary, File.join(@execute_in_folder, @executable_name)
end
def start
@execute_in_folder = Dir.mktmpdir
update
@server = Selenium::Server.new(File.join(@execute_in_folder, @executable_name), :background => true)
@server << @arguments
@monitor_thread = Thread.new do
folder_parts = @latest_binary.split('/')
folder_parts.pop
updater = self
server = @server
dw = DirectoryWatcher.new folder_parts.join('/')
dw.interval = 1.0
dw.glob = @executable_name
dw.reset true
dw.add_observer {|*args| args.each {|event|
if event.type == :added or event.type == :modified
puts "Webdriver going down for update"
server.stop
puts "Updating to latest version"
updater.update
puts "Webdriver restarting after update"
server.start
end
}
}
dw.start
end
@server.start
puts "AutoDriver started"
end
def stop
@server.stop
@monitor_thread.kill if not @monitor_thread.nil?
FileUtils.rm_rf @execute_in_folder if not @execute_in_folder.nil?
puts "AutoDriver stopped"
end
end
end