Skip to content

Commit

Permalink
[TASKS] Add more tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanogueira committed Sep 29, 2022
1 parent 0817043 commit 9ee6ed6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 32 deletions.
75 changes: 69 additions & 6 deletions rocket_connect/instance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 = [
[
Expand All @@ -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)
Expand All @@ -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(
Expand Down
38 changes: 21 additions & 17 deletions rocket_connect/instance/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,),
Expand Down Expand Up @@ -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,),
Expand All @@ -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,),
Expand All @@ -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,),
Expand Down Expand Up @@ -206,6 +211,7 @@ def close_abandoned_chats(
}


# T6
@celery_app.task(
retry_kwargs={"max_retries": 7, "countdown": 5},
autoretry_for=(requests.ConnectionError,),
Expand All @@ -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()
Expand Down
28 changes: 21 additions & 7 deletions rocket_connect/instance/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import uuid

import pytz
from django.conf import settings
Expand Down Expand Up @@ -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)
Expand All @@ -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])
)
Expand Down
2 changes: 1 addition & 1 deletion rocket_connect/templates/instance/new_server.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<label class="form-check-label">
<input type="checkbox"
class="form-check-input"
name="add_default_wppconnect"
name="install_default_wppconnect"
id=""
value="1"
checked>
Expand Down
4 changes: 3 additions & 1 deletion rocket_connect/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ <h1>{{server}}</h1>
</div>
<div class="card-body">
<p class="card-text">
<a name="" id="" class="btn btn-primary" href="{% url 'instance:server_detail' server.external_token %}" role="button">{{server.connectors.count}} Connectors</a>
<a name="" id="" class="btn btn-primary" href="{% url 'instance:server_detail' server.external_token %}" role="button">
{{server.connectors.count}} Connectors, {{server.tasks.count}} Tasks
</a>
</p>
</div>
</div>
Expand Down

0 comments on commit 9ee6ed6

Please sign in to comment.