Skip to content

Commit

Permalink
1.0.7: Allow scheduled shutdown by uptime (e.g. 4 hours). Closes #4
Browse files Browse the repository at this point in the history
* Added checks to ensure schedule config is valid, with warnings if not
* Fixed misleading debug message if schedule warnings are disabled
* Cleaned up Gradle file with extra commentary
* Added unicode fix for ForgeGradle
* Convert common.gradle to UTF-8
* Updated README.md
  • Loading branch information
RoyCurtis committed Nov 16, 2015
1 parent e972af2 commit d8b1701
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 107 deletions.
12 changes: 4 additions & 8 deletions 1.7.10/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ buildscript
apply plugin: 'forge'
apply from: '../common.gradle'

minecraft
{
version = "1.7.10-10.13.4.1448-1.7.10"
}
// Sets the Forge version to use
minecraft.version = "1.7.10-10.13.4.1448-1.7.10"

jar
{
classifier "1.7.10"
}
// Sets the built jar's suffix
jar.classifier "1.7.10"
39 changes: 35 additions & 4 deletions 1.7.10/src/main/java/roycurtis/autoshutdown/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package roycurtis.autoshutdown;

import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.Logger;

import java.io.File;

Expand All @@ -19,6 +20,7 @@ class Config
static boolean scheduleEnabled = true;
static boolean scheduleWarning = true;
static boolean scheduleDelay = false;
static boolean scheduleUptime = false;
static int scheduleHour = 6;
static int scheduleMinute = 0;
static int scheduleDelayBy = 5;
Expand Down Expand Up @@ -51,15 +53,18 @@ static void init(File configFile)
"All times are 24 hour (military) format, relative to machine's local time");

scheduleEnabled = config.getBoolean("Enabled", SCHEDULE, scheduleEnabled,
"If true, server will automatically shutdown at given time of day");
"If true, server will automatically shutdown");
scheduleWarning = config.getBoolean("Warnings", SCHEDULE, scheduleWarning,
"If true, server will give five minutes of warnings prior to shutdown");
scheduleDelay = config.getBoolean("Delay", SCHEDULE, scheduleDelay,
"If true, server will delay shutdown until server is empty");
scheduleHour = config.getInt("Hour", SCHEDULE, scheduleHour, 0, 23,
"Hour of the shutdown process (e.g. 8 for 8 AM)");
scheduleUptime = config.getBoolean("Uptime", SCHEDULE, scheduleUptime,
"If true, server will use Hour and Minute as uptime until shutdown.\n" +
"If false, server will use Hour and Minute as time of day to shutdown.");
scheduleHour = config.getInt("Hour", SCHEDULE, scheduleHour, 0, 720,
"Hour of the shutdown process (e.g. 8 for 8 AM OR 8 hours uptime)");
scheduleMinute = config.getInt("Minute", SCHEDULE, scheduleMinute, 0, 59,
"Minute of the shutdown process (e.g. 30 for half-past)");
"Minute of the shutdown process (e.g. 30 for half-past OR 30 mins uptime)");

scheduleDelayBy = config.getInt(
"DelayBy", SCHEDULE, scheduleDelayBy, 1, 1440,
Expand Down Expand Up @@ -104,9 +109,35 @@ static void init(File configFile)
msgKick = config.getString("Kick", MESSAGES, msgKick,
"Message shown to player on disconnect during shutdown");

check();
config.save();
}

/**
* Checks the loaded configuration and makes adjustments based on other config
*/
static void check()
{
final Logger LOGGER = ForgeAutoShutdown.LOGGER;

// Ensure daily shutdown hour is not set to more than 23:00
if (!scheduleUptime && scheduleHour >= 24)
{
LOGGER.warn("Uptime shutdown is disabled, but the shutdown hour is more " +
"than 23! Please fix this in the config. It will be set to 00 hours.");
scheduleHour = 0;
}

// Ensure uptime shutdown is not set to zero hours zero minutes
if (scheduleUptime && scheduleHour == 0 && scheduleMinute == 0)
{
LOGGER.warn("Uptime shutdown is enabled, but is set to shutdown after " +
"0 hours and 0 minutes of uptime! Please fix this in the config. " +
"It will be set to 24 hours.");
scheduleHour = 24;
}
}

