File Coverage

blib/lib/App/bif/sync.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::sync;
2 1     1   1939 use strict;
  1         2  
  1         95  
3 1     1   6 use warnings;
  1         2  
  1         40  
4 1     1   1495 use AnyEvent;
  1         5910  
  1         32  
5 1     1   571 use Bif::Sync::Client;
  0            
  0            
6             use Bif::Mo;
7             use Coro;
8             use DBIx::ThinSQL qw/qv/;
9              
10             our $VERSION = '0.1.5_6';
11             extends 'App::bif';
12              
13             sub run {
14             my $self = shift;
15             my $opts = $self->opts;
16             my $dbw = $self->dbw;
17              
18             # Consider upping PRAGMA cache_size? Or handle that in Bif::Role::Sync?
19             my @hubs = $dbw->xhashrefs(
20             select => [ 'h.id', 'n.name', 'h.location', 'n.uuid' ],
21             from => 'hubs h',
22             inner_join => 'nodes n',
23             on => 'n.id = h.id',
24             $opts->{hub} ? ( where => { 'n.name' => $opts->{hub} } ) : (),
25             order_by => 'n.name',
26             );
27              
28             return $self->err( 'SyncNone', 'no (matching) hubs found' )
29             unless @hubs;
30              
31             $|++; # no buffering
32              
33             foreach my $hub (@hubs) {
34             my $error;
35             my $cv = AE::cv;
36              
37             my $client = Bif::Sync::Client->new(
38             name => $hub->{name},
39             db => $dbw,
40             location => $hub->{location},
41             debug => $opts->{debug},
42             debug_bifsync => $opts->{debug_bifsync},
43             on_update => sub {
44             my $client = shift;
45             $self->lprint( $client->name . ': ' . $_[0] );
46             },
47             on_error => sub {
48             $error = shift;
49             $cv->send;
50             },
51             );
52              
53             my $fh = select;
54             my $coro = async {
55             select $fh;
56              
57             eval {
58             $dbw->txn(
59             sub {
60             my $start = time;
61              
62             my @status = $client->sync_hub( $hub->{id} );
63              
64             unless ( $status[0] eq 'HubMatch'
65             or $status[0] eq 'HubSync' )
66             {
67             $dbw->rollback;
68             $error =
69             "unexpected HubSync/Match status received: @status";
70             return;
71              
72             }
73              
74             if ( $status[0] eq 'HubSync' ) {
75             @status = $client->transfer_hub_changes;
76             if ( $status[0] ne 'TransferHubChanges' ) {
77             $dbw->rollback;
78             $error =
79             "unexpected TransferHubChanges status received: @status";
80             return;
81             }
82             }
83              
84             print "\n";
85             $self->lprint('');
86             my $hub_id = $client->hub_id;
87             my @projects = $dbw->xhashrefs(
88             select_distinct =>
89             [ 'p2.id AS id', 'n.path AS path' ],
90             from => 'projects p',
91             inner_join => 'nodes_tree nt',
92             on => 'nt.parent = p.id',
93             inner_join => 'projects p2',
94             on => 'p2.id = nt.child AND p2.local = 1',
95             inner_join => 'nodes n',
96             on => 'n.id = p2.id',
97             where => {
98             'p.default_hub_id' => $hub_id,
99             },
100             );
101              
102             foreach my $project (@projects) {
103             $client->name("$hub->{name}\[$project->{path}\]");
104             my $status =
105             $client->sync_project( $project->{id} );
106             if ( $status eq 'ProjectSync' ) {
107             @status =
108             $client->transfer_project_related_changes;
109              
110             if ( $status[0] ne
111             'TransferProjectRelatedChanges' )
112             {
113             $dbw->rollback;
114             $error =
115             "unexpected TransferProjectRelatedChanges status received: @status";
116             return;
117             }
118             }
119             elsif ( $status ne 'ProjectMatch' ) {
120             $dbw->rollback;
121             $error =
122             "unexpected sync_project status received: $status";
123             return $status;
124             }
125              
126             print "\n";
127             $self->lprint('');
128             }
129              
130             $dbw->xdo(
131             insert_or_replace_into =>
132             [ 'bifkv', qw/key change_id change_id2/ ],
133             select =>
134             [ qv('last_sync'), 'MAX(c.id)', 'MAX(c.id)', ],
135             from => 'changes c',
136             );
137              
138             $self->start_work(
139             node_id => $hub->{id},
140             start => $start,
141             start_comment => 'sync',
142             stop => time,
143             billable => 0,
144             );
145              
146             return;
147             }
148             );
149             };
150              
151             if ($@) {
152             $error = $@;
153             print "\n";
154             }
155              
156             return $cv->send;
157             };
158              
159             $cv->recv;
160             $client->disconnect;
161              
162             return $self->err( 'Unknown', $error ) if $error;
163              
164             }
165              
166             # TODO make this dependent on how many changes received/made since the last
167             # analyze
168             $dbw->do('ANALYZE');
169              
170             return $self->ok('Sync');
171             }
172              
173             1;
174              
175             __END__