Class: VagrantPlugins::Unison::CommandPolling

Inherits:
Object
  • Object
show all
Includes:
UnisonSync
Defined in:
lib/vagrant-unison2/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UnisonSync

#check_conflicting_options!, #execute_sync_command, #options, #options_parser, #parse_options!

Instance Attribute Details

#bg_threadObject

Returns the value of attribute bg_thread.



44
45
46
# File 'lib/vagrant-unison2/command.rb', line 44

def bg_thread
  @bg_thread
end

Class Method Details

.synopsisObject



46
47
48
# File 'lib/vagrant-unison2/command.rb', line 46

def self.synopsis
  "sync the unison shared folder forever, by polling for changes"
end

Instance Method Details

#executeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-unison2/command.rb', line 50

def execute
  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.terse = true
      command = command.to_s

      @env.ui.info "Running #{command}"

      # 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,
      # will bring us back to this trap handler.
      exit_on_next_sigint = false
      while true
        begin
          sleep 2 if exit_on_next_sigint
          exit_on_next_sigint = false
          status = system(command)
          @env.ui.info "**** unison exited. success: #{status} ****"
        rescue Interrupt
          if exit_on_next_sigint
            Thread.kill(@bg_thread) if @bg_thread
            exit 1
          end
          @env.ui.info '** Hit Ctrl + C again to kill. **'
          exit_on_next_sigint = true
        rescue Exception
            @env.ui.info '** Sync crashed. Respawning. Hit Ctrl + C twice to kill. **'
        end
      end
    end
  end
  if status
    return 0
  else
    return 1
  end
end

#watch_vm_for_memory_leak(machine) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-unison2/command.rb', line 93

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 == ''
        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"
      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
  end
end