#----------------------------------------------------------------
my $osrf_config = shift || '/openils/conf/opensrf_core.xml';
+my $lockfile = shift || "/tmp/hold_targeter-LOCK";
-OpenSRF::System->bootstrap_client(config_file => $osrf_config);
-Fieldmapper->import(
- IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
-OpenILS::Utils::CStoreEditor::init();
+if (-e $lockfile) {
+ die "I seem to be running already. If not remove $lockfile, try again\n";
+}
-my $targeter = OpenILS::Utils::HoldTargeter->new;
+open(F, ">$lockfile") or die "Cannot open lock file: $lockfile : $@\n";
+print F $$ or die "Cannot write to lock file: $lockfile : $@\n";
+close F;
-my $start = time;
-my $count = $targeter->target(
- # Return only the number processed,
- # instead of a result blob for each hold.
- return_count => 1
-);
+eval { # Make sure we can delete the lock file.
-my $minutes = sprintf('%0.2f', (time - $start) / 60);
+ OpenSRF::System->bootstrap_client(config_file => $osrf_config);
+ Fieldmapper->import(
+ IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
+ OpenILS::Utils::CStoreEditor::init();
+
+ my $targeter = OpenILS::Utils::HoldTargeter->new;
+
+ my $start = time;
+ my $count = $targeter->target(
+ # Return only the number processed,
+ # instead of a result blob for each hold.
+ return_count => 1
+ );
+
+ my $minutes = sprintf('%0.2f', (time - $start) / 60);
+
+ print "Processed $count holds in $minutes minutes.\n";
+};
+
+unlink $lockfile;
-print "Processed $count holds in $minutes minutes.\n";