diff --git a/docker/backend/shard.lock b/docker/backend/shard.lock
index d643463..aa8e8ed 100644
--- a/docker/backend/shard.lock
+++ b/docker/backend/shard.lock
@@ -108,6 +108,10 @@ shards:
git: https://github.com/spider-gazelle/secrets-env.git
version: 1.3.1
+ service:
+ git: https://git.dergrimm.net/dergrimm/service.git
+ version: 0.1.0+git.commit.b1e449572cffc80afa0ceb518590591d4f9c5bed
+
shard:
git: https://github.com/maiha/shard.cr.git
version: 0.3.1
diff --git a/docker/backend/shard.yml b/docker/backend/shard.yml
index 94d1140..db684c4 100644
--- a/docker/backend/shard.yml
+++ b/docker/backend/shard.yml
@@ -66,3 +66,5 @@ dependencies:
github: maiha/shard.cr
retriable:
github: Sija/retriable.cr
+ service:
+ git: https://git.dergrimm.net/dergrimm/service.git
diff --git a/docker/backend/src/backend.cr b/docker/backend/src/backend.cr
index 71f704f..9efb84a 100644
--- a/docker/backend/src/backend.cr
+++ b/docker/backend/src/backend.cr
@@ -14,8 +14,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
-require "retriable/core_ext/kernel"
-
require "./backend/*"
# Base module
diff --git a/docker/backend/src/backend/api.cr b/docker/backend/src/backend/api.cr
index 30e6c92..de67362 100644
--- a/docker/backend/src/backend/api.cr
+++ b/docker/backend/src/backend/api.cr
@@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
+require "./api/service"
require "./api/*"
# Api module
diff --git a/docker/backend/src/backend/api/run.cr b/docker/backend/src/backend/api/run.cr
deleted file mode 100644
index 6820443..0000000
--- a/docker/backend/src/backend/api/run.cr
+++ /dev/null
@@ -1,28 +0,0 @@
-# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
-# Copyright (C) 2022 Dominic Grimm
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see .
-
-require "http/server"
-
-module Backend
- module Api
- extend self
-
- # Runs API
- def run : Nil
- WebServer.new.run
- end
- end
-end
diff --git a/docker/backend/src/backend/api/service.cr b/docker/backend/src/backend/api/service.cr
index 2b62175..929b0cf 100644
--- a/docker/backend/src/backend/api/service.cr
+++ b/docker/backend/src/backend/api/service.cr
@@ -14,13 +14,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
+require "service"
+require "log"
+
module Backend
module Api
- # Api service
- SERVICE = ->do
- Log.info { "Starting Api service..." }
- run
- Log.info { "Api service stopped." }
+ # API service
+ class Service < ::Service
+ Log = ::Log.for(self)
+
+ # Runs API service
+ def run(_unit : ::Service::Unit) : ::Service::Unit?
+ Log.info { "Starting Api service..." }
+ WebServer.new.run
+ Log.info { "Api service stopped." }
+
+ ::Service::Unit.new(self)
+ end
end
end
end
diff --git a/docker/backend/src/backend/run.cr b/docker/backend/src/backend/run.cr
deleted file mode 100644
index b650a46..0000000
--- a/docker/backend/src/backend/run.cr
+++ /dev/null
@@ -1,58 +0,0 @@
-# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
-# Copyright (C) 2022 Dominic Grimm
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see .
-
-module Backend
- extend self
-
- # Runs backend services
- def run : Nil
- {% if flag?(:development) %}
- Log.warn { "Backend is running in development mode! Do not use this in production!" }
- {% end %}
-
- Log.info { "Checking if DB schema is up to date..." }
- retry(backoff: false, base_interval: 10.seconds, multiplier: 1.0) do
- case retry(on: DB::ConnectionRefused, backoff: false) do
- Db.schema_up_to_date_compare
- end
- when nil
- Log.fatal { "No database schema is applied. Please run `bash scripts/micrate.sh up` urgently!" }
- raise Exception.new
- when -1
- Log.warn { "Database schema is not up to date. Please run `bash scripts/micrate.sh up`." }
- when 0
- Log.info { "Database schema is up to date." }
- else
- Log.warn { "Database schema is maybe up to date but not consistent. Please run `bash scripts/micrate.sh up` to be safe." }
- end
- end
-
- Log.info { "Starting services..." }
- channel = Channel(Nil).new(SERVICES.size)
- SERVICES.each do |service|
- spawn do
- service.call
- channel.send(nil)
- end
- end
- SERVICES.size.times do
- channel.receive
- end
- Fiber.yield
-
- Log.info { "Backend services started." }
- end
-end
diff --git a/docker/backend/src/backend/runner.cr b/docker/backend/src/backend/runner.cr
new file mode 100644
index 0000000..fa4f079
--- /dev/null
+++ b/docker/backend/src/backend/runner.cr
@@ -0,0 +1,64 @@
+# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
+# Copyright (C) 2022 Dominic Grimm
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+
+require "service"
+require "log"
+require "retriable"
+
+module Backend
+ # Backend runner
+ class Runner < Service::Runner
+ Log = ::Log.for(self)
+
+ # Service starters
+ def starters : Array(Service::Starter)
+ [
+ Service::SynchronousStarter.new([
+ Api::Service.new.as(Service),
+ Worker::Service.new.as(Service),
+ ] of Service).as(Service::Starter),
+ ]
+ end
+
+ # Run the backend
+ def run : self
+ {% if flag?(:development) %}
+ Log.warn { "Backend is running in development mode! Do not use this in production!" }
+ {% end %}
+
+ Log.info { "Checking if DB schema is up to date..." }
+ Retriable.retry(backoff: false, base_interval: 10.seconds, multiplier: 1.0) do
+ case Retriable.retry(on: DB::ConnectionRefused, backoff: false) do
+ Db.schema_up_to_date_compare
+ end
+ when nil
+ Log.fatal { "No database schema is applied. Please run `bash scripts/micrate.sh up` urgently!" }
+ raise Exception.new
+ when -1
+ Log.warn { "Database schema is not up to date. Please run `bash scripts/micrate.sh up`." }
+ when 0
+ Log.info { "Database schema is up to date." }
+ else
+ Log.warn { "Database schema is maybe up to date but not consistent. Please run `bash scripts/micrate.sh up` to be safe." }
+ end
+ end
+
+ Log.info { "Starting services..." }
+
+ super
+ end
+ end
+end
diff --git a/docker/backend/src/backend/services.cr b/docker/backend/src/backend/services.cr
deleted file mode 100644
index 351b924..0000000
--- a/docker/backend/src/backend/services.cr
+++ /dev/null
@@ -1,23 +0,0 @@
-# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
-# Copyright (C) 2022 Dominic Grimm
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see .
-
-module Backend
- # Backend services to be included in the application
- SERVICES = [
- Api::SERVICE,
- Worker::SERVICE,
- ]
-end
diff --git a/docker/backend/src/backend/worker/run.cr b/docker/backend/src/backend/worker/run.cr
deleted file mode 100644
index a9a045d..0000000
--- a/docker/backend/src/backend/worker/run.cr
+++ /dev/null
@@ -1,26 +0,0 @@
-# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
-# Copyright (C) 2022 Dominic Grimm
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see .
-
-module Backend
- module Worker
- extend self
-
- # Runs the worker
- def run : Nil
- Mosquito::Runner.start
- end
- end
-end
diff --git a/docker/backend/src/backend/worker/service.cr b/docker/backend/src/backend/worker/service.cr
index 0871c79..1229aef 100644
--- a/docker/backend/src/backend/worker/service.cr
+++ b/docker/backend/src/backend/worker/service.cr
@@ -14,13 +14,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
+require "service"
+require "log"
+
module Backend
module Worker
# Worker service
- SERVICE = ->do
- Log.info { "Starting worker service..." }
- run
- Log.info { "Worker service stopped." }
+ class Service < ::Service
+ Log = ::Log.for(self)
+
+ # Runs worker service
+ def run(_unit : ::Service::Unit) : ::Service::Unit?
+ Log.info { "Starting worker service..." }
+ Mosquito::Runner.start
+ Log.info { "Worker service stopped." }
+
+ ::Service::Unit.new(self)
+ end
end
end
end
diff --git a/docker/backend/src/cli/backend.cr b/docker/backend/src/cli/backend.cr
index 33d7930..83176e3 100644
--- a/docker/backend/src/cli/backend.cr
+++ b/docker/backend/src/cli/backend.cr
@@ -61,7 +61,7 @@ cli = Commander::Command.new do |cmd|
c.long = c.short
c.run do
- Backend.run
+ Backend::Runner.new.run
end
end