View | Details | Raw Unified | Return to bug 41245
Collapse All | Expand All

(-)a/debian/docs/koha-worker.xml (+16 lines)
Lines 27-32 Link Here
27
      <arg><option>--start</option>|<option>--stop</option>|<option>--restart</option></arg>
27
      <arg><option>--start</option>|<option>--stop</option>|<option>--restart</option></arg>
28
      <arg><option>--status</option></arg>
28
      <arg><option>--status</option></arg>
29
      <arg><option>--queue</option></arg>
29
      <arg><option>--queue</option></arg>
30
      <arg><option>--type</option></arg>
31
      <arg><option>--not-type</option></arg>
30
      <arg><option>--quiet</option>|<option>-q</option></arg>
32
      <arg><option>--quiet</option>|<option>-q</option></arg>
31
      <arg><option>-h</option>|<option>--help</option></arg>
33
      <arg><option>-h</option>|<option>--help</option></arg>
32
    </cmdsynopsis>
34
    </cmdsynopsis>
Lines 67-72 Link Here
67
          <para>Note: There used to be a queue called elastic_index, but since the introduction of koha-es-indexer this queue should not be active.</para>
69
          <para>Note: There used to be a queue called elastic_index, but since the introduction of koha-es-indexer this queue should not be active.</para>
68
        </listitem>
70
        </listitem>
69
    </varlistentry>
71
    </varlistentry>
72
    <varlistentry>
73
        <term><option>--type</option></term>
74
        <listitem>
75
          <para>Allows specifying the background job type this worker.</para>
76
          <para>Current types are: pseudonymize_statistic, marc_import_commit_batch, update_elastic_index, update_holds_queue_for_biblios, batch_item_record_modification</para>
77
        </listitem>
78
    </varlistentry>
79
    <varlistentry>
80
        <term><option>--not-type</option></term>
81
        <listitem>
82
          <para>Allows specifying the background job type this worker to not process.</para>
83
          <para>This option operates as the inverse of --type.</para>
84
        </listitem>
85
    </varlistentry>
70
    <varlistentry>
86
    <varlistentry>
71
      <term><option>-h</option>|<option>--help</option></term>
87
      <term><option>-h</option>|<option>--help</option></term>
72
      <listitem>
88
      <listitem>
(-)a/debian/scripts/koha-worker (-18 / +64 lines)
Lines 40-46 $scriptname Link Here
40
This script lets you manage the worker daemon for your Koha instances.
40
This script lets you manage the worker daemon for your Koha instances.
41
41
42
Usage:
42
Usage:
43
$scriptname [--start|--stop|--restart] [--queue queue_name] [--quiet|-q] instancename1 [instancename2...]
43
$scriptname [--start|--stop|--restart] [--queue queue_name] [--type job_type] [--not-type job_type] [--quiet|-q] instancename1 [instancename2...]
44
$scriptname --status instancename1 [instancename2...]
44
$scriptname --status instancename1 [instancename2...]
45
$scriptname -h|--help
45
$scriptname -h|--help
46
46
Lines 49-54 $scriptname -h|--help Link Here
49
    --restart             Restart the worker daemon for the specified instances
49
    --restart             Restart the worker daemon for the specified instances
50
    --queue               Specify the queue/worker to restart - 'default' is used if not specified
50
    --queue               Specify the queue/worker to restart - 'default' is used if not specified
51
                          current queues are: default, long_tasks
51
                          current queues are: default, long_tasks
52
    --type                Specify the job type for this worker to process
53
    --not-type            Specify the job type for this worker to skip processing
52
    --status              Show the status of the worker for the specified instances
54
    --status              Show the status of the worker for the specified instances
53
    --quiet|-q            Make the script quiet about non existent instance names
55
    --quiet|-q            Make the script quiet about non existent instance names
54
                          (useful for calling from another scripts).
56
                          (useful for calling from another scripts).
