]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/hold_targeter.pl
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[Evergreen.git] / Open-ILS / src / support-scripts / hold_targeter.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------------
3 # Usage:
4 #   hold_targeter.pl <config_file> <lock_file>
5 # ---------------------------------------------------------------------
6
7 use strict; 
8 use warnings;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::System;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::MultiSession;
13
14 my $config = shift || die "bootstrap config required\n";
15 my $lockfile = shift || "/tmp/hold_targeter-LOCK";
16
17 if (-e $lockfile) {
18     die "I seem to be running already. If not remove $lockfile, try again\n";
19 }
20
21 open(F, ">$lockfile");
22 print F $$;
23 close F;
24
25 OpenSRF::System->bootstrap_client( config_file => $config );
26 my $settings = OpenSRF::Utils::SettingsClient->new;
27 my $parallel = $settings->config_value( hold_targeter => 'parallel' ) || 1; 
28
29 if ($parallel == 1) {
30
31     my $r = OpenSRF::AppSession
32                ->create( 'open-ils.storage' )
33                ->request( 'open-ils.storage.action.hold_request.copy_targeter' => '24h' );
34
35     while (!$r->complete) { 
36         my $start = time;
37         $r->recv(timeout => 3600);
38         last if (time() - $start) >= 3600;
39     };
40
41 } else {
42
43     my $multi_targeter = OpenSRF::MultiSession->new(
44         app => 'open-ils.storage', 
45         cap => $parallel, 
46         api_level => 1,
47         session_hash_function => sub {
48             my $ses = shift;
49             my $req = shift;
50             return $_[-1]; # last parameter is the ID of the metarecord associated with the
51                            # request's target; using this as the hash function value ensures
52                            # that parallel targeters won't try to simultaneously handle two
53                            # hold requests that have overlapping pools of copies that could
54                            # fill those requests
55         }
56     );
57
58     my $storage = OpenSRF::AppSession->create("open-ils.storage");
59
60     my $r = $storage->request('open-ils.storage.action.hold_request.targetable_holds.id_list', '24h');
61     while ( my $h = $r->recv ) {
62         die $r->failed->stringify . "\n" if $r->failed;
63         if (my $hold = $h->content) {
64             $multi_targeter->request( 'open-ils.storage.action.hold_request.copy_targeter', '', $hold->[0], $hold->[1]);
65         }
66     }
67
68     $storage->disconnect();
69
70     $multi_targeter->session_wait(1);
71     $multi_targeter->disconnect;
72
73 }
74
75 unlink $lockfile;
76