File Coverage

blib/lib/App/bif/pull/hub.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::bif::pull::hub;
2 1     1   3881 use strict;
  1         2  
  1         27  
3 1     1   4 use warnings;
  1         2  
  1         31  
4 1     1   1156 use AnyEvent;
  1         5110  
  1         41  
5 1     1   687 use Bif::Sync::Client;
  0            
  0            
6             use Bif::Mo;
7             use Coro;
8             use DBIx::ThinSQL qw/qv/;
9             use Log::Any '$log';
10             use Path::Tiny;
11              
12             our $VERSION = '0.1.5_5';
13             extends 'App::bif';
14              
15             sub run {
16             my $self = shift;
17             my $opts = $self->opts;
18              
19             # Do this early so that we complain about missing repo before we
20             # complain about a missing hub
21             my $dbw = $self->dbw; # Consider upping PRAGMA cache_size?
22             #Or handle that in Bif::Role::Sync?
23              
24             if ( $opts->{location} =~ m!^(ssh://)?(.*)@(.*)! ) {
25             $opts->{name} = $2;
26             }
27             elsif ( -d $opts->{location} ) {
28             $opts->{location} = path( $opts->{location} )->realpath;
29             $opts->{name} = $opts->{location}->basename;
30             }
31             else {
32             return $self->err( 'HubNotFound', 'hub not found: %s',
33             $opts->{location} );
34             }
35              
36             $log->debug("pull hub: $opts->{location}");
37             if ( my $hub = $self->dbw->get_hub( $self->uuid2id( $opts->{location} ) ) )
38             {
39             warn "warning: hub already exists: $hub->{name}\n";
40             }
41              
42             $|++; # no buffering
43             my $error;
44             my $cv = AE::cv;
45              
46             my $client = Bif::Sync::Client->new(
47             name => $opts->{name},
48             db => $dbw,
49             location => $opts->{location},
50             debug => $opts->{debug},
51             debug_bifsync => $opts->{debug_bifsync},
52             on_update => sub {
53             my $client = shift;
54             $self->lprint( $client->name . ': ' . $_[0] );
55             },
56             on_error => sub {
57             $error = shift;
58             $cv->send;
59             },
60             );
61              
62             my $fh = select;
63             my $coro = async {
64             select $fh;
65              
66             eval {
67             $dbw->txn(
68             sub {
69             my $start = time;
70             $self->stop_work(
71             stop => $start,
72             save => 1,
73             );
74              
75             my ( $status, $ref ) = $client->pull_hub;
76              
77             print "\n";
78              
79             $client->disconnect;
80              
81             if ( $status ne 'RepoImported' ) {
82             $error = $status;
83             $dbw->rollback;
84             return $status;
85             }
86              
87             $self->start_work(
88             node_id => $ref->[0],
89             start => $start,
90             start_comment => "pull hub",
91             billable => 1,
92             );
93              
94             $self->stop_work(
95             stop => time,
96             restore => 1,
97             );
98              
99             $dbw->xdo(
100             insert_or_replace_into =>
101             [ 'bifkv', qw/key change_id change_id2/ ],
102             select =>
103             [ qv('last_sync'), 'MAX(c.id)', 'MAX(c.id)', ],
104             from => 'changes c',
105             );
106              
107             return $status;
108             }
109             );
110             };
111              
112             if ($@) {
113             $error = $@;
114             print "\n";
115             }
116              
117             return $cv->send;
118             };
119              
120             $cv->recv;
121             $client->disconnect;
122             return $self->err( 'Unknown', $error ) if $error;
123             return $self->ok('PullHub');
124              
125             }
126              
127             1;
128             __END__