Merge pull request 'Service shard integration' (#59) from service-shard-integration into main
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: mentorenwahl/mentorenwahl#59
This commit is contained in:
commit
41b1e19802
13 changed files with 102 additions and 148 deletions
|
@ -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
|
||||
|
||||
shard:
|
||||
git: https://github.com/maiha/shard.cr.git
|
||||
version: 0.3.1
|
||||
|
|
|
@ -66,3 +66,5 @@ dependencies:
|
|||
github: maiha/shard.cr
|
||||
retriable:
|
||||
github: Sija/retriable.cr
|
||||
service:
|
||||
git: https://git.dergrimm.net/dergrimm/service.git
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "retriable/core_ext/kernel"
|
||||
|
||||
require "./backend/*"
|
||||
|
||||
# Base module
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "./api/service"
|
||||
require "./api/*"
|
||||
|
||||
# Api module
|
||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "http/server"
|
||||
|
||||
module Backend
|
||||
module Api
|
||||
extend self
|
||||
|
||||
# Runs API
|
||||
def run : Nil
|
||||
WebServer.new.run
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,13 +14,23 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "service"
|
||||
require "log"
|
||||
|
||||
module Backend
|
||||
module Api
|
||||
# Api service
|
||||
SERVICE = ->do
|
||||
# API service
|
||||
class Service < ::Service
|
||||
Log = ::Log.for(self)
|
||||
|
||||
# Runs API service
|
||||
def run(_unit : ::Service::Unit) : ::Service::Unit?
|
||||
Log.info { "Starting Api service..." }
|
||||
run
|
||||
WebServer.new.run
|
||||
Log.info { "Api service stopped." }
|
||||
|
||||
::Service::Unit.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
64
docker/backend/src/backend/runner.cr
Normal file
64
docker/backend/src/backend/runner.cr
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
module Backend
|
||||
# Backend services to be included in the application
|
||||
SERVICES = [
|
||||
Api::SERVICE,
|
||||
Worker::SERVICE,
|
||||
]
|
||||
end
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
module Backend
|
||||
module Worker
|
||||
extend self
|
||||
|
||||
# Runs the worker
|
||||
def run : Nil
|
||||
Mosquito::Runner.start
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,13 +14,23 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "service"
|
||||
require "log"
|
||||
|
||||
module Backend
|
||||
module Worker
|
||||
# Worker service
|
||||
SERVICE = ->do
|
||||
class Service < ::Service
|
||||
Log = ::Log.for(self)
|
||||
|
||||
# Runs worker service
|
||||
def run(_unit : ::Service::Unit) : ::Service::Unit?
|
||||
Log.info { "Starting worker service..." }
|
||||
run
|
||||
Mosquito::Runner.start
|
||||
Log.info { "Worker service stopped." }
|
||||
|
||||
::Service::Unit.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue