Implement task timeout

This commit is contained in:
RunasSudo 2019-03-06 12:59:42 +11:00
parent 3097092ae5
commit fd6f6bc4b1
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -1,5 +1,5 @@
# Eos - Verifiable elections # Eos - Verifiable elections
# Copyright © 2017-18 RunasSudo (Yingtong Li) # Copyright © 2017-2019 RunasSudo (Yingtong Li)
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -31,12 +31,15 @@ class TaskStatus(EosEnum):
class Task(TopLevelObject): class Task(TopLevelObject):
label = 'Unknown task' label = 'Unknown task'
_ver = StringField(default='0.8')
_id = UUIDField() _id = UUIDField()
run_strategy = EmbeddedObjectField() run_strategy = EmbeddedObjectField()
run_at = DateTimeField() run_at = DateTimeField()
timeout = IntField(default=3600) # seconds
started_at = DateTimeField() started_at = DateTimeField()
completed_at = DateTimeField() completed_at = DateTimeField()
@ -113,6 +116,16 @@ class TaskScheduler:
@staticmethod @staticmethod
def tick(): def tick():
now = DateTimeField.now()
for task in TaskScheduler.pending_tasks(): for task in TaskScheduler.pending_tasks():
if task.run_at and task.run_at < DateTimeField.now(): if task.run_at and task.run_at < now:
task.run() task.run()
for task in TaskScheduler.active_tasks():
if task.timeout and (now - task.started_at).total_seconds() > task.timeout:
task.status = TaskStatus.TIMEOUT
task.completed_at = DateTimeField.now()
task.messages.append('Elapsed time exceeded timeout')
task.save()
task.error()