/fswatch-rsync.p6 Secret
Created
June 29, 2016 04:15
Star
You must be signed in to star a gist
why does this consume 1 CPU at 99% and 5GB RES mem?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl6 | |
| use lib "{$?FILE.IO.dirname}/../lib".IO.resolve.path; | |
| use Config; | |
| sub MAIN (Str :$want = '', Bool :$dry-run = False, Bool :$run-once = False) { | |
| chdir $LOCAL_SRC_DIR or die "Error directory not found $LOCAL_SRC_DIR"; | |
| my $fswatch-channel = Channel.new; | |
| my $rsync-channel = Channel.new; | |
| my $loop-promise = Promise.new; | |
| my @app-dirs = find-app-dirs($want); | |
| my $rsync_count = @app-dirs.elems - 1; | |
| Promise.in(3).then({ | |
| for @app-dirs -> [$API, $basedir, $dir] { | |
| $rsync-channel.send([$API, $basedir, $dir]); | |
| } | |
| }); | |
| react { | |
| whenever $fswatch-channel -> [$API, $basedir, $dir] { | |
| my $fswatch = Proc::Async.new("fswatch","-1","--exclude=.git","-r","-l 1",$dir,:cwd($dir)); | |
| $fswatch.stdout.act: -> $line { | |
| print "fswatch changed {$line}".dark-pink; | |
| $rsync-channel.send([$API, $basedir, $dir]); | |
| }; | |
| $fswatch.start; | |
| } | |
| whenever $rsync-channel -> [$API, $basedir, $dir] { | |
| my $exclude = "{$HOME}/.rsync-exclude-{$API}".IO.f ?? "{$HOME}/.rsync-exclude-{$API}" !! "{$HOME}/.rsync-exclude"; | |
| my $from = "{$dir}/"; | |
| my $to = "{$REMOTE_HOST}:{$REMOTE_SRC_DIR}/{$basedir}/{$REMOTE_SUFFIX}"; | |
| say "rsync {($dry-run ?? 'dry-run' !! '')}".mid-blue; | |
| {($dry-run ?? "dry-run" !! "")} | |
| print qq:to/TXT/.mid-pink; | |
| exclude {$exclude} | |
| from {$dir} | |
| to {$to} | |
| TXT | |
| my $rsync = Proc::Async.new("rsync", "-ai"~($dry-run ?? "n" !! ""), "-e'ssh'", "--delete", "--exclude-from=$exclude", $from, $to, :cwd($dir), :out, :err); | |
| $rsync.stderr.act: -> $line { print $line.dark-blue }; | |
| $rsync.stdout.act: -> $line { print $line.mid-blue }; | |
| await $rsync.start; | |
| if $run-once { | |
| if $rsync_count-- == 0 { | |
| $loop-promise.keep("run-once done"); | |
| } | |
| } else { | |
| $fswatch-channel.send([$API, $basedir, $dir]); | |
| } | |
| } | |
| } | |
| loop { | |
| if $loop-promise.status !~~ Planned { | |
| last; | |
| } | |
| } | |
| say $loop-promise.result.dark-red; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment