Fix?
This commit is contained in:
parent
9b528042b3
commit
0618794491
9 changed files with 56 additions and 12 deletions
|
@ -4,14 +4,23 @@ import eu.m724.mstats.api.service.PluginService;
|
||||||
import io.quarkus.runtime.StartupEvent;
|
import io.quarkus.runtime.StartupEvent;
|
||||||
import jakarta.enterprise.event.Observes;
|
import jakarta.enterprise.event.Observes;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.persistence.EntityExistsException;
|
||||||
|
|
||||||
public class Startup {
|
public class Startup {
|
||||||
@Inject
|
@Inject
|
||||||
PluginService pluginService;
|
PluginService pluginService;
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public void onStartup(@Observes StartupEvent event) {
|
public void onStartup(@Observes StartupEvent event) {
|
||||||
pluginService.createPlugin("ploogin");
|
try {
|
||||||
|
pluginService.createPlugin(1, "Tweaks724");
|
||||||
|
} catch (EntityExistsException e) {
|
||||||
|
System.out.println("exists1");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
pluginService.createPlugin(2, "Giants");
|
||||||
|
} catch (EntityExistsException e) {
|
||||||
|
System.out.println("exists2");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ public class PluginApiResource {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class StatsResponse {
|
static class StatsResponse {
|
||||||
@JsonProperty("id")
|
@JsonProperty("id")
|
||||||
public Long id;
|
public Long id;
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ public class PluginApiResource {
|
||||||
public List<Version> pluginVersions;
|
public List<Version> pluginVersions;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Version {
|
static class Version {
|
||||||
public Version(String version, int servers) {
|
public Version(String version, int servers) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.servers = servers;
|
this.servers = servers;
|
||||||
|
|
|
@ -52,6 +52,17 @@ public class ServerApiResource {
|
||||||
return Response.ok(heartbeatResponse).build();
|
return Response.ok(heartbeatResponse).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Path("/remove")
|
||||||
|
@POST
|
||||||
|
public Response remove() {
|
||||||
|
if (!identity.isAnonymous()) {
|
||||||
|
Server server = identity.getAttribute("server");
|
||||||
|
serverService.removeServer(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
public static class HeartbeatRequest {
|
public static class HeartbeatRequest {
|
||||||
/** List of registered plugins, this is sent only on boot */
|
/** List of registered plugins, this is sent only on boot */
|
||||||
@JsonProperty("plugins")
|
@JsonProperty("plugins")
|
||||||
|
|
|
@ -7,10 +7,11 @@ import jakarta.transaction.Transactional;
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class PluginService {
|
public class PluginService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public Plugin createPlugin(String name) {
|
public Plugin createPlugin(long id, String name) {
|
||||||
return Plugin.createPlugin(name);
|
return Plugin.createPlugin(id, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Plugin getPlugin(long id) {
|
public Plugin getPlugin(long id) {
|
||||||
return Plugin.findById(id);
|
return Plugin.findById(id);
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package eu.m724.mstats.api.service;
|
package eu.m724.mstats.api.service;
|
||||||
|
|
||||||
import eu.m724.mstats.api.resource.ServerApiResource;
|
import eu.m724.mstats.api.resource.ServerApiResource;
|
||||||
|
import eu.m724.mstats.orm.Plugin;
|
||||||
import eu.m724.mstats.orm.Server;
|
import eu.m724.mstats.orm.Server;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class ServerService {
|
public class ServerService {
|
||||||
|
@ -19,17 +22,30 @@ public class ServerService {
|
||||||
server.persistAndFlush();
|
server.persistAndFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void removeServer(Server server) {
|
||||||
|
server.delete();
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void heartbeat(Server server, ServerApiResource.HeartbeatRequest heartbeatRequest) {
|
public void heartbeat(Server server, ServerApiResource.HeartbeatRequest heartbeatRequest) {
|
||||||
server = Server.findById(server.id); // this is necessary for some reason
|
server = Server.findById(server.id); // this is necessary for some reason
|
||||||
|
Set<Long> unknownPlugins = new HashSet<>();
|
||||||
|
|
||||||
for (ServerApiResource.RunningPlugin plugin : heartbeatRequest.plugins) {
|
for (ServerApiResource.RunningPlugin plugin : heartbeatRequest.plugins) {
|
||||||
server.addPlugin(plugin.id, plugin.version);
|
Plugin p = server.addPlugin(plugin.id, plugin.version);
|
||||||
|
if (p == null) {
|
||||||
|
unknownPlugins.add(plugin.id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heartbeatRequest.serverVersion != null)
|
if (heartbeatRequest.serverVersion != null)
|
||||||
server.serverVersion = heartbeatRequest.serverVersion;
|
server.serverVersion = heartbeatRequest.serverVersion;
|
||||||
|
|
||||||
|
if (!unknownPlugins.isEmpty()) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
server.heartbeat = Instant.now();
|
server.heartbeat = Instant.now();
|
||||||
server.persistAndFlush();
|
server.persistAndFlush();
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ public class MyHttpAuthenticationMechanism implements HttpAuthenticationMechanis
|
||||||
}
|
}
|
||||||
|
|
||||||
return QuarkusSecurityIdentity.builder()
|
return QuarkusSecurityIdentity.builder()
|
||||||
|
.setPrincipal(new QuarkusPrincipal("Anonymous")) // this is to fix an error in logging
|
||||||
.setAnonymous(true)
|
.setAnonymous(true)
|
||||||
.build();
|
.build();
|
||||||
}).runSubscriptionOn(Infrastructure.getDefaultWorkerPool());
|
}).runSubscriptionOn(Infrastructure.getDefaultWorkerPool());
|
||||||
|
|
|
@ -19,8 +19,14 @@ public class Plugin extends PanacheEntity {
|
||||||
public List<Server> servers = new ArrayList<>();
|
public List<Server> servers = new ArrayList<>();
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public static Plugin createPlugin(String name) {
|
public static Plugin createPlugin(long id, String name) {
|
||||||
|
if (Plugin.find("name", name).firstResultOptional().isPresent()) throw new EntityExistsException();
|
||||||
|
|
||||||
Plugin plugin = new Plugin();
|
Plugin plugin = new Plugin();
|
||||||
|
if (id != -1) {
|
||||||
|
if (Plugin.findById(id) != null) throw new EntityExistsException();
|
||||||
|
plugin.id = id;
|
||||||
|
}
|
||||||
plugin.name = name;
|
plugin.name = name;
|
||||||
|
|
||||||
plugin.persistAndFlush();
|
plugin.persistAndFlush();
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class Server extends PanacheEntity {
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
public byte[] token;
|
public byte[] token;
|
||||||
|
|
||||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
public List<PluginWithVersion> plugins = new ArrayList<>();
|
public List<PluginWithVersion> plugins = new ArrayList<>();
|
||||||
|
|
||||||
public String serverVersion;
|
public String serverVersion;
|
||||||
|
@ -38,7 +38,7 @@ public class Server extends PanacheEntity {
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (plugins.stream().anyMatch(pwv -> pwv.plugin.id == id))
|
if (plugins.stream().anyMatch(pwv -> pwv.plugin.id == id))
|
||||||
return null;
|
return plugin;
|
||||||
|
|
||||||
plugins.add(new PluginWithVersion(plugin, version));
|
plugins.add(new PluginWithVersion(plugin, version));
|
||||||
plugin.servers.add(this);
|
plugin.servers.add(this);
|
||||||
|
|
|
@ -2,5 +2,5 @@ quarkus.hibernate-orm.database.generation=update
|
||||||
|
|
||||||
quarkus.datasource.db-kind=h2
|
quarkus.datasource.db-kind=h2
|
||||||
quarkus.datasource.username=username-default
|
quarkus.datasource.username=username-default
|
||||||
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
|
quarkus.datasource.jdbc.url=jdbc:h2:./db:default
|
||||||
quarkus.datasource.jdbc.max-size=13
|
quarkus.datasource.jdbc.max-size=13
|
Loading…
Reference in a new issue