Index: trunk/ippTasks/pstamp.pro
===================================================================
--- trunk/ippTasks/pstamp.pro	(revision 36633)
+++ trunk/ippTasks/pstamp.pro	(revision 36634)
@@ -152,4 +152,7 @@
         active true
     end
+    task pstamp.queue.cleanup
+        active true
+    end
 end
 macro pstamp.cleanup.off
@@ -158,4 +161,7 @@
     end
     task pstamp.cleanup.run
+        active false
+    end
+    task pstamp.queue.cleanup
         active false
     end
@@ -1119,2 +1125,35 @@
     end
 end
+
+task pstamp.queue.cleanup
+    host        local
+
+    periods     -poll $RUNPOLL
+    periods     -exec 600
+    periods     -timeout 120
+    npending    1
+
+    task.exec
+        stdout NULL
+        stderr $LOGSUBDIR/pstamp.queue.cleanup.log
+
+        $run = pstamp_queue_cleanup.pl
+        command $run
+    end
+
+    task.exit $EXIT_SUCCESS
+        # echo nothing to do
+    end
+
+    task.exit   default
+        showcommand failure
+    end
+
+    task.exit   crash
+        showcommand crash
+    end
+
+    task.exit   timeout
+        showcommand timeout
+    end
+end
Index: trunk/pstamp/scripts/Makefile.am
===================================================================
--- trunk/pstamp/scripts/Makefile.am	(revision 36633)
+++ trunk/pstamp/scripts/Makefile.am	(revision 36634)
@@ -11,4 +11,5 @@
 	pstampparse.pl \
 	pstamp_parser_run.pl \
+	pstamp_queue_cleanup.pl \
 	pstamp_queue_requests.pl \
 	pstamp_request_file \
Index: trunk/pstamp/scripts/pstamp_queue_cleanup.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_queue_cleanup.pl	(revision 36634)
+++ trunk/pstamp/scripts/pstamp_queue_cleanup.pl	(revision 36634)
@@ -0,0 +1,80 @@
+#!/bin/env perl
+#
+# run pstamptool to queue requests older than preserve-days to be cleaned
+
+use strict;
+use warnings;
+
+use PS::IPP::Config 1.01 qw( :standard );
+use IPC::Cmd 0.36 qw( can_run run );
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my $verbose;
+my $save_temps;
+my $preserve_days;
+my $dbname;
+my $dbserver;
+
+GetOptions(
+    'preserve-days=s'       => \$preserve_days,     # clean up requests that stopped this many days ago
+    'verbose'               => \$verbose,
+    'save-temps'            => \$save_temps,
+    'dbserver=s'            => \$dbserver,
+    'dbname=s'              => \$dbname,
+) or pod2usage( 2 );
+
+my $missing_tools;
+my $pstamptool   = can_run('pstamptool') 
+    or (warn "Can't find pstamptool" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+my $ipprc = PS::IPP::Config->new(); # IPP Configuration
+
+if (!$dbserver) {
+    $dbserver =  metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
+    $pstamptool .= " -dbserver $dbserver" if $dbserver;
+}
+
+if (!$dbname) {
+    $dbname =  metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBNAME');
+    $pstamptool .= " -dbname $dbname" if $dbname;
+}
+
+
+
+if (!$preserve_days) {
+    $preserve_days = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_PRESERVE_DAYS');
+    if (!$preserve_days) {
+        $preserve_days = 14;
+    }
+}
+
+if ($preserve_days < 0) {
+    warn("preserve-days cannot be negative\n" );
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+my $ticks = time();
+$ticks -= 86400 * $preserve_days;
+
+my ($sec, $min, $hour, $mday, $month, $year) = gmtime($ticks);
+$year += 1900;
+$month += 1;
+
+my $timestamp_end = sprintf "%4d-%02d-%02dT%02d:%02d:%02d", $year, $month, $mday, $hour, $min, $sec;
+
+my $command = "$pstamptool -updatereq -set_state goto_cleaned -state stop -timestamp_end $timestamp_end";
+my  ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    run(command => $command, verbose => $verbose);
+unless ($success) {
+    $error_code = (($error_code >> 8) or 1);
+    warn("$command failed. exit status: $error_code");
+    exit $error_code;
+}
+
+
+exit 0;
