add bse_nightly.pl to run a list of BSE background tasks (which are
authorTony Cook <tony@develop-help.com>
Fri, 19 Mar 2010 07:07:53 +0000 (07:07 +0000)
committertony <tony@45cb6cf1-00bc-42d2-bb5a-07f51df49f94>
Fri, 19 Mar 2010 07:07:53 +0000 (07:07 +0000)
run in foreground, to avoid hosing the machine with several new
background processes.)

change the background process foreground handling to avoid exit

MANIFEST
schema/bse.sql
site/cgi-bin/modules/BSE/TB/BackgroundTask.pm
site/docs/config.pod
site/util/bse_nightly.pl [new file with mode: 0644]

index 5ab76389a185b953bcba39a96099713ad6197175..a2781ab6dac95ea5985e4d273aa516d3e9eb389e 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -649,6 +649,7 @@ site/templates/user/userpage_base.tmpl
 site/templates/xbase.tmpl
 site/util/bseaddimages.pl
 site/util/bse_back.pl
+site/util/bse_nightly.pl
 site/util/bse_notify_files.pl
 site/util/bse_s3.pl
 site/util/bse_session_clean.pl
index d2a58f1c73660906069bbe57f528cbcb6dd199a5..041136ae34cd754098652a79b9a75586a9979a39 100644 (file)
@@ -157,6 +157,8 @@ CREATE TABLE sessions (
   a_session text,
   -- so we can age this table
   whenChanged timestamp
+  -- note: an index on whenChanged would speed up only the rare case 
+  -- of bse_session_clean.pl, think hard before adding an index
 );
 
 -- these share data with the article table
@@ -1007,6 +1009,6 @@ create table bse_background_tasks (
   -- last completion time
   last_completion datetime null,
 
-  -- longer description
+  -- longer description - formatted as HTML
   long_desc text null
 );
index bd0f12d46aba675555387e23eb4e1a0a9f8ef574..439a93061cfc2c73d7d906f7b593438bc25a6930 100644 (file)
@@ -114,7 +114,9 @@ sub start {
     or die "Cannot redirect stdout: $!";
   untie *STDERR;
   open STDERR, ">&STDOUT" or die "Cannot redirect STDOUT: $!";
+  unless ($foreground) {
     POSIX::setsid();
+  }
 
   my $pid2 = fork;
   unless (defined $pid2) {
@@ -122,7 +124,12 @@ sub start {
     $self->set_running(0);
     $self->set_task_pid(undef);
     $self->save;
-    exit;
+    if ($foreground) {
+      $$msg = "Cannot start child: $!\n";
+    }
+    else {
+      exit;
+    }
   }
   
   if ($pid2) {
@@ -144,7 +151,12 @@ sub start {
     $task->set_last_exit($?);
     $task->set_last_completion(now_sqldatetime());
     $task->save;
-    exit;
+    if ($foreground) {
+      return $pid2;
+    }
+    else {
+      exit;
+    }
   }
   else {
     BSE::DB->forked;
index f883e36a656177a8c00b5402fbf7c025edaa781e..cec824b66f8f6efd4bf155c12dfb969a0dc00caa 100644 (file)
@@ -2017,6 +2017,46 @@ empty.  Useful for including a unit ("pixels") or format help
 
 =back
 
+=head2 [session cleanup]
+
+Controls the processing of the bse_session_clean.pl script.
+
+=over
+
+=item *
+
+days - the minimum age in days of sessions to be removed.  Default: 30.
+
+=item *
+
+per - the number of records to remove per SQL delete request.
+Default: 1000.
+
+=item *
+
+count - the number of SQL delete requests to perform, maximum.
+Default: 1000.
+
+=item *
+
+optimize - whether to perform a table optimization after deleting
+records.  Default: 1 (set to 0 to skip optimization)
+
+=back
+
+=head2 [nightly work]
+
+Controls the bse_nightly.pl script.
+
+=over
+
+=item *
+
+jobs - a comma separated list of BSE background processes to run when
+bse_nightly.pl is run.  Default: bse_session_clean
+
+=back
+
 =head1 AUTHOR
 
 Tony Cook <tony@develop-help.com>
diff --git a/site/util/bse_nightly.pl b/site/util/bse_nightly.pl
new file mode 100644 (file)
index 0000000..de513fd
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin/../cgi-bin/modules";
+use BSE::API qw(bse_init bse_cfg);
+use BSE::TB::BackgroundTasks;
+use Getopt::Long;
+use Time::HiRes qw(time);
+
+my $start_time = time;
+my $verbose;
+GetOptions("v:i" => \$verbose);
+!$verbose and defined $verbose and $verbose = 1;
+
+open MYSTDOUT, ">&STDOUT" or die "Cannot dup STDOUT: $!\n";
+
+{
+  bse_init("../cgi-bin");
+  my $cfg = bse_cfg();
+
+  my $def_nightly = "bse_session_clean";
+
+  my @tasks = split /,/, $cfg->entry("nightly work", "jobs", $def_nightly);
+
+
+ TASK:
+  for my $task_id (@tasks) {
+    my $task = BSE::TB::BackgroundTasks->getByPkey($task_id);
+
+    unless ($task) {
+      warn "Unknown task id $task_id\n";
+      next TASK;
+    }
+
+    if ($task->check_running) {
+      msg(1, "$task_id is already running - skipping\n");
+      next TASK;
+    }
+
+    my $msg;
+    msg(1, "Starting $task_id\n");
+    my $pid = $task->start
+      (
+       cfg => $cfg,
+       msg => \$msg,
+       foreground => 1,
+      ) or msg(0, "Could not start $task_id: $msg\n");
+  }
+  msg(1, "Background processing complete\n");
+
+  exit;
+}
+
+sub msg {
+  my ($level, $text) = @_;
+
+  if ($level <= $verbose) {
+    my $diff = time() - $start_time;
+    printf MYSTDOUT "%.2f: %s", $diff, $text;
+  }
+}