WIP - Stream workers output

This commit is contained in:
Alexis Métaireau 2016-06-18 11:13:22 +02:00
parent a45f56f23a
commit 3450be78fd
No known key found for this signature in database
GPG key ID: 1EDF5A7A485D4A11
8 changed files with 73 additions and 3 deletions

View file

@ -110,6 +110,7 @@ Nginx configuration
# Finally, send all non-media requests to the Pyramid server. # Finally, send all non-media requests to the Pyramid server.
location / { location / {
proxy_buffering off
uwsgi_pass zimit_upstream; uwsgi_pass zimit_upstream;
include /var/ideascube/uwsgi_params; include /var/ideascube/uwsgi_params;
} }

13
named.py Normal file
View file

@ -0,0 +1,13 @@
import os
import shlex
import subprocess
def spawn(cmd):
os.mkfifo("toto")
with open("toto", "w") as f:
process = subprocess.Popen(shlex.split(cmd), stdout=f)
process.wait()
os.unlink("toto")
spawn("stdbuf -o0 python test.py")

11
readpipe.py Normal file
View file

@ -0,0 +1,11 @@
from __future__ import print_function
import os
import os.path
def read_fifo(filename):
with open(filename) as fifo:
while os.path.exists(filename):
print(fifo.readline(), end='')
read_fifo("toto")

6
test.py Normal file
View file

@ -0,0 +1,6 @@
#! /use/bin/python
import time
for i in range(100):
time.sleep(0.2)
print i

View file

@ -14,9 +14,11 @@ pyramid.includes =
pyramid_mailer pyramid_mailer
[server:main] [server:main]
use = egg:waitress#main use = egg:gunicorn#main
host = 0.0.0.0 host = 0.0.0.0
port = 6543 port = 6543
send_bytes=1
worker_class = socketio.sgunicorn.GeventSocketIOWorker
# Begin logging configuration # Begin logging configuration

View file

@ -1,4 +1,5 @@
import os import os
import os.path
import shlex import shlex
import subprocess import subprocess
@ -13,3 +14,9 @@ def ensure_paths_exists(*paths):
if not os.path.exists(path): if not os.path.exists(path):
msg = '%s does not exist.' % path msg = '%s does not exist.' % path
raise OSError(msg) raise OSError(msg)
def read_fifo(filename):
with open(filename) as fifo:
while os.path.exists(filename):
yield fifo.readline()

View file

@ -1,8 +1,12 @@
from cornice import Service from cornice import Service
from colander import MappingSchema, SchemaNode, String from colander import MappingSchema, SchemaNode, String
from pyramid.response import Response
from zimit import utils
webpage = Service(name='website', path='/website')
home = Service(name='home', path='/') home = Service(name='home', path='/')
webpage = Service(name='website', path='/website')
logs = Service(name='home', path='/logs')
@home.get() @home.get()
@ -34,4 +38,18 @@ def crawl_new_website(request):
request.validated, request.validated,
timeout=1800) timeout=1800)
request.response.status_code = 201 request.response.status_code = 201
return {'success': True} return {
'success': True
}
@logs.get()
def get_logs(request):
stream_headers = [
('Content-Type', 'text/event-stream'),
('Cache-Control', 'no-cache')
]
return Response(
headerlist=stream_headers,
app_iter=utils.read_fifo("toto")
)

12
zimit/websocket.py Normal file
View file

@ -0,0 +1,12 @@
from socketio import namespace, socketio_manage
class LogsNamespace(namespace.BaseNamespace):
def on_read(self, filename):
self.emit('chat', "yeah")
def socketio_service(request):
socketio_manage(request.environ, {'/logs': LogsNamespace},
request)
return "out"