Bug 33444: Update AddRenewal to take a hashref of params
[koha.git] / misc / cronjobs / process_message_queue.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use Koha::Script -cron;
24 use C4::Letters qw( SendQueuedMessages );
25 use C4::Log qw( cronlogaction );
26 use Getopt::Long qw( GetOptions );
27 use Try::Tiny qw( catch try );
28
29 my $username = undef;
30 my $password = undef;
31 my $limit    = undef;
32 my $method = 'LOGIN';
33 my $help = 0;
34 my $verbose = 0;
35 my $where;
36 my @type;
37 my @letter_code;
38
39 my $command_line_options = join(" ",@ARGV);
40
41 GetOptions(
42     'u|username:s'      => \$username,
43     'p|password:s'      => \$password,
44     'l|limit:s'         => \$limit,
45     'm|method:s'        => \$method,
46     'h|help|?'          => \$help,
47     'v|verbose'         => \$verbose,
48     't|type:s'          => \@type,
49     'c|code:s'          => \@letter_code,
50     'w|where:s'         => \$where,
51 );
52 my $usage = << 'ENDUSAGE';
53
54 This script processes the message queue in the message_queue database
55 table. It sends out the messages in that queue and marks them
56 appropriately to indicate success or failure. It is recommended that
57 you run this regularly from cron, especially if you are using the
58 advance_notices.pl script.
59
60 This script has the following parameters :
61     -u --username: username of mail account
62     -p --password: password of mail account
63     -t --type: If supplied, only processes this type of message ( email, sms ), repeatable
64     -c --code: If supplied, only processes messages with this letter code, repeatable
65     -l --limit: The maximum number of messages to process for this run
66     -m --method: authentication method required by SMTP server (See perldoc Sendmail.pm for supported authentication types.)
67     -h --help: this message
68     -v --verbose: provides verbose output to STDOUT
69     -w --where: filter messages to send with additional conditions in the where clause
70 ENDUSAGE
71
72 die $usage if $help;
73
74 my $script_handler = Koha::Script->new({ script => $0 });
75
76 try {
77     $script_handler->lock_exec;
78 }
79 catch {
80     my $message = "Skipping execution of $0 ($_)";
81     print STDERR "$message\n"
82         if $verbose;
83     cronlogaction({ info => $message });
84     exit;
85 };
86
87 cronlogaction({ info => $command_line_options });
88
89 if ( C4::Context->config("enable_plugins") ) {
90     my @plugins = Koha::Plugins->new->GetPlugins({
91         method => 'before_send_messages',
92     });
93
94     if (@plugins) {
95         foreach my $plugin ( @plugins ) {
96             try {
97                 $plugin->before_send_messages(
98                     {
99                         verbose     => $verbose,
100                         limit       => $limit,
101                         type        => \@type,
102                         letter_code => \@letter_code,
103                     }
104                 );
105             }
106             catch {
107                 warn "$_";
108             };
109         }
110     }
111 }
112
113 C4::Letters::SendQueuedMessages(
114     {
115         verbose     => $verbose,
116         username    => $username,
117         password    => $password,
118         method      => $method,
119         limit       => $limit,
120         type        => \@type,
121         letter_code => \@letter_code,
122         where       => $where,
123     }
124 );
125
126 cronlogaction({ action => 'End', info => "COMPLETED" });