Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public class SystemsConfig {
*/
private int asyncPoolSize = 4;


/**
* The location of some sort of bash script that re-compiles a new version of the Bot.
* Example in <a href="https://github.com/Java-Discord/JavaBot/pull/330">Pull Request #330</a>.
*/
private String redeployScriptLocation = "";

/**
* Configuration for the Hikari connection pool that's used for the bot's
* SQL data source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.javadiscord.javabot.Bot;
import net.javadiscord.javabot.util.Checks;
import net.javadiscord.javabot.util.ExceptionLogger;
import net.javadiscord.javabot.util.Responses;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
* <h3>This class represents the /redeploy command.</h3>
* Command that lets staff-members redeploy the bot.
* <p>
* This only works if the way the bot is hosted is set up correctly, for example with a bash script that handles
* compilation and a service set up with that bash script running before the bot gets started.
* This only works if the way the bot is hosted is set up correctly with a bash script that handles
* compilation and a service set up that automatically restarts the Bot.
* <p>
* I have explained how we do it in https://github.com/Java-Discord/JavaBot/pull/195
* I have explained how we do it in <a href="https://github.com/Java-Discord/JavaBot/pull/330">PR #330</a>.
*/
@Slf4j
public class RedeployCommand extends SlashCommand {
Expand All @@ -39,8 +42,23 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
return;
}
log.warn("Redeploying... Requested by: " + event.getUser().getAsTag());
event.reply("**Redeploying...** This may take some time.").queue();
event.reply("Redeploying, this may take a few minutes.").queue();
try {
Process p = new ProcessBuilder("/bin/sh", Bot.getConfig().getSystems().getRedeployScriptLocation()).start();
p.waitFor();
String result = new String(p.getInputStream().readAllBytes());
if (result.contains("COMPILATION FAILED")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use the exit code of the script for checking whether it was successful or not.
For example, there could be errors other than the Gradle script failing (e.g. with git clone).

For failing with any error, set -e could be used in the script in order to abort the script as soon as any command fails.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, will do that soon™️

event.getHook().sendMessage("Compilation failed, redeploy canceled.").queue();
log.warn("Redeploy canceled due to compilation error.");
return;
} else {
event.getHook().sendMessage("Compilation successful, restarting...").queue();
}
} catch (InterruptedException | IOException e) {
ExceptionLogger.capture(e, getClass().getSimpleName());
}
Bot.getMessageCache().synchronize();
event.getJDA().shutdown();
System.exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to move this to its own listener so that it is called when shutting down JDA completed.

}
}
}