source: web/trunk/bin/sophie_scan @ 10

Last change on this file since 10 was 10, checked in by nanardon, 14 years ago
  • paralize path update
File size: 2.4 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 30;
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->w->name;
36                $modified_paths{$e->w->name} = 1;
37            }
38        )
39    }
40
41    $i;
42}
43
44sub 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
73
74        my $NB_PAR = 2;
75        my @split;
76        my $div = @pkey / $NB_PAR;
77
78        for (my $i = 0; $i < $NB_PAR - 1; $i++) {
79            $split[$i] = [ splice(@pkey, 0, $div) ];
80        }
81        $split[$NB_PAR -1] = [ @pkey ];
82        foreach my $job (@split) {
83            @{$job || []} or next;
84
85            if (fork()) {
86            } else {
87
88                foreach my $pathkey (@{ $job }) {
89                    my @delta = Sophie::Base::RpmsPath->new($pathkey)
90                    ->find_delta;
91                    while (my @d = splice(@delta, 0, 25)) {
92                        my $path = Sophie::Base::RpmsPath->new($pathkey);
93                        $path->update_content(@d);
94                        $path->db->disconnect;
95                    }
96                    Sophie::Base::RpmsPath->new($pathkey)->set_updated;
97                }
98                exit(0);
99            }
100        }
101        1 while(waitpid(-1, 0) <= 0);
102    }
103    return 1;
104}
105
Note: See TracBrowser for help on using the repository browser.