Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print the current sync time along with the watcher's output #431

Merged
merged 1 commit into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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