Skip to content

Commit

Permalink
Merge pull request #431 from tiagopog/print-current-sync-time
Browse files Browse the repository at this point in the history
Print the current sync time along with the watcher's output
  • Loading branch information
EugenMayer committed Jul 6, 2017
2 parents 9fca971 + a1df502 commit e7d8e56
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/workspace.xml
*.sw*
# Created by .ignore support plugin (hsz.mobi)
### Ruby template
*.gem
Expand Down
46 changes: 11 additions & 35 deletions lib/docker-sync/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,31 @@
require 'thor/shell'

module Execution

Thread.abort_on_exception = true

def threadexec(command, prefix = nil, color = nil)

if prefix.nil?
# TODO: probably pick the command name without args
prefix = 'unknown'
end

if color.nil?
color = :cyan
end

Thread.new {
def thread_exec(command, prefix = 'unknown', color = :cyan)
Thread.new do
Open3.popen3(command) do |_, stdout, stderr, _|

# noinspection RubyAssignmentExpressionInConditionalInspection
while line_out = stdout.gets
say_status prefix, line_out, color
say_status with_time(prefix), line_out, color
end

# noinspection RubyAssignmentExpressionInConditionalInspection
while line_err = stderr.gets
say_status prefix, line_err, :red
say_status with_time(prefix), line_err, :red
end

end
}

end
end

# unison doesn't work when ran in a new thread
# this functions creates a full new process instead
def forkexec(command, prefix = nil, color = nil)

if prefix.nil?
# TODO: probably pick the command name without args
prefix = 'unknown'
end

if color.nil?
color = :cyan
end

Process.fork {
`#{command}` || raise(command + ' failed')
}

def fork_exec(command, _prefix = 'unknown', _color = :cyan)
Process.fork { `#{command}` || raise(command + ' failed') }
end

end
def with_time(prefix)
"[#{Time.now}] #{prefix}"
end
end
2 changes: 1 addition & 1 deletion lib/docker-sync/sync_strategy/unison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def watch
cmd = cmd + 'unison ' + args.join(' ')

say_status 'command', cmd, :white if @options['verbose']
forkexec(cmd, "Sync #{@sync_name}", :blue)
fork_exec(cmd, "Sync #{@sync_name}", :blue)
end

def sync
Expand Down
2 changes: 1 addition & 1 deletion lib/docker-sync/watch_strategy/fswatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def watch
say_status 'command', cmd, :white if @options['verbose']

# run a thread here, since it is blocking
@watch_thread = threadexec(cmd, "Sync #{@sync_name}", :blue)
@watch_thread = thread_exec(cmd, "Sync #{@sync_name}", :blue)
end

def watch_options
Expand Down
2 changes: 1 addition & 1 deletion lib/docker-sync/watch_strategy/remotelogs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(sync_name, options)
def run
say_status 'success', "Showing unison logs from your sync container: #{@unison.get_container_name}", :green
cmd = "docker exec #{@unison.get_container_name} tail -F /tmp/unison.log"
@watch_thread = threadexec(cmd, 'Sync Log:')
@watch_thread = thread_exec(cmd, 'Sync Log:')
end

def stop
Expand Down

0 comments on commit e7d8e56

Please sign in to comment.