Module: RemoteSh::FileSyncer

Defined in:
lib/remote_sh/file_syncer.rb

Class Method Summary collapse

Class Method Details

.do_after_timeout(timeout, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/remote_sh/file_syncer.rb', line 50

def do_after_timeout(timeout, &block)
  @thread&.kill

  @thread = Thread.new do
    sleep(timeout)
    block.()
  end
end

.start(host, host_path, ignored_files) ⇒ Object



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
# File 'lib/remote_sh/file_syncer.rb', line 12

def start(host, host_path, ignored_files)
  local_path = WorkspaceConfiguration::WOKRING_DIR

  RsyncHelper.up(local_path, host_path, host, ignored_files)

  mutex = Mutex.new

  Thread.new do
    Filewatcher.new("#{local_path}/**/{.[^\.]*,*}", interval: 1).watch do |_changes|
      if @sync_side == :client || @sync_side.nil?
        mutex.synchronize do
          @sync_side = :clientA
          begin
            RsyncHelper.up(local_path, host_path, host, ignored_files)
            @up_requested = false
          rescue
            @up_requested = true
          end
          do_after_timeout(1) do
            begin
              RsyncHelper.up(local_path, host_path, host, ignored_files) if @up_requested
            rescue
            end

            mutex.synchronize do
              @sync_side = nil
            end
          end
        end
      end
    end
  end

  Thread.new do
    start_server(mutex, local_path, host_path, host, ignored_files)
  end
end

.start_server(mutex, local_path, host_path, host, ignored_files) ⇒ Object



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/remote_sh/file_syncer.rb', line 59

def start_server(mutex, local_path, host_path, host, ignored_files)
  pid_filename = "#{PID_FOLDER}/#{local_path.gsub("/", "__")}_syncing"
  socket_file = "#{pid_filename}_socket"

  path = socket_file.split("/")[...-1].join("/")

  `ssh #{host} "rm -f #{socket_file}"`
  `ssh #{host} "mkdir -p #{path}"`

  Thread.new do
    ruby_code = "      require \"net_http_unix\"\n      require \"filewatcher\"\n\n      Thread.new do\n        Filewatcher.new(\"\#{host_path}/**/{.[^\\.]*,*}\", interval: 1).watch do |_changes|\n          request = Net::HTTP::Get.new(\"/request_sync\")\n          NetX::HTTPUnix.new(\"unix://\" + \"\#{socket_file}\").request(request)\n        end\n      end\n\n      loop do\n        request = Net::HTTP::Get.new(\"/ping\")\n        NetX::HTTPUnix.new(\"unix://\" + \"\#{socket_file}\").request(request)\n        sleep(3)\n      end\n    RUBY\n\n    Open3.capture3(\"ssh -q \#{host} 'ruby -e \\\"\#{ruby_code}\\\"'\")\n  end\n\n  UNIXServer.open(socket_file) do |ssocket|\n    SshHelper.forward_local_socket(host, socket_file)\n\n    server = WEBrick::HTTPServer.new(\n      DoNotListen: true,\n      Logger: WEBrick::Log.new(\"/dev/null\"),\n      AccessLog: [],\n    )\n    server.listeners << ssocket\n\n    server.mount_proc \"/request_sync\" do |_req, res|\n      if @sync_side == :server || @sync_side.nil?\n        mutex.synchronize do\n          @sync_side = :server\n          begin\n            RsyncHelper.down(local_path, host_path, host, ignored_files)\n            @down_requested = false\n          rescue\n            @down_requested = true\n          end\n          do_after_timeout(1) do\n            begin\n              RsyncHelper.down(local_path, host_path, host, ignored_files) if @down_requested\n            rescue\n            end\n            mutex.synchronize { @sync_side = nil }\n          end\n        end\n      end\n\n      res.body = \"request_sync\"\n    end\n\n    server.mount_proc \"/ping\" do |_req, res|\n      res.body = \"ping\"\n    end\n\n    trap(\"INT\") { server.shutdown }\n\n    server.start\n  end\nensure\n  SshHelper.close_local_socket(host, socket_file)\n  File.unlink(socket_file)\nend\n".gsub('"', '\"')