loop_upgrade
[scpubgit/Tak.git] / lib / Tak.pm
1 package Tak;
2
3 use Tak::Loop;
4 use strictures 1;
5
6 our $VERSION = '0.001001'; # 0.1.1
7
8 our $loop;
9
10 sub loop { $loop ||= Tak::Loop->new }
11
12 sub loop_upgrade {
13   require IO::Async::Loop;
14   my $new_loop = IO::Async::Loop->new;
15   $loop->pass_watches_to($new_loop) if $loop;
16   $loop = $new_loop;
17 }
18
19 sub loop_until {
20   my ($class, $done) = @_;
21   return if $done;
22   $class->loop->loop_once until $_[1];
23 }
24
25 sub await_all {
26   my ($class, @requests) = @_;
27   @requests = grep !$_->is_done, @requests;
28   return unless @requests;
29   my %req = map +("$_" => "$_"), @requests;
30   my $done;
31   my %on_r = map {
32     my $orig = $_->{on_result};
33     my $tag = $req{$_};
34     ($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; })
35   } @requests;
36   my $call = sub { $class->loop_until($done) };
37   foreach (@requests) {
38     my $req = $_;
39     my $inner = $call;
40     $call = sub { local $req->{on_result} = $on_r{$req}; $inner->() };
41   }
42   $call->();
43   return;
44 }
45
46 "for lexie";
47
48 =head1 NAME
49
50 Tak - Multi host remote control over ssh
51
52 =head1 SYNOPSIS
53
54   bin/tak -h user1@host1 -h user2@host2 exec cat /etc/hostname
55
56 or
57
58   in Takfile:
59
60   package Tak::MyScript;
61   
62   use Tak::Takfile;
63   use Tak::ObjectClient;
64   
65   sub each_get_homedir {
66     my ($self, $remote) = @_;
67     my $oc = Tak::ObjectClient->new(remote => $remote);
68     my $home = $oc->new_object('Path::Class::Dir')->absolute->stringify;
69     $self->stdout->print(
70       $remote->host.': '.$home."\n"
71     );
72   }
73   
74   1;
75
76 then
77
78   tak -h something get-homedir
79
80 =head1 WHERE'S THE REST?
81
82 A drink leaked in my bag on the way back from LPW. You'll get more once I
83 get my laptop's drive into an enclosure and decant the slides.
84
85 =head1 AUTHOR
86
87 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
88
89 =head1 CONTRIBUTORS
90
91 None required yet. Maybe this module is perfect (hahahahaha ...).
92
93 =head1 COPYRIGHT
94
95 Copyright (c) 2011 the strictures L</AUTHOR> and L</CONTRIBUTORS>
96 as listed above.
97
98 =head1 LICENSE
99
100 This library is free software and may be distributed under the same terms
101 as perl itself.
102
103 =cut