-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskMaster_proactive_interface.py
69 lines (60 loc) · 2.08 KB
/
taskMaster_proactive_interface.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
from json.decoder import JSONDecodeError
import redis
from redis import Redis
import logging
import random
import time
import json
import sys
from typing import Final
HOST : Final = "localhost" #or the address of the Redis instance
PORT : Final = 6379 #port
DB : Final = 0
MASTER_NAME : Final = f"master{random.randint(0, 1e4)}_"
TODO_TASK_LIST : Final = "todo_tasks"
DONE_TASK_LIST : Final = "done_tasks"
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
pool = redis.ConnectionPool(host=HOST, port=PORT)
taskTypes = ["task+", "task-", "task*", "task/"]
only_sum = False
#taskTypes = ["task+"]
task_counter = 0
agent_set = set()
class Task:
def __init__(self):
self.task_type = taskTypes[random.randint(0,len(taskTypes)-1)]
self.task_name = f"{MASTER_NAME}{self.task_type}_{task_counter}"
self.a = random.randint(1, 100)
self.b = random.randint(1, 100)
self.result = 0
self.lock = "lock_"+self.task_name # lo imposto come identificativo del task
def __repr__(self) -> str:
return self.task_name
def write_stat_agents() -> None:
r = redis.Redis(connection_pool=pool)
f = open(f"masters_stats_auth/stats.txt", "w+") #se voglio le stat die task per ogni master, devo creare i dati nel master
string = ""
for e in agent_set:
string += f'{e} has accomplished {r.get(e).decode("utf-8")} tasks\n'
f.write(string)
f.close()
def show_task_lists(r:Redis) -> None:
f = open(f'proactive_stats/stats.txt', "w+")
todo = []
todo = r.lrange(name=TODO_TASK_LIST, start=0, end=-1)
done = []
done = r.lrange(name=DONE_TASK_LIST, start=0, end=-1)
string = f'To-do list:\n'
for i in todo:
string += f'{i}\n'
string += f'\nDone list:\n'
for i in done:
string += f'"{i.decode("utf-8")}" , svolta da: "{r.hget(i, "executedBy").decode("utf-8")}"\n'
f.write(string)
f.close()
if __name__=='__main__':
r = redis.Redis(connection_pool=pool)
while True:
show_task_lists(r)
time.sleep(1)