-
Notifications
You must be signed in to change notification settings - Fork 6
/
event_server.py
183 lines (147 loc) · 5.7 KB
/
event_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2022 HITeC e.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import threading
import flask
import redis
import json
from werkzeug.serving import WSGIRequestHandler
from flask import jsonify, json, request, Response
import subprocess
import fcntl
import os
import signal
import psutil
__author__ = 'Benjamin Milde'
redis_server_channel = 'subtitle2go'
app = flask.Flask(__name__)
red = redis.StrictRedis(charset='utf-8', decode_responses=True)
long_poll_timeout = 0.5
long_poll_timeout_burst = 0.08
current_jobs = {}
def persistence_event_stream():
global current_jobs
print('Estabilishing persistence event_stream...')
pubsub = red.pubsub()
pubsub.subscribe(redis_server_channel)
for message in pubsub.listen():
msg = str(message['data'])
print('New msg:', msg)
if 'pid' in msg:
msg_json = json.loads(msg)
key = str(msg_json['pid']) + '_' + msg_json['file_id']
current_jobs[key] = msg_json
def event_stream():
print('New connection to event_stream!')
pubsub = red.pubsub()
pubsub.subscribe(redis_server_channel)
# yield b'hello'
for message in pubsub.listen():
if not message['type'] == 'subscribe':
#print('New message:', message)
#print(type(message['data']))
yield b'data: %s\n\n' % message['data']
@app.route('/status')
def status():
return jsonify(current_jobs)
@app.route('/status/<jobid>')
def status_with_id(jobid):
if jobid in current_jobs:
return jsonify(current_jobs[jobid])
else:
return jsonify({'error': 'could not find jobid in current jobs.'})
@app.route('/load')
def check_current_load():
max_parallel_processes = 60
subtitle2go_processes = 0
for p in psutil.process_iter():
if "subtitle2go.py" in "".join(p.cmdline()):
subtitle2go_processes += 1
response = dict()
response['current_processes'] = subtitle2go_processes
# true if free resources are available, false if not
response['takes_job'] = subtitle2go_processes < max_parallel_processes
return jsonify(response)
@app.route('/start', methods=['POST'])
def start():
request_data = request.get_json()
filename = request_data['filename']
language = request_data['language']
callback_url = request_data['url'] + "/" + request_data['id']
# calculate and return id
filenameS = filename.rpartition('.')[0] # Filename without file extension
filenameS_hash = hex(abs(hash(filenameS)))[2:]
# prepare logging
log_file=filename+"_"+request_data['id']+".log"
# run subtitle2go in background with the calculated id
with open(log_file,"w+") as out:
p = subprocess.Popen(["python","subtitle2go.py",filename,"-l",language,"--with-redis-updates","-i",filenameS_hash,"-c",callback_url],stdout=out,stderr=out)
id = str(p.pid) + '_' + filenameS_hash
return id
@app.route('/stop', methods=['POST'])
def stop():
request_data = request.get_json()
subtitle2goId = request_data['speech2TextId']
pid = int(subtitle2goId.split("_")[0]);
# kill the process if still running
killed = False
try:
os.kill(pid, signal.SIGKILL)
except Exception as e:
pass
else:
killed = True
del current_jobs[subtitle2goId]
return str(killed)
@app.route('/clear')
def clear_finished():
to_delete = []
for key in current_jobs:
if 'finished' in current_jobs[key]['status'] \
or 'failed' in current_jobs[key]['status'] :
to_delete.append(key)
for key in to_delete:
del current_jobs[key]
return 'ok'
#Event stream end point for the browser, connection is left open. Must be used with threaded Flask.
@app.route('/stream')
def stream():
return flask.Response(event_stream(), mimetype='text/event-stream')
#Traditional long polling. This is the fall back, if a browser does not support server side events.
@app.route('/stream_poll')
def poll():
pubsub = red.pubsub()
pubsub.subscribe(redis_server_channel)
message = pubsub.get_message(timeout=long_poll_timeout)
while(message != None):
yield message
message = pubsub.get_message(timeout=long_poll_timeout_burst)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Status update server for subtitle2go jobs')
parser.add_argument('-l', '--listen-host', default='127.0.0.1', dest='host', help='Host address to listen on.')
parser.add_argument('-p', '--port', default=7500, dest='port', help='Port to listen on.', type=int)
parser.add_argument('--debug', dest='debug', help='Start with debugging enabled', action='store_true', default=False)
args = parser.parse_args()
#print(' * Starting app with base path:',base_path)
if args.debug:
app.debug = True
persistence_event_stream_thread = threading.Thread(target=persistence_event_stream)
persistence_event_stream_thread.start()
print('Running as testing server.')
print('Host:', args.host)
print('Port:', args.port)
WSGIRequestHandler.protocol_version = 'HTTP/1.1'
app.run(host=args.host, port=args.port, threaded=True, use_reloader=False, use_debugger=False)