From c3e5cec54867d0ac5c080419fa030619149d7a50 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 08:09:54 -0700 Subject: [PATCH 01/18] update requirements, allowing version to be figured out --- requirements.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 57c52af..44380d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -aiohttp==3.7.2 +aiohttp async-timeout==3.0.1 asyncpg==0.21.0 attrs==20.2.0 @@ -7,14 +7,14 @@ chardet==3.0.4 cycler==0.10.0 dateparser==0.7.4 dblpy==0.3.4 -discord.py==1.5.1 +discord.py==1.7.3 idna==2.10 idna-ssl==1.1.0 kiwisolver==1.3.0 matplotlib==3.2.1 motor==2.3.0 -multidict==5.0.0 -numpy==1.19.2 +multidict +numpy pymongo==3.11.0 pyparsing==2.4.7 python-dateutil==2.8.1 From 665b41ca1ef7af95bfd23faa5a42bfd78ee40f0a Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 08:35:32 -0700 Subject: [PATCH 02/18] add start script --- start.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 start.sh diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..a53f235 --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/bin/bash +python ipc.py & +python launcher.py \ No newline at end of file From 301fd87b72036666a960b2ba65014bb1db124e5c Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 08:41:24 -0700 Subject: [PATCH 03/18] lessen version requirements for async-timeout, dblpy, discord.py --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 44380d7..089af38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,13 @@ aiohttp -async-timeout==3.0.1 +async-timeout asyncpg==0.21.0 attrs==20.2.0 certifi==2020.6.20 chardet==3.0.4 cycler==0.10.0 dateparser==0.7.4 -dblpy==0.3.4 -discord.py==1.7.3 +dblpy +discord.py idna==2.10 idna-ssl==1.1.0 kiwisolver==1.3.0 From d80768aacd4ef1bd65d19af42400899fd39d0fb0 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 09:06:55 -0700 Subject: [PATCH 04/18] revision secrets, use env vars --- .gitignore | 3 --- essentials/secrets.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 essentials/secrets.py diff --git a/.gitignore b/.gitignore index af3fefe..d3cab67 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,6 @@ # PY VENV venv/ -# Tokens and Passwwords -essentials/secrets\.py - *.log # export diff --git a/essentials/secrets.py b/essentials/secrets.py new file mode 100644 index 0000000..92a9b2a --- /dev/null +++ b/essentials/secrets.py @@ -0,0 +1,10 @@ +import os + +class Secrets: + def __init__(self): + self.dbl_token = '' # DBL token (only needed for public bot) + self.mongo_db = os.environ.get('PM_DB') + self.bot_token = os.environ.get('PM_BOT_TOKEN') + self.mode = 'development' # or production + +SECRETS = Secrets() \ No newline at end of file From eef746a41d88846ab67897afdc2454eafef6cfc7 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 09:32:06 -0700 Subject: [PATCH 05/18] run launcher in bg to avoid Render timeout --- start.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/start.sh b/start.sh index a53f235..a7c587c 100755 --- a/start.sh +++ b/start.sh @@ -1,3 +1,4 @@ #!/bin/bash python ipc.py & -python launcher.py \ No newline at end of file +python launcher.py & +echo 'Donezo' \ No newline at end of file From 98c066305fc1dcb0f6144c159f84204238519610 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 09:41:36 -0700 Subject: [PATCH 06/18] try again to avoid render timeout with exit 0 --- start.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/start.sh b/start.sh index a7c587c..e597462 100755 --- a/start.sh +++ b/start.sh @@ -1,4 +1,4 @@ #!/bin/bash python ipc.py & -python launcher.py & -echo 'Donezo' \ No newline at end of file +python launcher.py +exit 0 \ No newline at end of file From db4f7243ff2e45ee1d85628e2f78bd2b97bcee08 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 10:05:28 -0700 Subject: [PATCH 07/18] small expressjs app to return 200 for health check to avoid timeout --- .gitignore | 4 +++- index.js | 15 +++++++++++++++ package.json | 22 ++++++++++++++++++++++ start.sh | 4 ++-- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore index d3cab67..84c5097 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ export/*.txt *.pyc #backup -backup/ \ No newline at end of file +backup/ + +node_modules/ \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..3507e1b --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +const express = require('express') +const app = express() +const port = 3000 + +app.get('/health-hack', (req, res) => { + res.sendStatus(200) +}) + +app.post('/health-hack', (req, res) => { + res.sendStatus(200) +}) + +app.listen(port, () => { + console.log(`Health hack listening on port ${port}`) +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..2379344 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "pollmaster-health-check", + "version": "1.0.0", + "description": "Just returns 200 for Render", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/starryzone/pollmaster.git" + }, + "author": "Mike Purvis", + "license": "MIT", + "bugs": { + "url": "https://github.com/starryzone/pollmaster/issues" + }, + "homepage": "https://github.com/starryzone/pollmaster#readme", + "dependencies": { + "express": "^4.17.3" + } +} diff --git a/start.sh b/start.sh index e597462..ed2a099 100755 --- a/start.sh +++ b/start.sh @@ -1,4 +1,4 @@ #!/bin/bash +node index.js & python ipc.py & -python launcher.py -exit 0 \ No newline at end of file +python launcher.py \ No newline at end of file From 02edd2dc06f462c610fa40bfdf203861533b49b0 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 10:29:30 -0700 Subject: [PATCH 08/18] debugging messages, attempt to have 1 cluster --- launcher.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/launcher.py b/launcher.py index f6671f3..b841589 100644 --- a/launcher.py +++ b/launcher.py @@ -45,6 +45,7 @@ def __init__(self, loop): self.init = time.perf_counter() def get_shard_count(self): + print(f"Settings.mode is {SETTINGS.mode}") if SETTINGS.mode == "development": return 1 data = requests.get('https://discordapp.com/api/v7/gateway/bot', headers={ @@ -81,8 +82,11 @@ def task_complete(self, task): async def startup(self): shards = list(range(self.get_shard_count())) - size = [shards[x:x + 4] for x in range(0, len(shards), 4)] + print(f"Shards: {shards}") + # size = [shards[x:x + 4] for x in range(0, len(shards), 4)] + size = [shards[x:x + 1] for x in range(0, len(shards), 1)] log.info(f"Preparing {len(size)} clusters") + print(f"Preparing {len(size)} clusters") for shard_ids in size: self.cluster_queue.append(Cluster(self, next(NAMES), shard_ids, len(shards))) From 93116727f0efa3aa6100018282fd932aaa336685 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 10:53:42 -0700 Subject: [PATCH 09/18] try killing all pids for "python launcher.py" in start script --- start.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/start.sh b/start.sh index ed2a099..1363101 100755 --- a/start.sh +++ b/start.sh @@ -1,4 +1,5 @@ #!/bin/bash +ps -ef | grep "python launcher.py" | grep -v grep | awk '{print $2}' | xargs kill node index.js & python ipc.py & python launcher.py \ No newline at end of file From 1d67e38c862bbd906cc3a827ab7f7d31548f8a22 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 11:06:39 -0700 Subject: [PATCH 10/18] tack 9 baby --- start.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.sh b/start.sh index 1363101..e0a1598 100755 --- a/start.sh +++ b/start.sh @@ -1,5 +1,5 @@ #!/bin/bash -ps -ef | grep "python launcher.py" | grep -v grep | awk '{print $2}' | xargs kill +ps -ef | grep "python launcher.py" | grep -v grep | awk '{print $2}' | xargs kill -9 node index.js & python ipc.py & python launcher.py \ No newline at end of file From 8b0c78ffb3251421ebf9f136f0eb72b740e02f5e Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 13:35:33 -0700 Subject: [PATCH 11/18] pin discord.py version to 1.5.1 --- requirements.txt | 2 +- start.sh | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 089af38..150327e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ chardet==3.0.4 cycler==0.10.0 dateparser==0.7.4 dblpy -discord.py +discord.py==1.5.1 idna==2.10 idna-ssl==1.1.0 kiwisolver==1.3.0 diff --git a/start.sh b/start.sh index e0a1598..b583846 100755 --- a/start.sh +++ b/start.sh @@ -1,5 +1,4 @@ #!/bin/bash -ps -ef | grep "python launcher.py" | grep -v grep | awk '{print $2}' | xargs kill -9 +#ps -ef | grep "python launcher.py" | grep -v grep | awk '{print $2}' | xargs kill -9 node index.js & python ipc.py & -python launcher.py \ No newline at end of file From e4f4f0ed36cc7d0f188748682e2d46ada4b09dd2 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 14:05:23 -0700 Subject: [PATCH 12/18] =?UTF-8?q?add=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cogs/help.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/help.py b/cogs/help.py index c368dd8..2e3c6b8 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -47,7 +47,7 @@ def get_help_embed(self, page, pre): title = f' Pollmaster Help - React with an emoji to learn more about a topic!' embed = discord.Embed(title='', description='', colour=SETTINGS.color) embed.set_author(name=title, icon_url=SETTINGS.author_icon) - embed.set_footer(text='Use reactions to navigate the help. This message will self-destruct in 5 minutes.') + embed.set_footer(text='Use reactions to navigate the help. This message will self-destruct in 5 minutes…') if page == '🏠': # POLL CREATION SHORT From b2f6f866c3807e0ee3ad54b5f4fe30b894fdfa20 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 15:22:21 -0700 Subject: [PATCH 13/18] hack to kill the former process for Render, heheh --- launcher.py | 9 ++++++--- requirements.txt | 1 + utils/misc.py | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/launcher.py b/launcher.py index b841589..212a87a 100644 --- a/launcher.py +++ b/launcher.py @@ -11,6 +11,7 @@ from bot import ClusterBot from essentials.multi_server import get_pre from essentials.settings import SETTINGS +from utils.misc import proceed_to_party TOKEN = SETTINGS.bot_token @@ -130,7 +131,7 @@ async def start_cluster(self): if self.cluster_queue: cluster = self.cluster_queue.pop(0) log.info(f"Starting Cluster#{cluster.name}") - await cluster.start() + await cluster.start(force=True) log.info("Done!") self.clusters.append(cluster) await self.start_cluster() @@ -198,5 +199,7 @@ def stop(self, sign=signal.SIGINT): if __name__ == "__main__": - loop = asyncio.get_event_loop() - Launcher(loop).start() + print("in main") + if proceed_to_party(): + loop = asyncio.get_event_loop() + Launcher(loop).start() diff --git a/requirements.txt b/requirements.txt index 150327e..ef8605a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,6 +15,7 @@ matplotlib==3.2.1 motor==2.3.0 multidict numpy +psutil pymongo==3.11.0 pyparsing==2.4.7 python-dateutil==2.8.1 diff --git a/utils/misc.py b/utils/misc.py index 26f9ba1..d2adfbd 100644 --- a/utils/misc.py +++ b/utils/misc.py @@ -1,4 +1,6 @@ import argparse +import os +import psutil import pytz import datetime as dt @@ -50,4 +52,24 @@ def possible_timezones(tz_offset, common_only=True): if desired_delta == non_dst_offset[0]: results.append(tz_name) - return results \ No newline at end of file + return results + +def proceed_to_party(): + current_pid = os.getpid() + # Loop through all processes + for proc in psutil.process_iter(): + # print(f"aloha pids and names {proc.pid} {proc.name()}") + try: + # Check if process name contains the given name string. + if "python" in proc.name(): + print(f"aloha found it {proc.pid} {proc.children()}") + # this returns the list of opened files by the current process + flist = proc.open_files() + if flist: + print(f"alohaz {proc.pid} {proc.name}") + for nt in flist: + if "launcher" in nt.path.lower() and proc.pid != current_pid: + proc.terminate() + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + pass + return True \ No newline at end of file From 0f2d856cf3533b27dbbd28cdc3dfb10d0c995ef9 Mon Sep 17 00:00:00 2001 From: Mike Purvis Date: Tue, 29 Mar 2022 16:44:30 -0700 Subject: [PATCH 14/18] just a hack --- cogs/poll_controls.py | 41 ++++++++++++++++++++++++++++++++++++++++- launcher.py | 8 +++++--- pollmaster.py | 10 +++++++++- utils/misc.py | 23 ++++++++++++++--------- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/cogs/poll_controls.py b/cogs/poll_controls.py index a0b18e1..dab1cbd 100644 --- a/cogs/poll_controls.py +++ b/cogs/poll_controls.py @@ -15,7 +15,7 @@ from essentials.multi_server import get_server_pre, ask_for_server, ask_for_channel from essentials.settings import SETTINGS from models.poll import Poll -from utils.misc import CustomFormatter +from utils.misc import CustomFormatter, proceed_to_party from utils.paginator import embed_list_paginated from utils.poll_name_generator import generate_word @@ -40,6 +40,8 @@ def cog_unload(self): # noinspection PyCallingNonCallable @tasks.loop(seconds=30) async def close_activate_polls(self): + if not proceed_to_party(): + return if hasattr(self.bot, 'db') and hasattr(self.bot.db, 'polls'): utc_now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) @@ -111,11 +113,15 @@ async def close_activate_polls(self): @close_activate_polls.before_loop async def before_close_activate_polls(self): + if not proceed_to_party(): + return # print('close task waiting...') await self.bot.wait_until_ready() @tasks.loop(seconds=5) async def refresh_queue(self): + if not proceed_to_party(): + return remove_list = [] for pid, t in self.bot.refresh_blocked.items(): if t - time.time() < 0: @@ -135,12 +141,16 @@ async def refresh_queue(self): @refresh_queue.before_loop async def before_refresh_queue(self): + if not proceed_to_party(): + return # print('refresh task waiting...') await self.bot.wait_until_ready() # General Methods @staticmethod def get_label(message: discord.Message): + if not proceed_to_party(): + return label = None if message and message.embeds: embed = message.embeds[0] @@ -193,6 +203,8 @@ async def say_embed(self, ctx, say_text='', title='Pollmaster', footer_text=None @commands.command() async def activate(self, ctx, *, short=None): + if not proceed_to_party(): + return """Activate a prepared poll. Parameter: