🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / engine / Engine.java
1 /*
2  * Rhynodge - Engine.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.engine;
19
20 import jakarta.inject.Inject;
21 import jakarta.inject.Singleton;
22 import kotlin.Unit;
23 import kotlin.jvm.functions.Function0;
24 import kotlin.jvm.functions.Function1;
25 import net.pterodactylus.rhynodge.Reaction;
26 import net.pterodactylus.rhynodge.actions.EmailAction;
27 import net.pterodactylus.rhynodge.states.StateManager;
28 import org.apache.log4j.Logger;
29
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.concurrent.*;
33
34 import static java.lang.System.currentTimeMillis;
35 import static java.util.concurrent.TimeUnit.MILLISECONDS;
36 import static net.pterodactylus.util.exception.ExceptionsKt.suppressException;
37
38 /**
39  * Rhynodge main engine.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 @Singleton
44 public class Engine {
45
46         private final StateManager stateManager;
47         private final ScheduledExecutorService executorService;
48         private final Map<String, Future<?>> scheduledFutures = new ConcurrentHashMap<>();
49         private final EmailAction errorEmailAction;
50
51         @Inject
52         public Engine(StateManager stateManager, EmailAction errorEmailAction) {
53                 this.stateManager = stateManager;
54                 this.errorEmailAction = errorEmailAction;
55                 executorService = new ScheduledThreadPoolExecutor(1);
56         }
57
58         //
59         // ACCESSORS
60         //
61
62         /**
63          * Adds the given reaction to this engine.
64          *
65          * @param name
66          *              The name of the reaction
67          * @param reaction
68          *              The reaction to add to this engine
69          */
70         public void addReaction(String name, Reaction reaction) {
71                 ReactionState reactionState = new ReactionState(stateManager, name);
72                 Optional<net.pterodactylus.rhynodge.State> lastState = reactionState.loadLastState();
73                 long lastExecutionTime = lastState.map(net.pterodactylus.rhynodge.State::time).orElse(0L);
74                 long nextExecutionTime = lastExecutionTime + reaction.updateInterval();
75                 ReactionRunner reactionRunner = new ReactionRunner(reaction, reactionState, errorEmailAction);
76                 ScheduledFuture<?> future = executorService.scheduleWithFixedDelay(suppressException(wrapRunnable(reactionRunner), logExceptionForReaction(reaction))::invoke, nextExecutionTime - currentTimeMillis(), reaction.updateInterval(), MILLISECONDS);
77                 scheduledFutures.put(name, future);
78         }
79
80         private Function1<Exception, Unit> logExceptionForReaction(Reaction reaction) {
81                 return (Exception e) -> {
82                         logger.warn("Exception during Reaction “%s”!".formatted(reaction.name()), e);
83                         return Unit.INSTANCE;
84                 };
85         }
86
87         private Function0<Unit> wrapRunnable(Runnable runnable) {
88                 return () -> {
89                         runnable.run();
90                         return Unit.INSTANCE;
91                 };
92         }
93
94         /**
95          * Removes the reaction with the given name.
96          *
97          * @param name
98          *              The name of the reaction to remove
99          */
100         public void removeReaction(String name) {
101                 if (!scheduledFutures.containsKey(name)) {
102                         return;
103                 }
104                 scheduledFutures.remove(name).cancel(true);
105         }
106
107         private static final Logger logger = Logger.getLogger(Engine.class);
108
109 }