-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_centre.py
384 lines (329 loc) · 12.9 KB
/
job_centre.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import os
import json
os.environ["PYTHONASYNCIODEBUG"] = "1"
import logging
# import jsonschema
# from jsonschema.exceptions import ValidationError
import asyncio
import heapq
import fastjsonschema
from typing import Any, Dict, Set, Tuple
import coloredlogs
HOST = "0.0.0.0"
PORT = 8000
logger = logging.getLogger(name="job")
coloredlogs.install(
level="CRITICAL",
# logger=logger, # Enable this to only pass logs from this logger instance
milliseconds=True,
fmt="%(asctime)s %(name)-8s [%(levelname)-9s] %(funcName)-8s %(message)s",
)
put_schema = {
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"title": "PUT request",
"description": "A client has sent a request for a job to be added",
"properties": {
"request": {"type": "string"},
"queue": {"type": "string"},
"job": {"type": "object"},
"pri": {"type": "integer", "minimum": 0},
},
"required": ["request", "queue", "job", "pri"],
"additionalProperties": False,
}
get_schema = {
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"title": "GET request",
"description": "A client has requested a job from the specified queues",
"properties": {
"request": {"type": "string"},
"queues": {"type": "array", "items": {"type": "string"}, "minItems": 1},
"wait": {"type": "boolean"},
},
"required": ["request", "queues"],
"additionalProperties": False,
}
abort_delete_schema = {
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"title": "ABORT/DELETE request",
"description": "A client has requested a job to be removed or aborted",
"properties": {
"request": {"type": "string", "enum": ["abort", "delete"]},
"id": {"type": "integer", "minimum": 0},
},
"required": ["request", "id"],
"additionalProperties": False,
}
request_schema = {
"type": "object",
"title": "Request",
"description": "Can be any request type",
"properties": {
"request": {"type": "string", "enum": ["get", "put", "abort", "delete"]},
},
"required": ["request"],
}
put_schema_validator = fastjsonschema.compile(put_schema)
get_schema_validator = fastjsonschema.compile(get_schema)
abort_delete_schema_validator = fastjsonschema.compile(abort_delete_schema)
request_schema_validator = fastjsonschema.compile(request_schema)
class Job:
def __init__(
self, job_id: int, job: Dict[Any, Any], priority: int, queue: str
) -> None:
self.job_id = job_id
self.job = job
self.queue = queue
self.priority = priority
self.running = False
self.deleted = False
def __str__(self) -> str:
return f"< Job {self.job_id} of {self.queue} [{self.priority}]>"
def __repr__(self) -> str:
return self.__str__()
def __lt__(self, other):
"""
Returns true if the priority of this job is higher than other
It is defined for less than(<) so that these objects behave can be
added to the heap to make it a MaxHeap
"""
return self.priority > other.priority
class JobManager:
"""
Manages jobs and their queues
"""
def __init__(self) -> None:
# Associates a queue name (string) with a heap(implemented as a list)
self.queues: Dict[str, list[Job]] = dict()
# Dictionary of jobs, job_id: job
self.jobs: Dict[int, Job] = dict()
# This counter starts from one and is incremented after a job is added
self.job_id_counter = -1
def _create_job(self, job: Dict[Any, Any], priority: int, queue: str) -> Job:
self.job_id_counter += 1
return Job(self.job_id_counter, job, priority, queue)
def put(self, queue_name: str, job_dict: Dict[Any, Any], priority: int) -> Job:
"""
Puts the job on the specified queue and returns the job object
"""
if queue_name not in self.queues:
# Create an empty job queue
self.queues[queue_name] = []
job = self._create_job(job_dict, priority, queue_name)
heapq.heappush(self.queues[queue_name], job)
self.jobs[job.job_id] = job
return job
def get(self, queues_list: list[str]):
"""
Returns the job with the highest priority among all the
given queues. If no job is found, None is returned
"""
if queues_list is None:
return None
highest_priority: int = -1
highest_priority_job: Job | None = None
for queue in queues_list:
# Check if the queue exists and is not empty
if queue in self.queues and self.queues[queue]:
# Find the element with highest priority in this queue
job = heapq.heappop(self.queues[queue])
flag = False
# Loop until the first non deleted job is found
while job.deleted:
if not self.queues[queue]: # The queue is empty
# del self.queues[queue]
flag = True
break
job = heapq.heappop(self.queues[queue])
if flag:
continue
if job.priority > highest_priority:
highest_priority = job.priority
highest_priority_job = job
# Push the job back into the queue
heapq.heappush(self.queues[queue], job)
if highest_priority_job is None:
return None
return heapq.heappop(self.queues[highest_priority_job.queue])
def is_valid(self, job_id: int):
"""
Returns True if this is a valid job id
"""
# Check if the job has been deleted
if job_id not in self.jobs:
return False
# Check if the job id has not been allocated
if job_id > self.job_id_counter:
return False
return True
def delete(self, job_id: int):
"""
Returns True if the delete is valid
False otherwise
"""
if not self.is_valid(job_id):
return False
self.jobs[job_id].deleted = True
del self.jobs[job_id]
return True
def abort(self, job_id: int):
if not self.is_valid(job_id):
return False
job = self.jobs[job_id]
job.running = False
# Put the job back in its queue
if job.queue not in self.queues:
# Create an empty job queue
self.queues[job.queue] = []
heapq.heappush(self.queues[job.queue], job)
return True
class Server:
def __init__(self) -> None:
# Dictionary of a client address with the jobs it has requested using GET
self.clients: Dict[str, set] = dict()
self.job_manager = JobManager()
async def handle_put(
self, address: str, writer: asyncio.StreamWriter, instance: Dict[Any, Any]
):
put_schema_validator(instance) # type: ignore
job = self.job_manager.put(instance["queue"], instance["job"], instance["pri"])
writer.write(b'{"status":"ok","id":%d}\n' % job.job_id)
async def handle_get(
self, address: str, writer: asyncio.StreamWriter, instance: Dict[Any, Any]
):
get_schema_validator(instance) #type: ignore
job = self.job_manager.get(instance["queues"])
if job is not None:
self.clients[address].add(job.job_id)
job_as_json = json.dumps(job.job).encode("utf-8")
writer.write(
b'{"status":"ok","id":%d,"job":%s,"pri":%d,"queue":"%s"}\n'
% (job.job_id, job_as_json, job.priority, job.queue.encode("utf-8"))
)
return
if instance.get('wait', False):
# Crude solution, poll every 1 second to see if a new job has been added
# A better way is to keep a list of waiters, and when a new job is inserted
# If a waiter is present, send it to the waiter instead of adding it to the
# queue
while job is None:
job = self.job_manager.get(instance["queues"])
await asyncio.sleep(0.5)
self.clients[address].add(job.job_id)
job_as_json = json.dumps(job.job).encode("utf-8")
writer.write(
b'{"status":"ok","id":%d,"job":%s,"pri":%d,"queue":"%s"}\n'
% (job.job_id, job_as_json, job.priority, job.queue.encode("utf-8"))
)
else:
writer.write(b'{"status":"no-job"}\n')
async def handle_delete(
self, address: str, writer: asyncio.StreamWriter, instance: Dict[Any, Any]
):
abort_delete_schema_validator(instance) #type: ignore
status = self.job_manager.delete(instance["id"])
if not status:
writer.write(b'{"status":"no-job"}\n')
else:
writer.write(b'{"status":"ok"}\n')
async def handle_abort(
self, address: str, writer: asyncio.StreamWriter, instance: Dict[Any, Any]
):
abort_delete_schema_validator(instance) #type: ignore
# Check if this is a valid job id
if not self.job_manager.is_valid(instance["id"]):
writer.write(b'{"status":"no-job"}\n')
return
if instance["id"] not in self.clients[address]:
writer.write(
b'{"status": "error", "error": "Only the client which requested this job can abort it"}\n'
)
return
status = self.job_manager.abort(instance["id"])
if not status:
writer.write(b'{"status":"no-job"}\n')
else:
writer.write(b'{"status":"ok"}\n')
async def accept_client(
self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
):
address = ""
try:
address = writer.get_extra_info("peername")
address = f"{address[0]}:{address[1]}"
logger.info("%s connected", address)
self.clients[address] = set()
while True:
if not (await self.process_client(address, reader, writer)):
break
except:
logger.exception("%s error occured while handling client", address)
finally:
await self.close_client(address, writer)
async def process_client(
self, address: str, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
):
data = await reader.readline()
if not data:
logger.info("%s disconnected", address)
return False
logger.info("%s sent %s", address, data)
# Load and validate the data
try:
instance = json.loads(data)
request_schema_validator(instance) #type: ignore
# Instance is a valid request and has a request field
if instance["request"] == "get":
await self.handle_get(address, writer, instance)
elif instance["request"] == "put":
await self.handle_put(address, writer, instance)
elif instance["request"] == "delete":
await self.handle_delete(address, writer, instance)
else:
await self.handle_abort(address, writer, instance)
except fastjsonschema.JsonSchemaValueException as e:
logger.warning("%s invalid request: %s", address, e.message)
writer.write(
b'{"status": "error", "error": "%s"}\n' % e.message.encode("utf-8")
)
except json.JSONDecodeError as e:
logger.warning("%s illformated json: %s", address, e.msg)
writer.write(
b'{"status": "error", "error": "%s"}\n' % e.msg.encode("utf-8")
)
except Exception as e:
logger.critical("%s unhandled exception", address, exc_info=1) # type: ignore
writer.write(b'{"status": "error", "error": "unhandled exception"}\n')
await writer.drain()
return True
async def close_client(self, address: str, writer: asyncio.StreamWriter):
try:
logger.info("%s clearing all jobs of this client", address)
# When the client disconnects, abort all running jobs of this client
for job in self.clients[address]:
self.job_manager.abort(job)
del self.clients[address]
except:
pass
try:
writer.close()
await writer.wait_closed()
except:
pass
async def main():
loop = asyncio.get_running_loop()
# Set asyncio debug
loop.set_debug(True)
logging.getLogger("asyncio").setLevel(logging.DEBUG)
logger.info("This is from logger")
job_server = Server()
server = await asyncio.start_server(job_server.accept_client, host=HOST, port=PORT)
logger.info("Started server on %s:%d", HOST, PORT)
async with server:
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
# Execute with pypy3 to avoid timeout in the big test