source: web/trunk/bin/sophie_scan @ 6

Last change on this file since 6 was 6, checked in by nanardon, 13 years ago
  • add base updater daemon
File size: 2.2 KB
Line 
1#!/bin/env perl
2
3use strict;
4use warnings;
5use Sophie::Base;
6use Sophie::Base::RpmsPath;
7use Linux::Inotify2;
8use POSIX ":sys_wait_h";
9
10$SIG{ALRM} = sub {};
11$SIG{CHLD} = sub { wait };
12
13my %modified_paths;
14
15while (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
24sub 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->fullname;
36            }
37        )
38    }
39
40    $i;
41}
42
43sub update_base {
44    my @path = @_;
45
46    if (waitpid(-1, &WNOHANG) != -1) {
47        warn "child still running";
48        return;
49    }
50    warn "updating base";
51
52    my @pkey;
53    {
54        my $sophie = Sophie::Base->connect;
55        @pkey = $sophie->resultset('Paths')->search(
56            path => [ @path ],
57        )->get_column('d_path_key')->all;
58        push(@pkey, $sophie->resultset('Paths')->search({
59            updated => [ undef, 
60                \[ " > now() - '6 hours'::interval"],
61            ],
62        })->get_column('d_path_key')->all);
63        my %uniq = map { $_ => 1 } @pkey;
64        @pkey = keys %uniq;
65    }
66
67    return 1 if (!@pkey);
68
69    if (my $pid = fork()) {
70    } else {
71        foreach my $pathkey (@pkey) {
72            my @delta = Sophie::Base::RpmsPath->new($pathkey)
73                ->find_delta;
74            while (my @d = splice(@delta, 0, 25)) {
75                my $path = Sophie::Base::RpmsPath->new($pathkey);
76                $path->update_content(@d);
77                $path->db->disconnect;
78            }
79        }
80        exit(0);
81    }
82    return 1;
83}
84
85
86#
87#my $i = Linux::Inotify2->new;
88#$i->watch("/home/olivier", IN_DELETE | IN_MODIFY | IN_CREATE,
89#    sub {
90#        warn "rr";
91#        my $e = shift;
92#        warn $e->fullname;
93#    }
94#);
95#$i->watch("/tmp", IN_DELETE | IN_MODIFY | IN_CREATE,
96#    sub {
97#        warn "rr";
98#        my $e = shift;
99#        warn $e->fullname;
100#    }
101#);
102#
103#1 while $i->poll;
Note: See TracBrowser for help on using the repository browser.