Mercurial > piecrust2
annotate piecrust/baking/worker.py @ 656:dba53f0f7671
admin: run an asset processing loop in the background.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Mon, 22 Feb 2016 22:44:39 -0800 |
| parents | 27e3b3f05648 |
| children | 3ceeca7bb71c |
| rev | line source |
|---|---|
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import time |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import logging |
|
466
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
3 from piecrust.app import PieCrust, apply_variant_and_values |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
4 from piecrust.baking.records import BakeRecord, _get_transition_key |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 from piecrust.baking.single import PageBaker, BakingError |
|
455
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
6 from piecrust.environment import AbortedSourceUseError |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 from piecrust.rendering import ( |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 QualifiedPage, PageRenderingContext, render_page_segments) |
|
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
9 from piecrust.routing import create_route_metadata |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 from piecrust.sources.base import PageFactory |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
11 from piecrust.workerpool import IWorker |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 logger = logging.getLogger(__name__) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 class BakeWorkerContext(object): |
|
425
afeebdd9f767
bake: Pass the sub-cache directory to the bake workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
422
diff
changeset
|
18 def __init__(self, root_dir, sub_cache_dir, out_dir, |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
19 previous_record_path=None, |
|
466
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
20 config_variant=None, config_values=None, |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
21 force=False, debug=False): |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 self.root_dir = root_dir |
|
425
afeebdd9f767
bake: Pass the sub-cache directory to the bake workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
422
diff
changeset
|
23 self.sub_cache_dir = sub_cache_dir |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 self.out_dir = out_dir |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
25 self.previous_record_path = previous_record_path |
|
466
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
26 self.config_variant = config_variant |
|
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
27 self.config_values = config_values |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 self.force = force |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 self.debug = debug |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
30 self.app = None |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
31 self.previous_record = None |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
32 self.previous_record_index = None |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
33 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
34 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
35 class BakeWorker(IWorker): |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
36 def __init__(self, ctx): |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
37 self.ctx = ctx |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
38 self.work_start_time = time.perf_counter() |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
39 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
40 def initialize(self): |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
41 # Create the app local to this worker. |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
42 app = PieCrust(self.ctx.root_dir, debug=self.ctx.debug) |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
43 app._useSubCacheDir(self.ctx.sub_cache_dir) |
|
471
5b57a189fd98
bug: Correctly setup the environment/app for bake workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
466
diff
changeset
|
44 app.config.set('baker/is_baking', True) |
|
476
27e3b3f05648
bake: Set the worker ID in the configuration. It's useful.
Ludovic Chabant <ludovic@chabant.com>
parents:
471
diff
changeset
|
45 app.config.set('baker/worker_id', self.wid) |
|
471
5b57a189fd98
bug: Correctly setup the environment/app for bake workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
466
diff
changeset
|
46 app.env.base_asset_url_format = '%uri%' |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
47 app.env.fs_cache_only_for_main_page = True |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
48 app.env.registerTimer("BakeWorker_%d_Total" % self.wid) |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
49 app.env.registerTimer("BakeWorkerInit") |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
50 app.env.registerTimer("JobReceive") |
|
466
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
51 apply_variant_and_values(app, self.ctx.config_variant, |
|
456db44dcc53
bake: Pass the config variants and values from the CLI to the baker.
Ludovic Chabant <ludovic@chabant.com>
parents:
455
diff
changeset
|
52 self.ctx.config_values) |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
53 self.ctx.app = app |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
54 |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
55 # Load previous record |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
56 if self.ctx.previous_record_path: |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
57 self.ctx.previous_record = BakeRecord.load( |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
58 self.ctx.previous_record_path) |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
59 self.ctx.previous_record_index = {} |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
60 for e in self.ctx.previous_record.entries: |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
61 key = _get_transition_key(e.path, e.taxonomy_info) |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
62 self.ctx.previous_record_index[key] = e |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
63 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
64 # Create the job handlers. |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
65 job_handlers = { |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
66 JOB_LOAD: LoadJobHandler(self.ctx), |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
67 JOB_RENDER_FIRST: RenderFirstSubJobHandler(self.ctx), |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
68 JOB_BAKE: BakeJobHandler(self.ctx)} |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
69 for jt, jh in job_handlers.items(): |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
70 app.env.registerTimer(type(jh).__name__) |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
71 self.job_handlers = job_handlers |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
72 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
73 app.env.stepTimerSince("BakeWorkerInit", self.work_start_time) |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
74 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
75 def process(self, job): |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
76 handler = self.job_handlers[job['type']] |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
77 with self.ctx.app.env.timerScope(type(handler).__name__): |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
78 return handler.handleJob(job['job']) |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
79 |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
80 def getReport(self): |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
81 self.ctx.app.env.stepTimerSince("BakeWorker_%d_Total" % self.wid, |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
82 self.work_start_time) |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
83 return { |
|
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
84 'type': 'timers', |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
85 'data': self.ctx.app.env._timers} |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 JOB_LOAD, JOB_RENDER_FIRST, JOB_BAKE = range(0, 3) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
90 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
91 class JobHandler(object): |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
92 def __init__(self, ctx): |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
93 self.ctx = ctx |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
94 |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
95 @property |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
96 def app(self): |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
97 return self.ctx.app |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
98 |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
99 def handleJob(self, job): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 raise NotImplementedError() |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
103 def _get_errors(ex): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 errors = [] |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 while ex is not None: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 errors.append(str(ex)) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 ex = ex.__cause__ |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 return errors |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
111 def save_factory(fac): |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
112 return { |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
113 'source_name': fac.source.name, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
114 'rel_path': fac.rel_path, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
115 'metadata': fac.metadata} |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
118 def load_factory(app, info): |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
119 source = app.getSource(info['source_name']) |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
120 return PageFactory(source, info['rel_path'], info['metadata']) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 class LoadJobHandler(JobHandler): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 def handleJob(self, job): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 # Just make sure the page has been cached. |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
126 fac = load_factory(self.app, job) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 logger.debug("Loading page: %s" % fac.ref_spec) |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
128 result = { |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
129 'source_name': fac.source.name, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
130 'path': fac.path, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
131 'config': None, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
132 'errors': None} |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 try: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 page = fac.buildPage() |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 page._load() |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
136 result['config'] = page.config.getAll() |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 except Exception as ex: |
|
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
138 logger.debug("Got loading error. Sending it to master.") |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
139 result['errors'] = _get_errors(ex) |
|
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
140 if self.ctx.debug: |
|
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
141 logger.exception(ex) |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
142 return result |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 class RenderFirstSubJobHandler(JobHandler): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 def handleJob(self, job): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 # Render the segments for the first sub-page of this page. |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
148 fac = load_factory(self.app, job) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 # These things should be OK as they're checked upstream by the baker. |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
151 route = self.app.getRoute(fac.source.name, fac.metadata, |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
152 skip_taxonomies=True) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
153 assert route is not None |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
154 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
155 page = fac.buildPage() |
|
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
156 route_metadata = create_route_metadata(page) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 qp = QualifiedPage(page, route, route_metadata) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
158 ctx = PageRenderingContext(qp) |
|
455
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
159 self.app.env.abort_source_use = True |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
160 |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
161 result = { |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
162 'path': fac.path, |
|
455
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
163 'aborted': False, |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
164 'errors': None} |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
165 logger.debug("Preparing page: %s" % fac.ref_spec) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 try: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 render_page_segments(ctx) |
|
455
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
168 except AbortedSourceUseError: |
|
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
169 logger.debug("Page %s was aborted." % fac.ref_spec) |
|
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
170 result['aborted'] = True |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
171 except Exception as ex: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 logger.debug("Got rendering error. Sending it to master.") |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
173 result['errors'] = _get_errors(ex) |
|
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
174 if self.ctx.debug: |
|
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
175 logger.exception(ex) |
|
455
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
176 finally: |
|
cb3446be44b7
bake: Abort "render first" jobs if we start using other pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
453
diff
changeset
|
177 self.app.env.abort_source_use = False |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
178 return result |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
179 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
180 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
181 class BakeJobHandler(JobHandler): |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
182 def __init__(self, ctx): |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
183 super(BakeJobHandler, self).__init__(ctx) |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
184 self.page_baker = PageBaker(ctx.app, ctx.out_dir, ctx.force) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
185 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
186 def handleJob(self, job): |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
187 # Actually bake the page and all its sub-pages to the output folder. |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
188 fac = load_factory(self.app, job['factory_info']) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
189 |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
190 route_metadata = job['route_metadata'] |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
191 tax_info = job['taxonomy_info'] |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
192 if tax_info is not None: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
193 route = self.app.getTaxonomyRoute(tax_info.taxonomy_name, |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
194 tax_info.source_name) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
195 else: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
196 route = self.app.getRoute(fac.source.name, route_metadata, |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
197 skip_taxonomies=True) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
198 assert route is not None |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
199 |
|
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
200 page = fac.buildPage() |
|
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
201 qp = QualifiedPage(page, route, route_metadata) |
|
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
202 |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
203 result = { |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
204 'path': fac.path, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
205 'taxonomy_info': tax_info, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
206 'sub_entries': None, |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
207 'errors': None} |
|
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
208 dirty_source_names = job['dirty_source_names'] |
|
453
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
209 |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
210 previous_entry = None |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
211 if self.ctx.previous_record_index is not None: |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
212 key = _get_transition_key(fac.path, tax_info) |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
213 previous_entry = self.ctx.previous_record_index.get(key) |
|
8351a77e13f5
bake: Don't pass the previous record entries to the workers.
Ludovic Chabant <ludovic@chabant.com>
parents:
451
diff
changeset
|
214 |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
215 logger.debug("Baking page: %s" % fac.ref_spec) |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
216 try: |
|
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
217 sub_entries = self.page_baker.bake( |
|
430
21e26ed867b6
internal: Create full route metadata in one place.
Ludovic Chabant <ludovic@chabant.com>
parents:
426
diff
changeset
|
218 qp, previous_entry, dirty_source_names, tax_info) |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
219 result['sub_entries'] = sub_entries |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
220 |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
221 except BakingError as ex: |
|
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
222 logger.debug("Got baking error. Sending it to master.") |
|
451
838f3964f400
bake: Optimize the bake by not using custom classes for passing info.
Ludovic Chabant <ludovic@chabant.com>
parents:
447
diff
changeset
|
223 result['errors'] = _get_errors(ex) |
|
415
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
224 if self.ctx.debug: |
|
0e9a94b7fdfa
bake: Improve bake record information.
Ludovic Chabant <ludovic@chabant.com>
parents:
411
diff
changeset
|
225 logger.exception(ex) |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
226 |
|
447
aefe70229fdd
bake: Commonize worker pool code between html and asset baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
441
diff
changeset
|
227 return result |
|
411
e7b865f8f335
bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
228 |
