Mercurial > piecrust2
annotate piecrust/serving/procloop.py @ 565:ff714d7f074d
serve: Fix error reporting when the background pipeline fails.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 09 Oct 2015 16:49:12 -0700 |
parents | cc6f3dbe3048 |
children | 7dabfdd056a1 |
rev | line source |
---|---|
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import os.path |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 import time |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import json |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import queue |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 import logging |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
7 import itertools |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 import threading |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
9 from piecrust.app import PieCrust |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
10 from piecrust.processing.pipeline import ProcessorPipeline |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 logger = logging.getLogger(__name__) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
15 # This flag is for cancelling all long running requests like SSEs. |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
16 server_shutdown = False |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
18 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
19 class PipelineStatusServerSentEventProducer(object): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
20 """ The producer for Server-Sent Events (SSE) notifying the front-end |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
21 about useful things like assets having been re-processed in the |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
22 background. |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
23 Each has its own queue because the user could have multiple pages |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
24 open, each having to display notifications coming from the server. |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
25 """ |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
26 def __init__(self, proc_loop): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
27 self._proc_loop = proc_loop |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
28 self._queue = queue.Queue() |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 self._start_time = 0 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
30 self._poll_interval = 0.5 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
31 self._ping_interval = 30 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
32 self._time_between_pings = 0 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
33 self._running = 0 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
34 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
35 def addBuildEvent(self, item): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
36 self._queue.put_nowait(item) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
37 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 def run(self): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 logger.debug("Starting pipeline status SSE.") |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
40 self._proc_loop.addObserver(self) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 self._start_time = time.time() |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
42 self._running = 1 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
43 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 outstr = 'event: ping\ndata: started\n\n' |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 yield bytes(outstr, 'utf8') |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
47 while self._running == 1 and not server_shutdown: |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 try: |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
49 # We use a short poll interval (less than a second) because |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
50 # we need to catch `server_shutdown` going `True` as soon as |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
51 # possible to exit this thread when the user hits `CTRL+C`. |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
52 data = self._queue.get(True, self._poll_interval) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 except queue.Empty: |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
54 # Not exact timing but close enough. |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
55 self._time_between_pings += self._poll_interval |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
56 if self._time_between_pings >= self._ping_interval: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
57 self._time_between_pings = 0 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
58 logger.debug("Sending ping/heartbeat event.") |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
59 outstr = 'event: ping\ndata: 1\n\n' |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
60 yield bytes(outstr, 'utf8') |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
61 continue |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
62 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
63 logger.debug("Sending pipeline status SSE.") |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
64 outstr = (('event: %s\n' % data['type']) + |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
65 ('id: %s\n' % data['id']) + |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
66 ('data: %s\n\n' % json.dumps(data))) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
67 self._queue.task_done() |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
68 yield bytes(outstr, 'utf8') |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 def close(self): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
71 logger.debug("Closing pipeline status SSE.") |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
72 self._proc_loop.removeObserver(self) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
73 self._running = 2 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
74 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
75 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
76 class ProcessingLoop(threading.Thread): |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
77 def __init__(self, root_dir, out_dir, sub_cache_dir=None, debug=False): |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
78 super(ProcessingLoop, self).__init__( |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
79 name='pipeline-reloader', daemon=True) |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
80 # TODO: re-create the app when `config.yml` is changed. |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
81 self.app = PieCrust(root_dir=root_dir, debug=debug) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
82 if sub_cache_dir: |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
83 self.app._useSubCacheDir(sub_cache_dir) |
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
84 self.pipeline = ProcessorPipeline(self.app, out_dir) |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
85 self.last_status_id = 0 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
86 self.interval = 1 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
87 self._paths = set() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
88 self._record = None |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
89 self._last_bake = 0 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
90 self._obs = [] |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
91 self._obs_lock = threading.Lock() |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
92 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
93 def addObserver(self, obs): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
94 with self._obs_lock: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
95 self._obs.append(obs) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
96 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
97 def removeObserver(self, obs): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
98 with self._obs_lock: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
99 self._obs.remove(obs) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
100 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
101 def run(self): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
102 # Build the first list of known files and run the pipeline once. |
553
cc6f3dbe3048
serve: Extract some of the server's functionality into WSGI middlewares.
Ludovic Chabant <ludovic@chabant.com>
parents:
552
diff
changeset
|
103 roots = [os.path.join(self.app.root_dir, r) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
104 for r in self.pipeline.mounts.keys()] |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
105 for root in roots: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
106 for dirpath, dirnames, filenames in os.walk(root): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
107 self._paths |= set([os.path.join(dirpath, f) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
108 for f in filenames]) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 self._last_bake = time.time() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 self._record = self.pipeline.run() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 while True: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
113 for root in roots: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
114 # For each mount root we try to find the first new or |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
115 # modified file. If any, we just run the pipeline on |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
116 # that mount. |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
117 found_new_or_modified = False |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
118 for dirpath, dirnames, filenames in os.walk(root): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
119 for filename in filenames: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
120 path = os.path.join(dirpath, filename) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 if path not in self._paths: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 logger.debug("Found new asset: %s" % path) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
123 self._paths.add(path) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
124 found_new_or_modified = True |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
125 break |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 if os.path.getmtime(path) > self._last_bake: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 logger.debug("Found modified asset: %s" % path) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 found_new_or_modified = True |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 break |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 if found_new_or_modified: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 break |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 if found_new_or_modified: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 self._runPipeline(root) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 time.sleep(self.interval) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 def _runPipeline(self, root): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 self._last_bake = time.time() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 try: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
142 self._record = self.pipeline.run( |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
143 root, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 previous_record=self._record, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 save_record=False) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
147 status_id = self.last_status_id + 1 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
148 self.last_status_id += 1 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
150 if self._record.success: |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
151 changed = filter( |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
152 lambda i: not i.was_collapsed_from_last_run, |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
153 self._record.entries) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
154 changed = itertools.chain.from_iterable( |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
155 map(lambda i: i.rel_outputs, changed)) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
156 changed = list(changed) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
157 item = { |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
158 'id': status_id, |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
159 'type': 'pipeline_success', |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
160 'assets': changed} |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
161 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
162 self._notifyObservers(item) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
163 else: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
164 item = { |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
165 'id': status_id, |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
166 'type': 'pipeline_error', |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
167 'assets': []} |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
168 for entry in self._record.entries: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
169 if entry.errors: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
170 asset_item = { |
565
ff714d7f074d
serve: Fix error reporting when the background pipeline fails.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
171 'path': entry.path, |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
172 'errors': list(entry.errors)} |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
173 item['assets'].append(asset_item) |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
174 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
175 self._notifyObservers(item) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
176 except Exception as ex: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
177 logger.exception(ex) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
178 |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
179 def _notifyObservers(self, item): |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
180 with self._obs_lock: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
181 observers = list(self._obs) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
182 for obs in observers: |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
183 obs.addBuildEvent(item) |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
184 |