1 # Copyright (C) 2007-2023 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <https://www.gnu.org/licenses/>.
18 """Interface for runners."""
20 from public
import public
21 from zope
.interface
import Attribute
, Interface
25 class RunnerCrashEvent
:
26 """Triggered when a runner encounters an exception in _dispose()."""
28 def __init__(self
, runner
, mlist
, msg
, metadata
, error
):
30 self
.mailing_list
= mlist
32 self
.metadata
= metadata
37 class RunnerInterrupt(Exception):
38 """A runner received a system call interrupting signal.
40 PEP 475 automatically, and at the C layer, retries system calls such as
41 time.sleep(). This can mean runners with long sleeps in their _snooze()
42 method won't actually exit. This exception is always raised in Mailman's
43 runner signal handlers to prevent this behavior. Runners that implement
44 their own .run() method must be prepared to handle this, usually by
50 class IRunner(Interface
):
54 """Start the runner."""
57 """Stop the runner on the next iteration through the loop."""
59 is_queue_runner
= Attribute("""\
60 A boolean variable describing whether the runner is a queue runner.
63 queue_directory
= Attribute(
64 'The queue directory. Overridden in subclasses.')
66 sleep_time
= Attribute("""\
67 The number of seconds this runner will sleep between iterations
68 through the main loop.
72 """Set up the signal handlers necessary to control the runner.
74 The runner should catch the following signals:
75 - SIGTERM and SIGINT: treated exactly the same, they cause the runner
76 to exit with no restart from the master.
77 - SIGUSR1: Also causes the runner to exit, but the master watcher will
79 - SIGHUP: Re-open the log files.
83 """The work done in one iteration of the main loop.
85 Can be overridden by subclasses.
87 :return: The number of files still left to process.
91 def _process_one_file(msg
, msgdata
):
92 """Process one queue file.
94 :param msg: The message object.
95 :type msg: `email.message.Message`
96 :param msgdata: The message metadata.
101 """Clean up upon exit from the main processing loop.
103 Called when the runner's main loop is stopped, this should perform any
104 necessary resource deallocation.
107 def _dispose(mlist
, msg
, msgdata
):
108 """Dispose of a single message destined for a mailing list.
110 Called for each message that the runner is responsible for, this is
111 the primary overridable method for processing each message.
112 Subclasses, must provide implementation for this method.
114 :param mlist: The mailing list this message is destined for.
115 :type mlist: `IMailingList`
116 :param msg: The message being processed.
117 :type msg: `email.message.Message`
118 :param msgdata: The message metadata.
120 :return: True if the message should continue to be queued, False if
121 the message should be deleted automatically.
126 """Do some arbitrary periodic processing.
128 Called every once in a while both from the runner's main loop, and
129 from the runner's hash slice processing loop. You can do whatever
130 special periodic processing you want here.
133 def _snooze(filecnt
):
134 """Sleep for a little while.
136 :param filecnt: The number of messages in the queue the last time
137 through. Runners can decide to continue to do work, or sleep for
138 a while based on this value. By default, the base runner only
139 snoozes when there was nothing to do last time around.
143 def _short_circuit():
144 """Should processing be short-circuited?
146 :return: True if the file processing loop should exit before it's
147 finished processing each message in the current slice of hash
148 space. False tells _one_iteration() to continue processing until
149 the current snapshot of hash space is exhausted.