From 9ee6ed67da94603791b3e81c855fd7de2b1336e7 Mon Sep 17 00:00:00 2001 From: Duda Nogueira Date: Thu, 29 Sep 2022 10:45:18 -0300 Subject: [PATCH] [TASKS] Add more tasks --- rocket_connect/instance/models.py | 75 +++++++++++++++++-- rocket_connect/instance/tasks.py | 38 +++++----- rocket_connect/instance/views.py | 28 +++++-- .../templates/instance/new_server.html | 2 +- rocket_connect/templates/pages/home.html | 4 +- 5 files changed, 115 insertions(+), 32 deletions(-) diff --git a/rocket_connect/instance/models.py b/rocket_connect/instance/models.py index 7162b56..dc66cd6 100644 --- a/rocket_connect/instance/models.py +++ b/rocket_connect/instance/models.py @@ -158,7 +158,9 @@ def install_server_tasks(self): all the server tasks for a server, disabled by default """ added_tasks = [] - # server_maintenance + # + # T1 server_maintenance + # task = PeriodicTask.objects.filter( task="instance.tasks.server_maintenance", kwargs__contains=self.external_token, @@ -174,7 +176,9 @@ def install_server_tasks(self): ) self.tasks.add(task) added_tasks.append(task) - # alert open + # + # T2 alert_last_message_open_chat + # task = PeriodicTask.objects.filter( task="instance.tasks.alert_last_message_open_chat", kwargs__contains=self.external_token, @@ -203,7 +207,9 @@ def install_server_tasks(self): ) self.tasks.add(task) added_tasks.append(task) - # generic webhook + # + # T3 alert_open_rooms_generic_webhook + # task = PeriodicTask.objects.filter( task="instance.tasks.alert_open_rooms_generic_webhook", kwargs__contains=self.external_token, @@ -224,7 +230,9 @@ def install_server_tasks(self): ) self.tasks.add(task) added_tasks.append(task) - # status change + # + # T4 change_user_status + # task = PeriodicTask.objects.filter( task="instance.tasks.change_user_status", kwargs__contains=self.external_token, @@ -247,12 +255,65 @@ def install_server_tasks(self): ) self.tasks.add(task) added_tasks.append(task) + # + # T5 close_abandoned_chats + # + task = PeriodicTask.objects.filter( + task="instance.tasks.close_abandoned_chats", + kwargs__contains=self.external_token, + ) + if not task.exists(): + crontab = CrontabSchedule.objects.first() + task = PeriodicTask.objects.create( + enabled=False, + name=f"Close Abandoned Chats for {self.name} (ID {self.id})", + description="close all open rooms that the last message from last_message_users " + + "with more then last_message_seconds. before, send a closing_message.", + crontab=crontab, + task="instance.tasks.close_abandoned_chats", + kwargs=json.dumps( + { + "server_token": self.external_token, + "last_message_users": "bot,otherbot", + "last_message_seconds": "600", + "closing_message": "Due to inactivity, your chat is being closed.", + } + ), + ) + self.tasks.add(task) + added_tasks.append(task) + # + # T6 alert_undelivered_messages + # + task = PeriodicTask.objects.filter( + task="instance.tasks.alert_undelivered_messages", + kwargs__contains=self.external_token, + ) + if not task.exists(): + crontab = CrontabSchedule.objects.first() + task = PeriodicTask.objects.create( + enabled=False, + name=f"Alert Undelivered Messages for {self.name} (ID {self.id})", + description="""Alert about Undelivered messages""", + crontab=crontab, + task="instance.tasks.alert_undelivered_messages", + kwargs=json.dumps( + { + "server_token": self.external_token, + "notification_target": "general,otherchannel", + "notification_template": "Found {{undelivered_messages.count}} undelivered messages", + } + ), + ) + self.tasks.add(task) + added_tasks.append(task) # return added tasks return added_tasks def install_omnichannel_webhook( self, rocketconnect_url="http://rocketconnect:5000" ): + output = [] rocket = self.get_rocket_client() configs = [ [ @@ -275,7 +336,9 @@ def install_omnichannel_webhook( ["Livechat_Routing_Method", "Manual_Selection"], ] for config in configs: - rocket.settings_update(config[0], config[1]) + r = rocket.settings_update(config[0], config[1]) + output.append(r) + return output def add_default_wppconnect(self, name="WPPCONNECT"): random = random_string(size=5) @@ -293,7 +356,7 @@ def add_default_wppconnect(self, name="WPPCONNECT"): "enable_ack_receipt": True, } connector.config = config - connector.save() + return connector.save() uuid = models.UUIDField(default=uuid.uuid4, editable=False) owners = models.ManyToManyField( diff --git a/rocket_connect/instance/tasks.py b/rocket_connect/instance/tasks.py index 79cb91a..18e2fa5 100644 --- a/rocket_connect/instance/tasks.py +++ b/rocket_connect/instance/tasks.py @@ -23,6 +23,22 @@ def intake_unread_messages(connector_id): return unread +# T1 +@celery_app.task( + retry_kwargs={"max_retries": 7, "countdown": 5}, + autoretry_for=(requests.ConnectionError,), +) +def server_maintenance(server_token): + """do all sorts of server maintenance""" + server = Server.objects.get(external_token=server_token) + response = {} + # sync room + response["room_sync"] = server.room_sync(execute=True) + # return results + return response + + +# T2 @celery_app.task( retry_kwargs={"max_retries": 7, "countdown": 5}, autoretry_for=(requests.ConnectionError,), @@ -96,20 +112,7 @@ def alert_last_message_open_chat( } -@celery_app.task( - retry_kwargs={"max_retries": 7, "countdown": 5}, - autoretry_for=(requests.ConnectionError,), -) -def server_maintenance(server_token): - """do all sorts of server maintenance""" - server = Server.objects.get(external_token=server_token) - response = {} - # sync room - response["room_sync"] = server.room_sync(execute=True) - # return results - return response - - +# T3 @celery_app.task( retry_kwargs={"max_retries": 7, "countdown": 5}, autoretry_for=(requests.ConnectionError,), @@ -128,6 +131,7 @@ def alert_open_rooms_generic_webhook(server_token, endpoint): return response.ok +# T4 @celery_app.task( retry_kwargs={"max_retries": 7, "countdown": 5}, autoretry_for=(requests.ConnectionError,), @@ -149,6 +153,7 @@ def change_user_status(server_token, users, status, message=""): return responses +# T5 @celery_app.task( retry_kwargs={"max_retries": 7, "countdown": 5}, autoretry_for=(requests.ConnectionError,), @@ -206,6 +211,7 @@ def close_abandoned_chats( } +# T6 @celery_app.task( retry_kwargs={"max_retries": 7, "countdown": 5}, autoretry_for=(requests.ConnectionError,), @@ -214,9 +220,7 @@ def alert_undelivered_messages( server_token, notification_target, notification_template ): """ - - get all undelivered messages from server - - render notification template - - send to notification targets + Alert about Undelivered messages """ server = Server.objects.get(external_token=server_token) rocket = server.get_rocket_client() diff --git a/rocket_connect/instance/views.py b/rocket_connect/instance/views.py index 3463963..e3d0d79 100644 --- a/rocket_connect/instance/views.py +++ b/rocket_connect/instance/views.py @@ -1,5 +1,6 @@ import datetime import json +import uuid import pytz from django.conf import settings @@ -355,6 +356,7 @@ def new_server(request): form = NewServerForm(request.POST or None) form.fields["admin_user_id"].required = True form.fields["url"].initial = "http://rocketchat:3000" + form.fields["secret_token"].initial = str(uuid.uuid4()).upper() form.fields["admin_user_token"].required = True if form.is_valid(): server = form.save(commit=False) @@ -365,15 +367,27 @@ def new_server(request): server.save() server.owners.add(request.user) messages.success(request, "Server Created!") + aditional_tasks = {} # add omnichannel - if request.GET.get("install_omnichannel_webhooks"): - server.install_omnichannel() + if request.POST.get("install_omnichannel_webhooks"): + i = server.install_omnichannel_webhook() + aditional_tasks["install_omnichannel_webhooks"] = i # add default wppconnect - if request.GET.get("install_default_wppconnect"): - server.install_default_wppconnect() - if request.GET.get("add_default_server_tasks"): - server.install_server_tasks() - + if request.POST.get("install_default_wppconnect"): + i = server.install_default_wppconnect() + aditional_tasks["install_default_wppconnect"] = i + # add default tasks + if request.POST.get("add_default_server_tasks"): + i = server.install_server_tasks() + aditional_tasks["add_default_server_tasks"] = i + + if aditional_tasks: + messages.success( + request, + "Additional tasks after server creation: {}".format( + aditional_tasks + ), + ) return redirect( reverse("instance:server_detail", args=[server.external_token]) ) diff --git a/rocket_connect/templates/instance/new_server.html b/rocket_connect/templates/instance/new_server.html index ebb844c..4d1687e 100644 --- a/rocket_connect/templates/instance/new_server.html +++ b/rocket_connect/templates/instance/new_server.html @@ -25,7 +25,7 @@