static boolean isNothingEnabled()
{
return !scheduleEnabled && !voteEnabled && !watchdogEnabled;
Expand Down
28 changes: 19 additions & 9 deletions 1.7.10/src/main/java/roycurtis/autoshutdown/ShutdownTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ public static void create()

Timer timer = new Timer("ForgeAutoShutdown timer");
Calendar shutdownAt = Calendar.getInstance();
shutdownAt.set(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.set(Calendar.MINUTE, Config.scheduleMinute);
shutdownAt.set(Calendar.SECOND, 0);

// Adjust for when current time surpasses shutdown schedule
// (e.g. if shutdown time is 07:00 and current time is 13:21)
if ( shutdownAt.before( Calendar.getInstance() ) )
shutdownAt.add(Calendar.DAY_OF_MONTH, 1);
if (Config.scheduleUptime)
{
shutdownAt.add(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.add(Calendar.MINUTE, Config.scheduleMinute);
}
else
{
shutdownAt.set(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.set(Calendar.MINUTE, Config.scheduleMinute);
shutdownAt.set(Calendar.SECOND, 0);

// Adjust for when current time surpasses shutdown schedule
// (e.g. if shutdown time is 07:00 and current time is 13:21)
if ( shutdownAt.before( Calendar.getInstance() ) )
shutdownAt.add(Calendar.DAY_OF_MONTH, 1);
}

Date shutdownAtDate = shutdownAt.getTime();

Expand Down Expand Up @@ -98,11 +107,12 @@ public void onServerTick(TickEvent.ServerTickEvent event)
}

if (Config.scheduleWarning && warningsLeft > 0)
{
performWarning();
LOGGER.debug("ShutdownTask ticked; %d warning(s) to go", warningsLeft);
}
else
Server.shutdown(Config.msgKick);

LOGGER.debug("ShutdownTask ticked; %d warning(s) to go", warningsLeft);
}

private boolean performDelay()
Expand Down
12 changes: 4 additions & 8 deletions 1.8/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ buildscript
apply plugin: 'forge'
apply from: '../common.gradle'

minecraft
{
version = "1.8-11.14.3.1450"
}
// Sets the Forge version to use
minecraft.version = "1.8-11.14.3.1450"

jar
{
classifier "1.8"
}
// Sets the built jar's suffix
jar.classifier "1.8"
39 changes: 35 additions & 4 deletions 1.8/src/main/java/roycurtis/autoshutdown/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package roycurtis.autoshutdown;

import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.Logger;

import java.io.File;

Expand All @@ -19,6 +20,7 @@ class Config
static boolean scheduleEnabled = true;
static boolean scheduleWarning = true;
static boolean scheduleDelay = false;
static boolean scheduleUptime = false;
static int scheduleHour = 6;
static int scheduleMinute = 0;
static int scheduleDelayBy = 5;
Expand Down Expand Up @@ -51,15 +53,18 @@ static void init(File configFile)
"All times are 24 hour (military) format, relative to machine's local time");

scheduleEnabled = config.getBoolean("Enabled", SCHEDULE, scheduleEnabled,
"If true, server will automatically shutdown at given time of day");
"If true, server will automatically shutdown");
scheduleWarning = config.getBoolean("Warnings", SCHEDULE, scheduleWarning,
"If true, server will give five minutes of warnings prior to shutdown");
scheduleDelay = config.getBoolean("Delay", SCHEDULE, scheduleDelay,
"If true, server will delay shutdown until server is empty");
scheduleHour = config.getInt("Hour", SCHEDULE, scheduleHour, 0, 23,
"Hour of the shutdown process (e.g. 8 for 8 AM)");
scheduleUptime = config.getBoolean("Uptime", SCHEDULE, scheduleUptime,
"If true, server will use Hour and Minute as uptime until shutdown.\n" +
"If false, server will use Hour and Minute as time of day to shutdown.");
scheduleHour = config.getInt("Hour", SCHEDULE, scheduleHour, 0, 720,
"Hour of the shutdown process (e.g. 8 for 8 AM OR 8 hours uptime)");
scheduleMinute = config.getInt("Minute", SCHEDULE, scheduleMinute, 0, 59,
"Minute of the shutdown process (e.g. 30 for half-past)");
"Minute of the shutdown process (e.g. 30 for half-past OR 30 mins uptime)");

scheduleDelayBy = config.getInt(
"DelayBy", SCHEDULE, scheduleDelayBy, 1, 1440,
Expand Down Expand Up @@ -104,9 +109,35 @@ static void init(File configFile)
msgKick = config.getString("Kick", MESSAGES, msgKick,
"Message shown to player on disconnect during shutdown");

check();
config.save();
}

/**
* Checks the loaded configuration and makes adjustments based on other config
*/
static void check()
{
final Logger LOGGER = ForgeAutoShutdown.LOGGER;

// Ensure daily shutdown hour is not set to more than 23:00
if (!scheduleUptime && scheduleHour >= 24)
{
LOGGER.warn("Uptime shutdown is disabled, but the shutdown hour is more " +
"than 23! Please fix this in the config. It will be set to 00 hours.");
scheduleHour = 0;
}

// Ensure uptime shutdown is not set to zero hours zero minutes
if (scheduleUptime && scheduleHour == 0 && scheduleMinute == 0)
{
LOGGER.warn("Uptime shutdown is enabled, but is set to shutdown after " +
"0 hours and 0 minutes of uptime! Please fix this in the config. " +
"It will be set to 24 hours.");
scheduleHour = 24;
}
}

