summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
blob: e18f3c8906e64e5a833fe6df7d06283d379572e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.keuin.kbackupfabric.operation;

import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation;
import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;

import java.io.IOException;
import java.util.Objects;

public class RestoreOperation extends InvokableBlockingOperation {

    //private static final Logger LOGGER = LogManager.getLogger();
    private final Thread serverThread;
    private final CommandContext<ServerCommandSource> context;
    private final MinecraftServer server;
    private final ConfiguredBackupMethod configuredBackupMethod;

    public RestoreOperation(CommandContext<ServerCommandSource> context, ConfiguredBackupMethod configuredBackupMethod) {
        server = Objects.requireNonNull(context.getSource().getMinecraftServer());
        this.serverThread = Objects.requireNonNull(server.getThread());
        this.context = Objects.requireNonNull(context);
        this.configuredBackupMethod = Objects.requireNonNull(configuredBackupMethod);
    }

    @Override
    protected boolean blockingContext() {
        // do restore to backupName
        PrintUtil.broadcast(String.format("Restoring to backup %s ...", configuredBackupMethod.getBackupFileName()));

        PrintUtil.debug("Backup file name: " + configuredBackupMethod.getBackupFileName());

        PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depending on world size and disk speed, the progress may take from seconds to minutes.", true);
        PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true);
        PrintUtil.msgInfo(context, "After it shuts down, please restart the server manually.", true);
        final int WAIT_SECONDS = 10;
        for (int i = 0; i < WAIT_SECONDS; ++i) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignored) {
            }
        }
        PrintUtil.broadcast("Shutting down ...");
        //RestoreWorker worker = new RestoreWorker(server.getThread(), backupFilePath, levelDirectory);
        Thread workerThread = new Thread(new WorkerThread(), "RestoreWorker");
        workerThread.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException ignored) {
        }
        server.stop(false);
        return true;
    }

    @Override
    public String toString() {
        return String.format("restoration from %s", configuredBackupMethod.getBackupFileName());
    }

    private class WorkerThread implements Runnable {

        @Override
        public void run() {
            try {
                // Wait server thread die
                PrintUtil.info("Waiting for the server thread to exit ...");
                while (serverThread.isAlive()) {
                    try {
                        serverThread.join();
                    } catch (InterruptedException | RuntimeException ignored) {
                    }
                }

                int cnt = 5;
                do {
                    PrintUtil.info(String.format("Wait for %d seconds...", cnt));
                    try{
                        Thread.sleep(1000);
                    } catch (InterruptedException ignored) {
                    }
                }while(--cnt > 0);

                ////////////////////
                long startTime = System.currentTimeMillis();
                if (configuredBackupMethod.restore()) {
                    long endTime = System.currentTimeMillis();
                    PrintUtil.info(String.format(
                            "Restore complete! (%.2fs) Please restart the server manually.",
                            (endTime - startTime) / 1000.0
                    ));
                    PrintUtil.info("If you want to restart automatically after restoring, " +
                            "refer to https://github.com/keuin/KBackup-Fabric/blob/master/README.md");
                    //ServerRestartUtil.forkAndRestart();
                    System.exit(111);
                } else {
                    PrintUtil.error("Failed to restore! server will not restart automatically.");
                }

            } catch (SecurityException e) {
                e.printStackTrace();
                PrintUtil.error("An exception occurred while restoring.");
            } catch (IOException e) {
                e.printStackTrace();
                PrintUtil.error("Failed to restore due to an unexpected I/O exception.");
            }
            PrintUtil.error("Failed to restore.");
            System.exit(0); // all failed restoration will eventually go here
        }
    }
}