comparison piecrust/serving/procloop.py @ 553:cc6f3dbe3048

serve: Extract some of the server's functionality into WSGI middlewares.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 08 Aug 2015 22:01:47 -0700
parents 9612cfc6455a
children ff714d7f074d
comparison
equal deleted inserted replaced
552:9612cfc6455a 553:cc6f3dbe3048
4 import json 4 import json
5 import queue 5 import queue
6 import logging 6 import logging
7 import itertools 7 import itertools
8 import threading 8 import threading
9 from piecrust.app import PieCrust
10 from piecrust.processing.pipeline import ProcessorPipeline
9 11
10 12
11 logger = logging.getLogger(__name__) 13 logger = logging.getLogger(__name__)
12 14
13 # This flag is for cancelling all long running requests like SSEs. 15 # This flag is for cancelling all long running requests like SSEs.
70 self._proc_loop.removeObserver(self) 72 self._proc_loop.removeObserver(self)
71 self._running = 2 73 self._running = 2
72 74
73 75
74 class ProcessingLoop(threading.Thread): 76 class ProcessingLoop(threading.Thread):
75 def __init__(self, pipeline): 77 def __init__(self, root_dir, out_dir, sub_cache_dir=None, debug=False):
76 super(ProcessingLoop, self).__init__( 78 super(ProcessingLoop, self).__init__(
77 name='pipeline-reloader', daemon=True) 79 name='pipeline-reloader', daemon=True)
78 self.pipeline = pipeline 80 # TODO: re-create the app when `config.yml` is changed.
81 self.app = PieCrust(root_dir=root_dir, debug=debug)
82 if sub_cache_dir:
83 self.app._useSubCacheDir(sub_cache_dir)
84 self.pipeline = ProcessorPipeline(self.app, out_dir)
79 self.last_status_id = 0 85 self.last_status_id = 0
80 self.interval = 1 86 self.interval = 1
81 self._paths = set() 87 self._paths = set()
82 self._record = None 88 self._record = None
83 self._last_bake = 0 89 self._last_bake = 0
92 with self._obs_lock: 98 with self._obs_lock:
93 self._obs.remove(obs) 99 self._obs.remove(obs)
94 100
95 def run(self): 101 def run(self):
96 # Build the first list of known files and run the pipeline once. 102 # Build the first list of known files and run the pipeline once.
97 app = self.pipeline.app 103 roots = [os.path.join(self.app.root_dir, r)
98 roots = [os.path.join(app.root_dir, r)
99 for r in self.pipeline.mounts.keys()] 104 for r in self.pipeline.mounts.keys()]
100 for root in roots: 105 for root in roots:
101 for dirpath, dirnames, filenames in os.walk(root): 106 for dirpath, dirnames, filenames in os.walk(root):
102 self._paths |= set([os.path.join(dirpath, f) 107 self._paths |= set([os.path.join(dirpath, f)
103 for f in filenames]) 108 for f in filenames])