rails: Add options for database pool size and connection timeout

Improvements:

  * Added new database options

  * Added first test file
This commit is contained in:
Peter Jones 2019-08-01 13:56:02 -07:00
parent 4043563dbc
commit 96ac477d36
No known key found for this signature in database
GPG key ID: 9DAFAA8D01941E49
17 changed files with 141 additions and 8 deletions

View file

@ -44,6 +44,12 @@ Module List
Simple way to configure a whole network of WireGuard machines.
Running Tests
-------------
$ nix-build test
[nixos]: https://nixos.org/
[nixpkgs]: https://nixos.org/nixpkgs/
[phoebe]: https://en.wikipedia.org/wiki/Phoebe_(moon)

View file

@ -1,8 +1,8 @@
<%= ENV['RAILS_ENV'] %>:
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
pool: <%= ENV['DATABASE_POOL_SIZE'] %>
timeout: <%= ENV['DATABASE_TIMEOUT'] %>
host: <%= ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'] %>
database: <%= ENV['DATABASE_NAME'] %>

View file

@ -21,6 +21,8 @@ rec {
DATABASE_PORT = toString app.database.port;
DATABASE_NAME = app.database.name;
DATABASE_USER = app.database.user;
DATABASE_POOL_SIZE = toString app.database.pool;
DATABASE_TIMEOUT = toString app.database.timeout;
DATABASE_PASSWORD_FILE = "${app.home}/state/database.password";
} // app.environment;

View file

@ -45,10 +45,24 @@ let
};
port = mkOption {
type = types.int;
type = types.ints.positive;
default = config.services.postgresql.port;
description = "Port number for the database server";
};
pool = mkOption {
type = types.ints.positive;
default = 10;
example = 5;
description = "Size of connection pool.";
};
timeout = mkOption {
type = types.ints.positive;
default = 1000;
example = 5000;
description = "Database timeout in milliseconds.";
};
};
};

View file

@ -74,7 +74,7 @@ let
preStart = optionalString (service.isMain || service.isMigration) ''
# Link the package into the application's home directory:
if [ ! -e "${funcs.appLink app}" ] || [ "${toString app.deployedExternally}" -ne 1 ]; then
if [ ! -e "${funcs.appLink app}" ] || [ -z "${toString app.deployedExternally}" ]; then
ln -nfs "${app.package}" "${funcs.appLink app}"
fi
@ -82,9 +82,9 @@ let
rm -rf ${app.home}/config
mkdir -p ${app.home}/{config,log,tmp,db,state}
cp -rf ${funcs.appLink app}/share/${app.name}/config.dist/* ${app.home}/config/
cp ${funcs.appLink app}/share/${app.name}/db/schema.rb.dist ${app.home}/db/schema.rb
cp ${./database.yml} ${app.home}/config/database.yml
cp ${app.database.passwordFile} ${app.home}/state/database.password
cp -f ${funcs.appLink app}/share/${app.name}/db/schema.rb.dist ${app.home}/db/schema.rb
cp -f ${./database.yml} ${app.home}/config/database.yml
cp -f ${app.database.passwordFile} ${app.home}/state/database.password
# Additional set up for the home directory:
mkdir -p ${app.home}/home
@ -96,7 +96,7 @@ let
# Copy the sourcedFile if necessary:
${optionalString (app.sourcedFile != null) ''
cp ${app.sourcedFile} ${app.home}/state/sourcedFile.sh
cp -f ${app.sourcedFile} ${app.home}/state/sourcedFile.sh
''}
# Fix permissions:

8
test/default.nix Normal file
View file

@ -0,0 +1,8 @@
{ pkgs ? import <nixpkgs> {}
}:
with pkgs;
{
rails = (callPackage ./services/web/rails/test.nix {}).test;
}

View file

@ -0,0 +1,2 @@
source 'https://rubygems.org'
gem('rake')

View file

@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
rake (12.3.3)
PLATFORMS
ruby
DEPENDENCIES
rake
BUNDLED WITH
1.17.2

View file

@ -0,0 +1 @@
This is a fake Ruby on Rails application for testing.

View file

@ -0,0 +1,19 @@
# A bunch of dummy rake tasks:
namespace :db do
task :migrate do
puts "db:migrate"
end
namespace :schema do
task :load do
puts "db:schema:load"
end
end
end
namespace :assets do
task :precompile do
puts "assets:precompile"
end
end

View file

View file

@ -0,0 +1,27 @@
{ pkgs ? import <nixpkgs> { }
}:
let
phoebe = import ../../../../../helpers.nix { inherit pkgs; };
puma = pkgs.writeShellScriptBin "puma" ''
# Loop forever:
while :; do :; done
'';
env = pkgs.bundlerEnv {
name = "app-env";
ruby = pkgs.ruby;
gemfile = ./Gemfile;
lockfile = ./Gemfile.lock;
gemset = ./nix/gemset.nix;
};
in
phoebe.mkRailsDerivation {
inherit env;
name = "app";
src = ./.;
extraPackages = [ puma ];
}

View file

@ -0,0 +1,12 @@
{
rake = {
groups = ["default"];
platforms = [];
source = {
remotes = ["https://rubygems.org"];
sha256 = "1cvaqarr1m84mhc006g3l1vw7sa5qpkcw0138lsxlf769zdllsgp";
type = "gem";
};
version = "12.3.3";
};
}

View file

@ -0,0 +1,29 @@
{ pkgs ? import <nixpkgs> {}
}:
pkgs.nixosTest {
name = "rails-test";
nodes = {
simple = {config, pkgs, ...}: {
imports = [ ../../../../modules ];
phoebe.security.enable = false;
phoebe.services.rails.apps.app = {
package = import ./app/default.nix { inherit pkgs; };
domain = "foo.example.com";
port = 3000;
database.name = "app";
database.user = "app";
database.passwordFile = "/dev/null";
};
};
};
testScript = ''
$simple->start;
$simple->waitForUnit("rails-app-main.service");
$simple->succeed("railsdo app rake -T");
$simple->succeed("test -L /var/lib/rails/app/package");
'';
}