| 1 | #!/usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # <LJDEP> |
|---|
| 4 | # lib: Proc::ProcessTable, cgi-bin/ljlib.pl |
|---|
| 5 | # </LJDEP> |
|---|
| 6 | |
|---|
| 7 | use strict; |
|---|
| 8 | require "$ENV{'LJHOME'}/cgi-bin/ljlib.pl"; |
|---|
| 9 | |
|---|
| 10 | use Proc::ProcessTable; |
|---|
| 11 | |
|---|
| 12 | my $DELAY = $LJ::QBUFFERD_DELAY || 15; |
|---|
| 13 | |
|---|
| 14 | my $pidfile = '/home/lj/var/qbufferd.pid'; |
|---|
| 15 | my $pid; |
|---|
| 16 | if (-e $pidfile) { |
|---|
| 17 | open (PID, $pidfile); |
|---|
| 18 | chomp ($pid = <PID>); |
|---|
| 19 | close PID; |
|---|
| 20 | my $processes = Proc::ProcessTable->new()->table; |
|---|
| 21 | if (grep { $_->cmndline =~ /qbufferd/ } @$processes) { |
|---|
| 22 | exit; |
|---|
| 23 | } |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | my $is_parent = 0; |
|---|
| 27 | my $running = 0; |
|---|
| 28 | |
|---|
| 29 | END { |
|---|
| 30 | unless ($is_parent || ! $running) { |
|---|
| 31 | print "END-STOP\n"; |
|---|
| 32 | &stop_qbufferd(); |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | $SIG{'INT'} = \&stop_qbufferd; |
|---|
| 37 | $SIG{'TERM'} = \&stop_qbufferd; |
|---|
| 38 | $SIG{'HUP'} = sub { |
|---|
| 39 | # nothing. maybe later make a HUP force a flush? |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | # Perhaps I should give it a command to not do this in the future. |
|---|
| 43 | if ($pid = fork) |
|---|
| 44 | { |
|---|
| 45 | $is_parent = 1; |
|---|
| 46 | open (PID, ">$pidfile") or die "Couldn't write PID file. Exiting.\n"; |
|---|
| 47 | print PID $pid, "\n"; |
|---|
| 48 | close PID; |
|---|
| 49 | print "qbufferd started with pid $pid\n"; |
|---|
| 50 | if (-s $pidfile) { print "pid file written ($pidfile)\n"; } |
|---|
| 51 | exit; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | sub stop_qbufferd |
|---|
| 55 | { |
|---|
| 56 | print "Quitting.\n"; |
|---|
| 57 | unlink $pidfile; |
|---|
| 58 | exit; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $running = 1; |
|---|
| 62 | while (LJ::start_request()) |
|---|
| 63 | { |
|---|
| 64 | my $cycle_start = time(); |
|---|
| 65 | |
|---|
| 66 | # do main cluster updates |
|---|
| 67 | my $dbh = LJ::get_dbh("master"); |
|---|
| 68 | if ($dbh) { |
|---|
| 69 | my $sth = $dbh->prepare("SELECT tablename, COUNT(*) FROM querybuffer GROUP BY 1"); |
|---|
| 70 | $sth->execute; |
|---|
| 71 | my @tables; |
|---|
| 72 | while (my ($table, $count) = $sth->fetchrow_array) { |
|---|
| 73 | push @tables, $table; |
|---|
| 74 | } |
|---|
| 75 | foreach my $table (@tables) { |
|---|
| 76 | my $count = LJ::query_buffer_flush($dbh, $table); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | # handle clusters |
|---|
| 81 | foreach my $c (@LJ::CLUSTERS) { |
|---|
| 82 | my $db = LJ::get_cluster_master($c); |
|---|
| 83 | next unless $db; |
|---|
| 84 | |
|---|
| 85 | my $sth = $db->prepare("SELECT cmd, COUNT(*) FROM cmdbuffer GROUP BY 1"); |
|---|
| 86 | $sth->execute; |
|---|
| 87 | my @cmds; |
|---|
| 88 | while (my ($cmd, $count) = $sth->fetchrow_array) { |
|---|
| 89 | LJ::cmd_buffer_flush($dbh, $db, $cmd); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | my $elapsed = time() - $cycle_start; |
|---|
| 94 | sleep ($DELAY-$elapsed) if $elapsed < $DELAY; |
|---|
| 95 | }; |
|---|