everything as jobs

This commit is contained in:
CroneKorkN 2026-01-18 19:00:07 +01:00
parent 34be944b96
commit 154d2c48e8
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw
7 changed files with 108 additions and 10 deletions

View file

@ -46,25 +46,19 @@ class ServersController < ApplicationController
def start
authorize_user!
L4dServer::SystemdManager.start(@server)
@server.update(status: :starting)
Activity.log(current_user, "started", "Server", @server.id, {})
StartServerJob.perform_later(@server.id)
redirect_to @server, notice: "Server starting..."
end
def stop
authorize_user!
L4dServer::SystemdManager.stop(@server)
@server.update(status: :stopped)
Activity.log(current_user, "stopped", "Server", @server.id, {})
redirect_to @server, notice: "Server stopped."
StopServerJob.perform_later(@server.id)
redirect_to @server, notice: "Server stopping..."
end
def restart
authorize_user!
L4dServer::SystemdManager.restart(@server)
@server.update(status: :starting)
Activity.log(current_user, "restarted", "Server", @server.id, {})
RestartServerJob.perform_later(@server.id)
redirect_to @server, notice: "Server restarting..."
end

View file

@ -0,0 +1,28 @@
class RestartServerJob < ApplicationJob
queue_as :default
def perform(server_id)
server = Server.find(server_id)
log "Restarting server: #{server.name}"
begin
log "Calling SystemdManager.restart..."
L4dServer::SystemdManager.restart(server)
log "SystemdManager.restart completed successfully"
server.update(status: :starting)
log "Server status updated to starting"
Activity.log(server.user, "restarted", "Server", server.id, {})
log "Activity logged"
rescue StandardError => e
log "ERROR: Failed to restart server: #{e.message}"
log "Backtrace: #{e.backtrace.first(3).join("\n")}"
server.update(status: :failed)
Activity.log(server.user, "restart_failed", "Server", server.id, { error: e.message })
raise
end
end
end

View file

@ -0,0 +1,28 @@
class StartServerJob < ApplicationJob
queue_as :default
def perform(server_id)
server = Server.find(server_id)
log "Starting server: #{server.name}"
begin
log "Calling SystemdManager.start..."
L4dServer::SystemdManager.start(server)
log "SystemdManager.start completed successfully"
server.update(status: :starting)
log "Server status updated to starting"
Activity.log(server.user, "started", "Server", server.id, {})
log "Activity logged"
rescue StandardError => e
log "ERROR: Failed to start server: #{e.message}"
log "Backtrace: #{e.backtrace.first(3).join("\n")}"
server.update(status: :failed)
Activity.log(server.user, "start_failed", "Server", server.id, { error: e.message })
raise
end
end
end

View file

@ -0,0 +1,27 @@
class StopServerJob < ApplicationJob
queue_as :default
def perform(server_id)
server = Server.find(server_id)
log "Stopping server: #{server.name}"
begin
log "Calling SystemdManager.stop..."
L4dServer::SystemdManager.stop(server)
log "SystemdManager.stop completed successfully"
server.update(status: :stopped)
log "Server status updated to stopped"
Activity.log(server.user, "stopped", "Server", server.id, {})
log "Activity logged"
rescue StandardError => e
log "ERROR: Failed to stop server: #{e.message}"
log "Backtrace: #{e.backtrace.first(3).join("\n")}"
Activity.log(server.user, "stop_failed", "Server", server.id, { error: e.message })
raise
end
end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class RestartServerJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class StartServerJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class StopServerJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end