Service shard integration #59

Merged
dergrimm merged 3 commits from service-shard-integration into main 2022-02-14 17:49:12 +00:00
13 changed files with 102 additions and 148 deletions

View file

@ -17,7 +17,7 @@
*/
-- +micrate Up
-- SQL in section ' Up ' is executed when this migration is applied
CREATE TYPE user_roles AS ENUM (' Teacher ', ' Student ');
CREATE TYPE user_roles AS ENUM ('Teacher', 'Student');
CREATE TABLE users(
id BIGSERIAL PRIMARY KEY,

View file

@ -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

View file

@ -66,3 +66,5 @@ dependencies:
github: maiha/shard.cr
retriable:
github: Sija/retriable.cr
service:
git: https://git.dergrimm.net/dergrimm/service.git

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
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

View file

@ -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

View 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

View file

@ -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

View file

@ -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

View file

@ -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
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

View file

@ -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