Lines 64-73 start_worker() Link Here
64
{
66
{
65
    local name=$1
67
    local name=$1
66
    local queue=$2
68
    local queue=$2
69
    local job_type=$3
70
    local not_job_type=$4
67
71
68
    if ! is_worker_running $name $queue; then
72
    if ! is_worker_running $name $queue $job_type $not_job_type; then
69
73
70
        worker_name=`get_worker_name $name $queue`
74
        worker_name=`get_worker_name $name $queue $job_type $notjob__type`
71
75
72
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
76
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
73
77
Lines 78-92 start_worker() Link Here
78
            --verbose=1 --respawn --delay=30 \
82
            --verbose=1 --respawn --delay=30 \
79
            --user=$name-koha.$name-koha"
83
            --user=$name-koha.$name-koha"
80
84
81
        log_daemon_msg "Starting Koha worker daemon for $name ($queue)"
85
        local job_limits=""
86
        if $job_type; then
87
            job_limits="--job-type $job_type"
88
        elif $not_job_type; then
89
            job_limits="--not-job-type $not_job_type"
90
        fi
91
92
        log_daemon_msg "Starting Koha worker daemon for $name --queue $queue $job_limits"
82
93
83
        if daemon $DAEMONOPTS -- $worker_DAEMON --queue ${queue}; then
94
        if daemon $DAEMONOPTS -- $worker_DAEMON --queue ${queue} $job_limits; then
84
            log_end_msg 0
95
            log_end_msg 0
85
        else
96
        else
86
            log_end_msg 1
97
            log_end_msg 1
87
        fi
98
        fi
88
    else
99
    else
89
        log_daemon_msg "Error: worker already running for $name ($queue)"
100
        log_daemon_msg "Error: worker already running for --queue $queue $job_limits"
90
        log_end_msg 1
101
        log_end_msg 1
91
    fi
102
    fi
92
}
103
}
Lines 95-100 stop_worker() Link Here
95
{
106
{
96
    local name=$1
107
    local name=$1
97
    local queue=$2
108
    local queue=$2
109
    local job_type=$3
110
    local not_job_type=$4
98
111
99
    if is_worker_running $name $queue; then
112
    if is_worker_running $name $queue; then
100
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
113
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
Lines 108-122 stop_worker() Link Here
108
            --verbose=1 --respawn --delay=30 \
121
            --verbose=1 --respawn --delay=30 \
109
            --user=$name-koha.$name-koha"
122
            --user=$name-koha.$name-koha"
110
123
111
        log_daemon_msg "Stopping Koha worker daemon for $name ($queue)"
124
        local job_limits=""
125
        if $job_type; then
126
            job_limits="--job-type $job_type"
127
        elif $not_job_type; then
128
            job_limits="--not-job-type $not_job_type"
129
        fi
130
131
        log_daemon_msg "Stopping Koha worker daemon for --queue $queue $job_limits"
112
132
113
        if daemon $DAEMONOPTS --stop -- $worker_DAEMON --queue ${queue}; then
133
        if daemon $DAEMONOPTS --stop -- $worker_DAEMON --queue ${queue} $job_limits; then
114
            log_end_msg 0
134
            log_end_msg 0
115
        else
135
        else
116
            log_end_msg 1
136
            log_end_msg 1
117
        fi
137
        fi
118
    else
138
    else
119
        log_daemon_msg "Error: worker not running for $name ($queue)"
139
        log_daemon_msg "Error: worker not running for --queue $queue $job_limits"
120
        log_end_msg 1
140
        log_end_msg 1
121
    fi
141
    fi
122
}
142
}
Lines 125-130 restart_worker() Link Here
125
{
145
{
126
    local name=$1
146
    local name=$1
127
    local queue=$2
147
    local queue=$2
148
    local job_type=$3
149
    local not_job_type=$4
128
150
129
    if is_worker_running $name $queue; then
151
    if is_worker_running $name $queue; then
130
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
152
        export KOHA_CONF="/etc/koha/sites/$name/koha-conf.xml"
Lines 138-152 restart_worker() Link Here
138
            --verbose=1 --respawn --delay=30 \
160
            --verbose=1 --respawn --delay=30 \
139
            --user=$name-koha.$name-koha"
161
            --user=$name-koha.$name-koha"
140
162
141
        log_daemon_msg "Restarting Koha worker daemon for $name ($queue)"
163
        local job_limits=""
164
        if $job_type; then
165
            job_limits="--job-type $job_type"
166
        elif $not_job_type; then
167
            job_limits="--not-job-type $not_job_type"
168
        fi
169
170
        log_daemon_msg "Restarting Koha worker daemon for --queue $queue $job_limits"
142
171
143
        if daemon $DAEMONOPTS --restart -- $worker_DAEMON --queue ${queue}; then
172
        if daemon $DAEMONOPTS --restart -- $worker_DAEMON --queue ${queue} $job_limits; then
144
            log_end_msg 0
173
            log_end_msg 0
145
        else
174
        else
146
            log_end_msg 1
175
            log_end_msg 1
147
        fi
176
        fi
148
    else
177
    else
149
        log_warning_msg "Worker not running for $name ($queue)."
178
        log_warning_msg "Worker not running for --queue $queue $job_limits."
150
        start_worker $name $queue
179
        start_worker $name $queue
151
    fi
180
    fi
152
}
181
}
Lines 155-166 worker_status() Link Here
155
{
184
{
156
    local name=$1
185
    local name=$1
157
    local queue=$2
186
    local queue=$2
187
    local job_type=$3
188
    local not_job_type=$4
189
190
    local job_limits=""
191
    if $job_type; then
192
        job_limits="--job-type $job_type"
193
    elif $not_job_type; then
194
        job_limits="--not-job-type $not_job_type"
195
    fi
158
196
159
    if is_worker_running ${name} ${queue}; then
197
    if is_worker_running ${name} ${queue}; then
160
        log_daemon_msg "worker running for ${name} ($queue)"
198
        log_daemon_msg "worker running for ${name} --queue $queue $job_limits"
161
        log_end_msg 0
199
        log_end_msg 0
162
    else
200
    else
163
        log_daemon_msg "worker not running for ${name} ($queue)"
201
        log_daemon_msg "worker not running for ${name} --queue $queue $job_limits"
164
        log_end_msg 3
202
        log_end_msg 3
165
    fi
203
    fi
166
}
204
}
Lines 177-182 set_action() Link Here
177
op=""
215
op=""
178
queue="default"
216
queue="default"
179
quiet="no"
217
quiet="no"
218
job_type=""
219
not_job_type=""
180
220
181
# Read command line parameters
221
# Read command line parameters
182
while [ $# -gt 0 ]; do
222
while [ $# -gt 0 ]; do
Lines 202-207 while [ $# -gt 0 ]; do Link Here
202
        --queue)
242
        --queue)
203
            queue="$2"
243
            queue="$2"
204
            shift 2 ;;
244
            shift 2 ;;
245
        --type)