static boolean isNothingEnabled()
{
return !scheduleEnabled && !voteEnabled && !watchdogEnabled;
Expand Down
28 changes: 19 additions & 9 deletions 1.8/src/main/java/roycurtis/autoshutdown/ShutdownTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,23 @@ public static void create()

Timer timer = new Timer("ForgeAutoShutdown timer");
Calendar shutdownAt = Calendar.getInstance();
shutdownAt.set(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.set(Calendar.MINUTE, Config.scheduleMinute);
shutdownAt.set(Calendar.SECOND, 0);

// Adjust for when current time surpasses shutdown schedule
// (e.g. if shutdown time is 07:00 and current time is 13:21)
if ( shutdownAt.before( Calendar.getInstance() ) )
shutdownAt.add(Calendar.DAY_OF_MONTH, 1);
if (Config.scheduleUptime)
{
shutdownAt.add(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.add(Calendar.MINUTE, Config.scheduleMinute);
}
else
{
shutdownAt.set(Calendar.HOUR_OF_DAY, Config.scheduleHour);
shutdownAt.set(Calendar.MINUTE, Config.scheduleMinute);
shutdownAt.set(Calendar.SECOND, 0);

// Adjust for when current time surpasses shutdown schedule
// (e.g. if shutdown time is 07:00 and current time is 13:21)
if ( shutdownAt.before( Calendar.getInstance() ) )
shutdownAt.add(Calendar.DAY_OF_MONTH, 1);
}

Date shutdownAtDate = shutdownAt.getTime();

Expand Down Expand Up @@ -98,11 +107,12 @@ public void onServerTick(TickEvent.ServerTickEvent event)
}

if (Config.scheduleWarning && warningsLeft > 0)
{
performWarning();
LOGGER.debug("ShutdownTask ticked; %d warning(s) to go", warningsLeft);
}
else
Server.shutdown(Config.msgKick);

LOGGER.debug("ShutdownTask ticked; %d warning(s) to go", warningsLeft);
}

private boolean performDelay()
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ForgeAutoShutdown is a server-only mod that can:

* Schedules a specific time of the day for the server to shut down. This allows the server to be automatically restarted daily by a shell script, Windows batch file or service.
* Allow players to vote for a manual shutdown, so a lagged out server would not require admin intervention
* Schedule the server to automatically shut down at a specific time of day, or after X hours and minutes of uptime. This allows the server to be automatically restarted by a shell script, Windows batch file or service.
* Allow players to vote for a manual shutdown, so a lagged out server does not require admin intervention
* Shutdown or kill a server that is hung (stalled) or laggy

# Requirements
Expand All @@ -23,14 +23,17 @@ ForgeAutoShutdown is a server-only mod that can:

*Any of these features may be disabled in the config*

## Scheduled daily shutdown
## Scheduled shutdown
ForgeAutoShutdown will log a message at the INFO level on server startup, with a date and time of the next scheduled shutdown. For example:

`[10:50:09] [Server thread/INFO] [ForgeAutoShutdown]: Next automatic shutdown: 08:30:00 19-June-2015`

If this message is missing, the mod has not been correctly installed or the schedule is disabled in config. If the mod is installed on a Minecraft client, it will log an ERROR to the console and not perform any function. It will not crash or disable the client.

Scheduled shutdown will always happen within the next 24 hours after server startup. This means that if the server starts and has missed the shutdown time even by a few minutes, it will schedule for the next day.
### Mode
By default, the shutdown will be scheduled to happen at a specific time of day. This is the time local to the server and will always happen within the next 24 hours after server startup. This means that if the server starts and has missed the shutdown time even by a few minutes, it will schedule for the next day.

Alternatively, setting `Uptime` to true means the server can shutdown after a specific amount of hours or minutes instead. This can allow the server to restart multiple times a day, or after a few days, etc.

### Warnings
By default a scheduled shutdown will give a warning to all players, each minute for five minutes, after the scheduled time. This can be disabled by setting `Warnings` to `false`. This means the server will shutdown, without warning, by the scheduled time.
Expand All @@ -40,7 +43,6 @@ If desired, the shutdown can be delayed by a configurable amount if players are

The shutdown will be repeatedly delayed until the server is empty. When checking if the server for players, fake players (e.g. BuildCraft's quarry) are excluded. Note that shutdown warnings are ineffective with delays, and a pending shutdown will be cancelled if a player comes online during the countdown.


## Voting

If enabled, players may vote a manual shutdown. To do so, a player must execute `/shutdown`. Then, **all** players (including the vote initiator) must vote using `/shutdown yes` or `/shutdown no`.
Expand Down
Loading

0 comments on commit d8b1701

Please sign in to comment.