-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoolModule.py
155 lines (127 loc) · 5.31 KB
/
CoolModule.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
"""Create queue of tasks with PostrgeSQL and execute it.
Classes:
Pickler
Unpickler
"""
import psycopg2
import time
import logging
import threading
class Tasker():
""" Class to create a queqe of tasks with text document."""
def __init__(self, dbname, user, password, host, port,
periodicity=3, threaded=False):
self.periodicity = periodicity
self.dbname = dbname
self.user = user
self.password = password
self.host = host
self.port = port
self.tasking = False
self.threaded = threaded
self._thread = None
def init(self):
"""Initialization the database to storage the tasks."""
with psycopg2.connect(dbname=self.dbname, host=self.host, password=self.password, port=self.port, user=self.user) as conn:
cursor = conn.cursor()
cursor.execute('''create table if not exists task
(
id serial,
status integer not null default 0, -- 0 - новая, 1 - в работе, 2 - выполнена
adress text,
theme text,
message text
);
create index if not exists task__status__idx on task (status);''')
def createTask(self, data):
""" Create task from string """
with psycopg2.connect(dbname=self.dbname, host=self.host, password=self.password, port=self.port, user=self.user) as conn:
cursor = conn.cursor()
cursor.execute("insert into task (status,adress,theme,message) values (0,'{}','{}','{}') on conflict do nothing".format(
*data.split('/')))
def start(self, sourse):
""" Start to create tasks automaticlly """
self.tasking = True
def tasking():
for data in sourse:
if self.tasking:
self.createTask(data)
time.sleep(self.periodicity)
else:
break
if self.threaded:
self._thread = threading.Thread(target=tasking, daemon=True)
self._thread.start()
else:
try:
while True:
tasking()
except KeyboardInterrupt:
pass
def stop(self):
""" Stop to create tasks automaticlly """
self.tasking = False
class Worker():
""" Class to listen database and execute tasks """
def __init__(self, dbname, user, password, host, port, periodicity_start=5, periodicity_end=10):
self.periodicity_start = periodicity_start
self.periodicity_end = periodicity_end
self.dbname = dbname
self.user = user
self.password = password
self.host = host
self.port = port
self.activeListen = True
self._logger = logging.getLogger("Worker")
self._logger.setLevel(logging.INFO)
fh = logging.FileHandler("mail_logs.log", encoding='utf-8')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
fh.setFormatter(formatter)
self._logger.addHandler(fh)
self._logger.info("Program started")
def listen(self, threaded=False):
""" Functon to read database and creating of tasks queue. """
self.activeListen = True
def check():
with psycopg2.connect(dbname=self.dbname, host=self.host, password=self.password, port=self.port, user=self.user) as conn:
cursor = conn.cursor()
while self.activeListen:
cursor.execute('''with next_task as (
select id,task.adress,task.theme,task.message from task
where status = 0
limit 1
for update skip locked
)
update task
set
status = 1
from next_task
where task.id = next_task.id
returning task.id,task.adress,task.theme,task.message; ''')
conn.commit()
self.execute(cursor.fetchall())
time.sleep(4)
if threaded:
threading.Thread(target=check, daemon=True).start()
else:
try:
check()
except KeyboardInterrupt:
self._logger.info('Stoping...')
def stop(self):
""" Stop reading database """
self.activeListen = False
def execute(self, task):
"""Function to execute tasks."""
if task:
task = task[0]
self._logger.info('Send {}:{} to {}'.format(
task[2], task[1], task[3]))
with psycopg2.connect(dbname=self.dbname, host=self.host, password=self.password, port=self.port, user=self.user) as conn:
cursor = conn.cursor()
cursor.execute('''update task
set
status = 2
where task.id = {} '''.format(task[0]))
else:
time.sleep(5)