246
            type="$2"
247
            shift 2 ;;
248
        --not-type)
249
            type="$2"
250
            shift 2 ;;
205
        -*)
251
        -*)
206
            die "Error: invalid option switch ($1)" ;;
252
            die "Error: invalid option switch ($1)" ;;
207
        *)
253
        *)
Lines 231-246 if [ $# -gt 0 ]; then Link Here
231
277
232
            case $op in
278
            case $op in
233
                "start")
279
                "start")
234
                    start_worker $name $queue
280
                    start_worker $name $queue $job_type $not_job_type
235
                    ;;
281
                    ;;
236
                "stop")
282
                "stop")
237
                    stop_worker $name $queue
283
                    stop_worker $name $queue $job_type $not_job_type
238
                    ;;
284
                    ;;
239
                "restart")
285
                "restart")
240
                    restart_worker $name $queue
286
                    restart_worker $name $queue $job_type $not_job_type
241
                    ;;
287
                    ;;
242
                "status")
288
                "status")
243
                    worker_status $name $queue
289
                    worker_status $name $queue $job_type $not_job_type
244
            esac
290
            esac
245
291
246
        else
292
        else
(-)a/misc/workers/background_jobs_worker.pl (-4 / +26 lines)
Lines 21-27 background_jobs_worker.pl - Worker script that will process background jobs Link Here
21
21
22
=head1 SYNOPSIS
22
=head1 SYNOPSIS
23
23
24
./background_jobs_worker.pl [--queue QUEUE] [-m|--max-processes MAX_PROCESSES]
24
./background_jobs_worker.pl [--queue QUEUE] [-m|--max-processes MAX_PROCESSES] [-t JOB_TYPE] [-n NOT_JOB_TYPE]
25
25
26
=head1 DESCRIPTION
26
=head1 DESCRIPTION
27
27
Lines 32-37 You can specify some queues only (using --queue, which is repeatable) if you wan Link Here
32
32
33
--m --max-processes specifies how many jobs to process simultaneously
33
--m --max-processes specifies how many jobs to process simultaneously
34
34
35
-t --type specifies what job type to process, repeatable
36
37
-n --not-type specifies what job type to skip processing, repeatable
38
35
Max processes will be set from the command line option, the environment variable MAX_PROCESSES, or the koha-conf file, in that order of precedence.
39
Max processes will be set from the command line option, the environment variable MAX_PROCESSES, or the koha-conf file, in that order of precedence.
36
By default the script will only run one job at a time.
40
By default the script will only run one job at a time.
37
41
Lines 67-73 use C4::Context; Link Here
67
71
68
$SIG{'PIPE'} = 'IGNORE';    # See BZ 35111; added to ignore PIPE error when connection lost on Ubuntu.
72
$SIG{'PIPE'} = 'IGNORE';    # See BZ 35111; added to ignore PIPE error when connection lost on Ubuntu.
69
73
70
my ( $help, @queues );
74
my ( $help, @queues, @job_types, @not_job_types );
71
75
72
my $max_processes = $ENV{MAX_PROCESSES};
76
my $max_processes = $ENV{MAX_PROCESSES};
73
$max_processes ||= C4::Context->config('background_jobs_worker')->{max_processes}
77
$max_processes ||= C4::Context->config('background_jobs_worker')->{max_processes}
Lines 82-87 GetOptions( Link Here
82
    'm|max-processes=i' => \$max_processes,
86
    'm|max-processes=i' => \$max_processes,
83
    'h|help'            => \$help,
87
    'h|help'            => \$help,
84
    'queue=s'           => \@queues,
88
    'queue=s'           => \@queues,
89
    't|type=s'          => \@job_types,
90
    'n|not-type=s'      => \@not_job_types,
85
) || pod2usage(1);
91
) || pod2usage(1);
86
92
87
pod2usage(0) if $help;
93
pod2usage(0) if $help;
Lines 174-179 while (1) { Link Here
174
            next;
180
            next;
175
        }
