-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
58 lines (47 loc) · 1.71 KB
/
worker.py
File metadata and controls
58 lines (47 loc) · 1.71 KB
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
from celery import Celery
from celery.schedules import crontab
import requests
from settings import Settings
import telegram_send
settings = Settings()
celery = Celery(broker=f"{settings.REDIS_URL}")
logger = celery.log.get_default_logger()
def send_to_telegram(arr: list):
"""
Send telegram notifications for clients email list
"""
for i in arr:
telegram_send.send(messages=[f"Client with name: {i['name']}, email: {i['email']} failed task."])
logger.info(f"User message for: {i['email']}")
return True
def get_clients_info(clients: set):
"""
Get name, email from the clients set ids
"""
black_list = []
for client in clients:
response = requests.get(f"https://jsonplaceholder.typicode.com/users/{client}").json()
data = {"name": response["name"], "email": response["email"]}
logger.info(f"User info: {data}")
black_list.append(data)
return black_list
@celery.task
def send_notifictions():
"""
Send notifications about clients with failed tasks
"""
logger.info(f"Start send notifications for failed tasks")
response = requests.get("https://jsonplaceholder.typicode.com/todos").json()
# falsed = tuple(client for client in response if client["completed"] == False)
falsed = [client["userId"] for client in response if client["completed"]== False]
clients = set(falsed)
clients_list = get_clients_info(clients)
send_telegram = send_to_telegram(clients_list)
logger.info(f"Done send notifications for failed tasks")
celery.conf.beat_schedule = {
"every-1-minute": {
"task": "worker.send_notifictions",
"schedule": crontab(minute='*/1'),
#"args": ("every 200 secs",),
}
}