comparison piecrust/baking/worker.py @ 666:81d9c3a3a0b5

internal: Get rid of the whole "sub cache" business. * Compute cache keys up front, so the cache directory is only chosen once. * Buffer up config variants to apply before loading the config. Makes it possible to cache variant-resulting configs, too. * Make a factory class to reuse the logic that creates the `PieCrust` object correctly for multi-process workers and such. * Add a test.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 03 Mar 2016 08:22:41 -0800
parents 3ceeca7bb71c
children 61d606fbc313
comparison
equal deleted inserted replaced
665:5dc13c816045 666:81d9c3a3a0b5
13 13
14 logger = logging.getLogger(__name__) 14 logger = logging.getLogger(__name__)
15 15
16 16
17 class BakeWorkerContext(object): 17 class BakeWorkerContext(object):
18 def __init__(self, root_dir, sub_cache_dir, out_dir, *, 18 def __init__(self, appfactory, out_dir, *,
19 previous_record_path=None, 19 force=False, previous_record_path=None):
20 config_variant=None, config_values=None, 20 self.appfactory = appfactory
21 force=False, debug=False, theme_site=False):
22 self.root_dir = root_dir
23 self.sub_cache_dir = sub_cache_dir
24 self.out_dir = out_dir 21 self.out_dir = out_dir
22 self.force = force
25 self.previous_record_path = previous_record_path 23 self.previous_record_path = previous_record_path
26 self.config_variant = config_variant
27 self.config_values = config_values
28 self.force = force
29 self.debug = debug
30 self.theme_site = theme_site
31 self.app = None 24 self.app = None
32 self.previous_record = None 25 self.previous_record = None
33 self.previous_record_index = None 26 self.previous_record_index = None
34 27
35 28
38 self.ctx = ctx 31 self.ctx = ctx
39 self.work_start_time = time.perf_counter() 32 self.work_start_time = time.perf_counter()
40 33
41 def initialize(self): 34 def initialize(self):
42 # Create the app local to this worker. 35 # Create the app local to this worker.
43 app = PieCrust(self.ctx.root_dir, debug=self.ctx.debug, 36 app = self.ctx.appfactory.create()
44 theme_site=self.ctx.theme_site)
45 app._useSubCacheDir(self.ctx.sub_cache_dir)
46 app.config.set('baker/is_baking', True) 37 app.config.set('baker/is_baking', True)
47 app.config.set('baker/worker_id', self.wid) 38 app.config.set('baker/worker_id', self.wid)
48 app.env.base_asset_url_format = '%uri%' 39 app.env.base_asset_url_format = '%uri%'
49 app.env.fs_cache_only_for_main_page = True 40 app.env.fs_cache_only_for_main_page = True
50 app.env.registerTimer("BakeWorker_%d_Total" % self.wid) 41 app.env.registerTimer("BakeWorker_%d_Total" % self.wid)
51 app.env.registerTimer("BakeWorkerInit") 42 app.env.registerTimer("BakeWorkerInit")
52 app.env.registerTimer("JobReceive") 43 app.env.registerTimer("JobReceive")
53 apply_variant_and_values(app, self.ctx.config_variant,
54 self.ctx.config_values)
55 self.ctx.app = app 44 self.ctx.app = app
56 45
57 # Load previous record 46 # Load previous record
58 if self.ctx.previous_record_path: 47 if self.ctx.previous_record_path:
59 self.ctx.previous_record = BakeRecord.load( 48 self.ctx.previous_record = BakeRecord.load(
137 page._load() 126 page._load()
138 result['config'] = page.config.getAll() 127 result['config'] = page.config.getAll()
139 except Exception as ex: 128 except Exception as ex:
140 logger.debug("Got loading error. Sending it to master.") 129 logger.debug("Got loading error. Sending it to master.")
141 result['errors'] = _get_errors(ex) 130 result['errors'] = _get_errors(ex)
142 if self.ctx.debug: 131 if self.ctx.app.debug:
143 logger.exception(ex) 132 logger.exception(ex)
144 return result 133 return result
145 134
146 135
147 class RenderFirstSubJobHandler(JobHandler): 136 class RenderFirstSubJobHandler(JobHandler):
221 result['sub_entries'] = sub_entries 210 result['sub_entries'] = sub_entries
222 211
223 except BakingError as ex: 212 except BakingError as ex:
224 logger.debug("Got baking error. Sending it to master.") 213 logger.debug("Got baking error. Sending it to master.")
225 result['errors'] = _get_errors(ex) 214 result['errors'] = _get_errors(ex)
226 if self.ctx.debug: 215 if self.ctx.app.debug:
227 logger.exception(ex) 216 logger.exception(ex)
228 217
229 return result 218 return result
230 219