lib/vagrant-vaimo-unison/command.rb in vagrant-vaimo-unison-1.0.0 vs lib/vagrant-vaimo-unison/command.rb in vagrant-vaimo-unison-1.1.0
- old
+ new
@@ -7,29 +7,61 @@
require_relative 'shell_command'
require_relative 'unison_sync'
module VagrantPlugins
module Unison
- class CommandSync < Vagrant.plugin("2", :command)
+ class CommandSync < Vagrant.plugin('2', :command)
include UnisonSync
attr_accessor :bg_thread
def self.synopsis
'sync the unison shared folder forever, by polling for changes'
end
def execute
+ sync_once
+ sync_polling
+ end
+
+ def sync_once
status = nil
with_target_vms do |machine|
+ execute_sync_command(machine) do |command|
+ command.batch = true
+ command.terse = true
+ command = command.to_s
+
+ @env.ui.info 'Started main sync process'
+ if options[:verbose]
+ @env.ui.info ' Verbose mode: enabled'
+ @env.ui.info " Command: #{command}"
+ end
+
+ status = system(command)
+ @env.ui.info 'unison exited with a error' unless status
+ end
+ end
+ return 0 if status
+
+ 1
+ end
+
+ def sync_polling
+ status = nil
+ with_target_vms do |machine|
@bg_thread = watch_vm_for_memory_leak(machine)
execute_sync_command(machine) do |command|
command.repeat = true
- command.batch = true
command.terse = true
command = command.to_s
- @env.ui.info "Running #{command}"
+ @env.ui.info 'Polling process started'
+ if options[:verbose]
+ @env.ui.info ' Verbose mode: enabled'
+ @env.ui.info " Memory limit: #{machine.config.unison.mem_cap_mb}MB"
+ @env.ui.info " Command: #{command}" if options[:verbose]
+ end
# Re-run on a crash.
# On a sigint, wait 2 seconds before respawning command.
# If INT comes in again while waiting, program exits.
# If INT comes in after we've respanwned,
@@ -52,34 +84,32 @@
@env.ui.info '** Sync crashed. Respawning. Hit Ctrl + C twice to kill. **'
end
end
end
end
- if status
- return 0
- end
+ return 0 if status
1
end
def watch_vm_for_memory_leak(machine)
ssh_command = SshCommand.new(machine)
Thread.new(ssh_command.ssh, machine.config.unison.mem_cap_mb) do |ssh_command_text, mem_cap_mb|
while true
sleep 15
- total_mem = `#{ssh_command_text} 'free -m | egrep "^Mem:" | awk "{print \\$2}"' 2>/dev/null`
- _unison_proc_returnval = (
- `#{ssh_command_text} 'ps aux | grep "[u]nison -server" | awk "{print \\$2, \\$4}"' 2>/dev/null`
- )
- if _unison_proc_returnval == ''
+ total_mem = `#{ssh_command_text} 'free -m | egrep "^Mem:" | awk "{print \\$2}"' 2>/dev/null`
+ unison_proc_returnval = `#{ssh_command_text} 'ps aux | grep "[u]nison -server" | awk "{print \\$2, \\$4}"' 2>/dev/null`
+
+ if unison_proc_returnval == ''
puts 'Unison not running in VM'
next
end
- pid, mem_pct_unison = _unison_proc_returnval.strip.split(' ')
- mem_unison = (total_mem.to_f * mem_pct_unison.to_f / 100).round(1)
- # Debugging: uncomment to log every loop tick
- # puts "Unison running as #{pid} using #{mem_unison} mb"
+
+ pid, mem_pct_unison = unison_proc_returnval.strip.split(' ')
+ mem_unison = (total_mem.to_f * mem_pct_unison.to_f / 100)
+ mem_unison = mem_unison.round(1)
+
if mem_unison > mem_cap_mb
puts "Unison using #{mem_unison}MB memory is over limit of #{mem_cap_mb}MB, restarting"
`#{ssh_command_text} kill -HUP #{pid} 2>/dev/null`
end
end
@@ -89,23 +119,23 @@
class CommandOnce < Vagrant.plugin('2', :command)
include UnisonSync
def self.synopsis
- 'sync the unison shared folder once'
+ 'sync the unison shared folder once (deprecated)'
end
def execute
- @env.ui.info 'The command is deprecated, use vagrant unison-sync'
+ @env.ui.info 'The command is deprecated, use vagrant unison-sync --no-polling'
end
end
- class CommandPolling < Vagrant.plugin("2", :command)
+ class CommandPolling < Vagrant.plugin('2', :command)
include UnisonSync
attr_accessor :bg_thread
def self.synopsis
- 'sync the unison shared folder forever, by polling for changes'
+ 'sync the unison shared folder forever, by polling for changes (deprecated)'
end
def execute
@env.ui.info 'The command is deprecated, use vagrant unison-sync'
end