Browse Source

rails: Add options for database pool size and connection timeout

Improvements:

  * Added new database options

  * Added first test file
master
Peter J. Jones 3 months ago
parent
commit
96ac477d36
Signed by: Peter Jones <pjones@devalot.com> GPG Key ID: 9DAFAA8D01941E49

+ 6
- 0
README.md 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)

+ 2
- 2
modules/services/web/rails/database.yml 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'] %>

+ 2
- 0
modules/services/web/rails/functions.nix 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;


+ 15
- 1
modules/services/web/rails/options.nix 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.";
};
};
};


+ 5
- 5
modules/services/web/rails/systemd.nix 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
- 0
test/default.nix View File

@@ -0,0 +1,8 @@
{ pkgs ? import <nixpkgs> {}
}:

with pkgs;

{
rails = (callPackage ./services/web/rails/test.nix {}).test;
}

+ 2
- 0
test/services/web/rails/app/Gemfile View File

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

+ 13
- 0
test/services/web/rails/app/Gemfile.lock 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

+ 1
- 0
test/services/web/rails/app/README.md View File

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

+ 19
- 0
test/services/web/rails/app/Rakefile 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

+ 0
- 0
test/services/web/rails/app/config/database.yml View File


+ 0
- 0
test/services/web/rails/app/config/secrets.yml View File


+ 0
- 0
test/services/web/rails/app/db/migrate/00000000000000_fake.rb View File


+ 0
- 0
test/services/web/rails/app/db/schema.rb View File


+ 27
- 0
test/services/web/rails/app/default.nix 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 ];
}

+ 12
- 0
test/services/web/rails/app/nix/gemset.nix View File

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

+ 29
- 0
test/services/web/rails/test.nix 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");
'';
}

Loading…
Cancel
Save