181
        }
176
182
183
        if ( $job && @job_types && !grep { $job->type } @job_types ) {
184
            $conn->nack( { frame => $frame, requeue => 'true' } );
185
            next;
186
        }
187
        if ( $job && @not_job_types && grep { $job->type } @not_job_types ) {
188
            $conn->nack( { frame => $frame, requeue => 'true' } );
189
            next;
190
        }
191
177
        unless ($job) {
192
        unless ($job) {
178
            $not_found_retries->{ $args->{job_id} } //= 0;
193
            $not_found_retries->{ $args->{job_id} } //= 0;
179
            if ( ++$not_found_retries->{ $args->{job_id} } >= $max_retries ) {
194
            if ( ++$not_found_retries->{ $args->{job_id} } >= $max_retries ) {
Lines 201-207 while (1) { Link Here
201
        $pm->finish;
216
        $pm->finish;
202
217
203
    } else {
218
    } else {
204
        my $jobs = Koha::BackgroundJobs->search( { status => 'new', queue => \@queues } );
219
        my $params = { status => 'new', queue => \@queues };
220
221
        if (@job_types) {
222
            $params->{type} = { '-in' => \@job_types };
223
        } elsif (@not_job_types) {
224
            $params->{type} = { '-not_in' => \@job_types };
225
        }
226
227
        my $jobs = Koha::BackgroundJobs->search($params);
205
        while ( my $job = $jobs->next ) {
228
        while ( my $job = $jobs->next ) {
206
            my $args = try {
229
            my $args = try {
207
                $job->json->decode( $job->data );
230
                $job->json->decode( $job->data );
208
- 

Return to bug 41245