-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync_processing_pool.py
executable file
·179 lines (148 loc) · 6.54 KB
/
rsync_processing_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
"""A working (and used!) example of using pyinotify to trigger data reduction.
A multiprocessing pool is used to perform the reduction in
an asynchronous and parallel fashion.
"""
import sys
import multiprocessing
import logging, logging.handlers
import os
import optparse
import shutil
import time
import pyinotify
import atexit
logger = logging.getLogger()
class options():
"""Dummy class serving as a placeholder for argparse handling."""
watch_dir = "test/rsync_to_here"
log_dir = "test/"
output_dir = "test/output"
nthreads = 4
class RsyncNewFileHandler(pyinotify.ProcessEvent):
"""Identifies new rsync'ed files and passes their path for processing.
rsync creates temporary files with a `.` prefix and random 6 letter suffix,
then renames these to the original filename when the transfer is complete.
To reliably catch (only) new transfers while coping with this file-shuffling,
we must do a little bit of tedious file tracking, using
the internal dict `tempfiles`.
Note we only track those files satisfying the condition
``file_predicate(basename)==True``.
"""
def my_init(self, nthreads, file_predicate, file_processor):
self.mask = pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO
self.tempfiles = {}
self.predicate = file_predicate
self.process = file_processor
def process_IN_CREATE(self, event):
original_filename = os.path.splitext(event.name[1:])[0]
if self.predicate(original_filename):
logger.debug("Transfer started, tempfile at:\n\t%s\n",
event.pathname)
self.tempfiles[original_filename] = event.pathname
def process_IN_MOVED_TO(self, event):
#Now rsync has renamed the file to drop the temporary suffix.
#NB event.name == basename(event.pathname) AFAICT
if event.name in self.tempfiles:
self.tempfiles.pop(event.name)
logger.info('Sending for processing: %s', event.pathname)
self.process(event.pathname)
def is_rawfile(filename):
"""Predicate function for identifying incoming AMI data"""
if '.raw' in filename:
return True
return False
def process_rawfile(file_path, output_dir):
"""A data reduction subroutine (specific to user's application)."""
try:
#Add a short sleep to demonstrate the parallel nature:
time.sleep(0.5)
#Call whatever external processing subroutine you want here
#We just copy the file in this trivial example.
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
shutil.copyfile(file_path,
os.path.join(output_dir, os.path.basename(file_path)))
# Should always catch *all* exception subclasses here.
# Otherwise, if an unexpected exception occurs during a pooled process,
# the file reduction will fail *silently*, and the only clue you'll get
# will be a missing 'job complete' message.
except Exception as e:
error_message = (
"Hit exception processing file: %s, exception reads:\n%s\n"
% (file_path, e))
return error_message
return "Successfully processed " + file_path
def processed_callback(summary):
"""Used to log the 'job complete' / error message in the master thread."""
logger.info('*** Job complete: ' + summary)
def main(options):
"""Define processing logic and fire up the watcher"""
watch_dir = options.watch_dir
pool = multiprocessing.Pool(options.nthreads)
def simply_process_rawfile(file_path):
"""
Wraps `process_rawfile` to take a single argument (file_path).
This is the trivial, single threaded version -
occasionally useful for debugging purposes.
"""
summary = process_rawfile(file_path, options.output_dir)
processed_callback(summary)
def asynchronously_process_rawfile(file_path):
"""
Wraps `process_rawfile` to take a single argument (file_path).
This version runs 'process_rawfile' asynchronously via the pool.
This provides parallel processing, at the cost of being harder to
debug if anything goes wrong (see notes on exception catching above)
"""
pool.apply_async(process_rawfile,
[file_path, options.output_dir],
callback=processed_callback)
handler = RsyncNewFileHandler(nthreads=options.nthreads,
file_predicate=is_rawfile,
file_processor=simply_process_rawfile
# file_processor=asynchronously_process_rawfile
)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch(options.watch_dir, handler.mask, rec=True)
log_preamble(options)
notifier.loop()
return 0
def log_preamble(options):
logger.info("***********")
logger.info('Watching %s', options.watch_dir)
logger.info('Output dir %s', options.output_dir)
logger.info('Log dir %s', options.log_dir)
logger.info("***********")
def setup_logging(options):
"""Set up basic (INFO level) and debug logfiles
These should list successful reductions, and any errors encountered.
We also copy the basic log to STDOUT, but it is expected that
the monitor script will be daemonised / run in a screen in the background.
"""
if not os.path.isdir(options.log_dir):
os.makedirs(options.log_dir)
log_filename = os.path.join(options.log_dir, 'autocruncher_log')
date_fmt = "%a %d %H:%M:%S"
std_formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s', date_fmt)
debug_formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s', date_fmt)
info_logfile = logging.handlers.RotatingFileHandler(log_filename,
maxBytes=5e5, backupCount=10)
info_logfile.setFormatter(std_formatter)
info_logfile.setLevel(logging.INFO)
debug_logfile = logging.handlers.RotatingFileHandler(log_filename + '.debug',
maxBytes=5e5, backupCount=10)
debug_logfile.setFormatter(debug_formatter)
debug_logfile.setLevel(logging.DEBUG)
log_stream = logging.StreamHandler()
log_stream.setFormatter(std_formatter)
log_stream.setLevel(logging.INFO)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(info_logfile)
logger.addHandler(log_stream)
logger.addHandler(debug_logfile)
if __name__ == '__main__':
setup_logging(options)
sys.exit(main(options))