Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQUEST] Server Restarting Message before regular restart #40

Closed
motonari728 opened this issue Apr 28, 2020 · 6 comments
Closed

[REQUEST] Server Restarting Message before regular restart #40

motonari728 opened this issue Apr 28, 2020 · 6 comments
Assignees

Comments

@motonari728
Copy link

Before regular restart, I want Discord message like
'Server is restarting for maintenance. It takes less than 3 minutes'.

(My)Discord Members can understand that The server will come back soon.

@motonari728
Copy link
Author

I run discord bot with RoR2DS on one VM.
So I want to make it clear whether mentenance, some accidents or VM shutdown.

@motonari728
Copy link
Author

motonari728 commented Apr 29, 2020

I completed Server autostop feature for me.
I'm setting server waits 30min to autostop.
I notice that the server autostop before autostart.
So this feature is Unnecessary right now. Sorry.😅

By the way, I made autostop function post to discord before autostop.
May be reusable, so I past here.
Code will tell more than my bad english.

@motonari728
Copy link
Author

motonari728 commented Apr 29, 2020

async def server_autostop(self):
    n = 30
    print('AutoStop enabled every ' + str(n) + ' minutes')
    while True:
        # 秒単位で待つ. n分
        # n分ごとにサーバーのプレイ人数をチェックして、0人だったら終了する
        await asyncio.sleep(60*n)
        is_server_running = await server()
        if (not is_server_running) or server_info.player_count == 0:
            # stop RoR2DS first
            await server_stop()
            await asyncio.sleep(10)
            if os.path.exists(BepInEx / "LogOutput.log"):
                try:
                    os.remove(BepInEx / "LogOutput.log")
                except Exception:
                    print('Unable to remove log file')
            if os.path.exists(BepInEx / "LogOutput.log.offset"):
                try:
                    os.remove(BepInEx / "LogOutput.log.offset")
                except Exception:
                    print('Unable to remove offset! Chat may not work!')

            # make POST request to Azure Automation
            url = "Azure Automation Webhook(HTTP POST) Here"
            data = {}
            headers = {
                'Content-Type': 'application/json',
            }
            try:
                print("Start autostop function. Requesting Azure API")
                request = urllib.request.Request(url, json.dumps(data).encode(), headers)
                response = urllib.request.urlopen(request)
                responseCode = response.getcode()
                body = response.read()
                # json = body.decode('utf-8')
                print('Server Shutdown is requested because no one playing. ResponseCode: ' + str(responseCode) + "\n body: " + str(body))
            except Exception as e:
                print("error in requesting Azure API")
                print(e)
                print(traceback.format_exc())

            # Post result to Discord
            try: 
                print("Post to discord before auto server shutdown")
                channel = config_object.getint('RoR2', 'channel')
                channel = self.bot.get_channel(channel)
                chatContent = 'しばらく誰も使っていないため、サーバーにシャットダウンリクエストを送ります。 \nresponseCode: ' + str(responseCode) + '\n responseBody: ' + str(body)
                await channel.send(chatContent)
            except Exception as e:
                print("error in posting to discord before auto server shutdown")
                print(e)
                print(traceback.format_exc())

        elif server_info.player_count > 0:
            print('Players currently in server')

@motonari728
Copy link
Author

(But this feature looks good when use with autorestart function)

@InfernalPlacebo
Copy link
Contributor

Thanks for showing the code you used to fix this! I'll look into maybe including this into the main program. Sorry I wasn't able to look into this today for you. Did that resolve your issue/can I close this?

@motonari728
Copy link
Author

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants