Changeset 434


Ignore:
Timestamp:
06/25/12 16:55:47 (12 years ago)
Author:
nanardon
Message:
  • sophie-scan can run as daemon
Location:
server/trunk/web
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • server/trunk/web/bin/sophie_scan

    r420 r434  
    55use Getopt::Long; 
    66use Sophie::Scan; 
     7use Sys::Syslog; 
     8use Pod::Usage; 
     9 
     10GetOptions( 
     11    'd' => \my $daemon, 
     12    'sleep=i' => \my $sleep, 
     13    'runas=s' => \my $runas, 
     14    'pidfile=s' => \my $pidfile, 
     15) or do { pod2usage(1) }; 
    716 
    817$ENV{LC_ALL} = 'C'; 
    918require Sophie::Scan::RpmsPath; 
    1019 
    11 my $scan = Sophie::Scan->new; 
    12 $scan->update_meta_paths; 
     20sub process { 
     21    my $scan = Sophie::Scan->new; 
     22    $scan->update_meta_paths; 
    1323 
    14 my @pkey = $scan->list_unscanned_paths; 
     24    my @pkey = $scan->list_unscanned_paths; 
    1525 
    16 foreach my $pathkey (@pkey) { 
    17      
    18     my $time = time; 
    19     my $mark = 0; 
    20     my @delta; 
     26    foreach my $pathkey (@pkey) { 
     27         
     28        my $time = time; 
     29        my $mark = 0; 
     30        my @delta; 
    2131 
    22     { 
    23         my $path = Sophie::Scan::RpmsPath 
    24             ->new($pathkey, Sophie::Scan->new); 
    25         #$path->set_no_needupdate; 
     32        { 
     33            my $path = Sophie::Scan::RpmsPath 
     34                ->new($pathkey, Sophie::Scan->new); 
    2635 
    27         $mark = $path->get_needupdate; 
    28         @delta = $path->find_delta; 
    29     } 
     36            $mark = $path->get_needupdate; 
     37            @delta = $path->find_delta; 
     38        } 
    3039 
    31     while (my @d = splice(@delta, 0, 10)) { 
    32         my $scan = Sophie::Scan->new; 
    33         my $path = Sophie::Scan::RpmsPath->new($pathkey, $scan); 
    34         $path->update_content(@d); 
    35         last if (time > $time + 7 * 60); 
    36     } 
     40        while (my @d = splice(@delta, 0, 10)) { 
     41            my $scan = Sophie::Scan->new; 
     42            my $path = Sophie::Scan::RpmsPath->new($pathkey, $scan); 
     43            $path->update_content(@d); 
     44            last if (time > $time + 7 * 60); 
     45        } 
    3746 
    38     if (!@delta) { # update only if we finished 
    39         my $path = Sophie::Scan::RpmsPath->new($pathkey, Sophie::Scan->new); 
    40         $path->set_updated; 
    41         $path->set_no_needupdate 
    42             unless ($mark ne $path->get_needupdate); 
     47        if (!@delta) { # update only if we finished 
     48            my $path = Sophie::Scan::RpmsPath->new($pathkey, Sophie::Scan->new); 
     49            $path->set_updated; 
     50            $path->set_no_needupdate 
     51                unless ($mark ne $path->get_needupdate); 
     52        } 
    4353    } 
    4454} 
    4555 
     56openlog('sophie-notify', 'ndelay,pid' . ($daemon ? '' : ',perror'), 'user'); 
     57$SIG{__WARN__} = sub { 
     58    syslog('warning', @_); 
     59}; 
     60$SIG{__DIE__} = sub { 
     61    syslog('crit', @_); 
     62    exit(1); 
     63}; 
     64 
     65if ($daemon) { 
     66    if (fork()) { 
     67        exit(0); 
     68    } 
     69    warn "Fork done, entering daemon mode\n"; 
     70} 
     71 
     72if ($pidfile) { 
     73    if (open(my $handle, '>', $pidfile)) { 
     74        print $handle $$ . "\n"; 
     75        close($handle); 
     76    } else { 
     77        die "Can't write pidfile $pidfile, exiting :\\\n"; 
     78    } 
     79} 
     80 
     81 
     82if ($runas) { 
     83    my ($login,$pass,$uid,$gid) = $runas =~ /^\d/ 
     84    ? getpwuid($runas) 
     85    : getpwnam($runas); 
     86 
     87    $> = $uid; $) = $gid; 
     88    if ($> ne $uid) { 
     89        die "Cannot change to user $runas\n"; 
     90    } 
     91} 
     92 
     93$sleep ||= 60; 
     94 
     95while (1) { 
     96    my $csleep = int(rand($sleep) + ($sleep / 2 )); 
     97    my $currenttime = time; 
     98    if (my $pid = fork) { 
     99        waitpid($pid, 0); 
     100    } else { 
     101        process(); 
     102        exit(0); 
     103    } 
     104    my $delay = $csleep - (time - $currenttime); 
     105 
     106    if ($delay > 0) { 
     107        warn "sleep $delay\n"; 
     108        sleep $delay; 
     109    } 
     110} 
  • server/trunk/web/lib/Sophie/Controller/Admin.pm

    r375 r434  
    455455} 
    456456 
     457sub help : private { 
     458    my ( $self, $c, $cmd ) = @_; 
     459    my $ctx = $c->session->{admin_ctx} || ''; 
     460    my $module = 'Admin::Cli' . ($ctx ? "::$ctx" : ''); 
     461    if ($cmd) { 
     462        my @message = grep { /\S+/ } split(/\n/, 
     463            $c->model('Help::POD')->admin_help_text($ctx, $cmd) || 'No help available'); 
     464        return $c->stash->{xmlrpc} = [ $self->prompt($c), \@message ]; 
     465    } else { 
     466        return $c->stash->{xmlrpc} = [ 
     467            $self->prompt($c), 
     468            [ 
     469                'available command:', 
     470                join(', ', sort grep { $_ !~ /^end$/ } 
     471                    ('help', @{ $c->controller($module)->_commands })), 
     472            ] 
     473        ]; 
     474    } 
     475}    
     476 
     477sub prompt : XMLRPC { 
     478    my ($self, $c) = @_; 
     479    my $ctx = $c->session->{admin_ctx} || ''; 
     480    my $path = '/admin/cli' . ($ctx ? lc("/$ctx") : ''); 
     481    if ($c->get_action( 'prompt', $path )) { 
     482        return $c->stash->{xmlrpc} = $c->forward("$path/prompt"); 
     483    } else { 
     484        return $c->stash->{xmlrpc} = '> '; 
     485    } 
     486} 
     487 
     488sub cli : XMLRPC { 
     489    my ($self, $c, $cmd, @args) = @_; 
     490 
     491    if ($cmd eq 'help') { 
     492        $c->go('help', [ @args ]); 
     493    } 
     494     
     495    my $ctx = $c->session->{admin_ctx} || ''; 
     496    my $path = '/admin/cli' . ($ctx ? lc("/$ctx") : ''); 
     497    if ($c->get_action( $cmd, $path )) { 
     498        return $c->go($path . '/' . $cmd, [ @args ]); 
     499    } else { 
     500        $c->error( "No such command $cmd" ); 
     501    } 
     502} 
     503 
     504sub complete : XMLRPC { 
     505    my ($self, $c, $cmd, @args) = @_; 
     506 
     507    my $ctx = $c->session->{admin_ctx} || ''; 
     508    my $path = '/admin/cli' . ($ctx ? lc("/$ctx") : ''); 
     509    if ($c->get_action( "_c_$cmd", $path )) { 
     510        my $vals = $c->go($path . '/' . "_c_$cmd", [ @args ]); 
     511        return $args[-1] ? [ grep { index($_, $args[-1]) == 0 } @$vals ] : $vals; 
     512    } else { 
     513        return $c->stash->{xmlrpc} = []; 
     514    } 
     515} 
     516 
     517 
    457518=head1 AUTHOR 
    458519 
  • server/trunk/web/lib/Sophie/Model/Help/POD.pm

    r260 r434  
    7676} 
    7777 
     78sub admin_help_text { 
     79    my ($self, $context, $cmd) = @_; 
     80    my $module = 'Admin::Cli' . ($context ? "::$context" : ''); 
     81    my $botpom = $self->{pom}{$module}; 
     82    foreach my $head1 ($botpom->content) { 
     83        $head1->title eq 'AVAILABLE FUNCTIONS' or next; 
     84        foreach ($head1->content) { 
     85            $_->title =~ /^\Q$cmd\E( |$)/ or next; 
     86            my $ppvt = Pod::POM::View::Text->new; 
     87            return $_->present($ppvt); 
     88        } 
     89        last; 
     90    } 
     91    return; 
     92} 
     93 
    7894sub chat_functions { 
    7995    my ($self) = @_; 
  • server/trunk/web/lib/Sophie/Scan/RpmsPath.pm

    r419 r434  
    6565    my ($self) = @_; 
    6666 
    67     warn "$$ " . $self->path; 
     67    warn "Scanning " . $self->path . "\n"; 
    6868 
    6969    my @delta; 
     
    152152sub remove_rpm { 
    153153    my ($self, $rpm) = @_; 
    154     warn "$$ deleting $rpm"; 
     154    warn "Deleting $rpm\n"; 
    155155    $self->db->base->storage->txn_do( 
    156156        sub { 
     
    166166    my ($self, $rpm) = @_; 
    167167 
    168     warn "$$ adding $rpm"; 
     168    -f $self->path . '/' . $rpm or return; 
     169    warn "Adding $rpm\n"; 
    169170    my @stat = stat($self->path . '/' . $rpm); 
    170171    eval { 
     
    199200    }; 
    200201    $header or do { 
    201         warn "$$ Cannot read " . $self->path . '/' . $rpm; 
     202        warn "Cannot read " . $self->path . "/$rpm\n"; 
    202203        return ""; 
    203204    }; 
     
    208209        )->get_column('pkgid')->all; 
    209210        if ($find) { 
    210             warn "$$ Find"; 
    211211            return($header->queryformat('%{PKGID}'), 0); 
    212212        } 
Note: See TracChangeset for help on using the changeset viewer.