1 | #!/bin/env perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Sophie::Base; |
---|
6 | use Sophie::Base::RpmsPath; |
---|
7 | use Linux::Inotify2; |
---|
8 | use POSIX ":sys_wait_h"; |
---|
9 | |
---|
10 | $SIG{ALRM} = sub {}; |
---|
11 | $SIG{CHLD} = sub { wait }; |
---|
12 | |
---|
13 | my %modified_paths; |
---|
14 | |
---|
15 | while (1) { |
---|
16 | alarm 100; |
---|
17 | if (update_base(keys %modified_paths)) { |
---|
18 | %modified_paths = (); |
---|
19 | } |
---|
20 | my $inotify = inotify_path(); |
---|
21 | $inotify->poll; |
---|
22 | } |
---|
23 | |
---|
24 | sub inotify_path { |
---|
25 | |
---|
26 | my $i = Linux::Inotify2->new; |
---|
27 | my $sophie = Sophie::Base->connect; |
---|
28 | |
---|
29 | foreach ($sophie->resultset('Paths')->get_column('path')->all) { |
---|
30 | $i->watch( |
---|
31 | $_, |
---|
32 | IN_DELETE | IN_MODIFY | IN_CREATE, |
---|
33 | sub { |
---|
34 | my $e = shift; |
---|
35 | warn $e->name; |
---|
36 | $modified_paths{$e->name} = 1; |
---|
37 | } |
---|
38 | ) |
---|
39 | } |
---|
40 | |
---|
41 | $i; |
---|
42 | } |
---|
43 | |
---|
44 | sub update_base { |
---|
45 | my @path = @_; |
---|
46 | |
---|
47 | if (waitpid(-1, &WNOHANG) != -1) { |
---|
48 | warn "child still running"; |
---|
49 | return; |
---|
50 | } |
---|
51 | warn "updating base"; |
---|
52 | |
---|
53 | my @pkey; |
---|
54 | { |
---|
55 | my $sophie = Sophie::Base->connect; |
---|
56 | @pkey = $sophie->resultset('Paths')->search( |
---|
57 | path => [ @path ], |
---|
58 | )->get_column('d_path_key')->all; |
---|
59 | push(@pkey, $sophie->resultset('Paths')->search({ |
---|
60 | updated => [ undef, |
---|
61 | \[ " < now() - '6 hours'::interval"], |
---|
62 | ], |
---|
63 | })->get_column('d_path_key')->all); |
---|
64 | my %uniq = map { $_ => 1 } @pkey; |
---|
65 | @pkey = keys %uniq; |
---|
66 | } |
---|
67 | |
---|
68 | return 1 if (!@pkey); |
---|
69 | |
---|
70 | if (my $pid = fork()) { |
---|
71 | } else { |
---|
72 | foreach my $pathkey (@pkey) { |
---|
73 | my @delta = Sophie::Base::RpmsPath->new($pathkey) |
---|
74 | ->find_delta; |
---|
75 | while (my @d = splice(@delta, 0, 25)) { |
---|
76 | my $path = Sophie::Base::RpmsPath->new($pathkey); |
---|
77 | $path->update_content(@d); |
---|
78 | $path->db->disconnect; |
---|
79 | } |
---|
80 | Sophie::Base::RpmsPath->new($pathkey)->set_updated; |
---|
81 | } |
---|
82 | exit(0); |
---|
83 | } |
---|
84 | return 1; |
---|
85 | } |
---|
86 | |
---|