mirror of
https://github.com/juspay/services-flake.git
synced 2024-11-12 18:08:30 +03:00
Add redis service (#8)
This commit is contained in:
parent
9a9fc54749
commit
beaaca9f55
@ -17,7 +17,7 @@ TODO
|
||||
- [x] Hello World
|
||||
- [x] PostgreSQL
|
||||
- [ ] MySQL
|
||||
- [ ] Redis
|
||||
- [x] Redis
|
||||
- [ ] ...
|
||||
|
||||
## Contributing
|
||||
|
@ -2,5 +2,6 @@
|
||||
imports = [
|
||||
./hello.nix
|
||||
./postgres.nix
|
||||
./redis.nix
|
||||
];
|
||||
}
|
||||
|
98
nix/redis.nix
Normal file
98
nix/redis.nix
Normal file
@ -0,0 +1,98 @@
|
||||
# Based on https://github.com/cachix/devenv/blob/main/src/modules/services/redis.nix
|
||||
{ pkgs, lib, config, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
{
|
||||
options.services.redis = lib.mkOption {
|
||||
description = ''
|
||||
Enable redis server
|
||||
'';
|
||||
default = { };
|
||||
type = lib.types.submodule ({ config, ... }: {
|
||||
options = {
|
||||
enable = lib.mkEnableOption "redis";
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "redis";
|
||||
description = "Unique process name";
|
||||
};
|
||||
|
||||
package = lib.mkPackageOption pkgs "redis" { };
|
||||
|
||||
dataDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "./data/${config.name}";
|
||||
description = "The redis data directory";
|
||||
};
|
||||
|
||||
bind = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = "127.0.0.1";
|
||||
description = ''
|
||||
The IP interface to bind to.
|
||||
`null` means "all interfaces".
|
||||
'';
|
||||
example = "127.0.0.1";
|
||||
};
|
||||
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 6379;
|
||||
description = ''
|
||||
The TCP port to accept connections.
|
||||
|
||||
If port is set to `0`, redis will not listen on a TCP socket.
|
||||
'';
|
||||
};
|
||||
|
||||
extraConfig = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Additional text to be appended to `redis.conf`.";
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
config = let cfg = config.services.redis; in lib.mkIf cfg.enable {
|
||||
|
||||
settings.processes.${cfg.name} =
|
||||
let
|
||||
redisConfig = pkgs.writeText "redis.conf" ''
|
||||
port ${toString cfg.port}
|
||||
${optionalString (cfg.bind != null) "bind ${cfg.bind}"}
|
||||
${cfg.extraConfig}
|
||||
'';
|
||||
|
||||
startScript = pkgs.writeShellScriptBin "start-redis" ''
|
||||
set -euo pipefail
|
||||
|
||||
export REDISDATA=${cfg.dataDir}
|
||||
|
||||
if [[ ! -d "$REDISDATA" ]]; then
|
||||
mkdir -p "$REDISDATA"
|
||||
fi
|
||||
|
||||
exec ${cfg.package}/bin/redis-server ${redisConfig} --dir "$REDISDATA"
|
||||
'';
|
||||
in
|
||||
{
|
||||
command = "${startScript}/bin/start-redis";
|
||||
|
||||
readiness_probe = {
|
||||
exec.command = "${cfg.package}/bin/redis-cli -p ${toString cfg.port} ping";
|
||||
initial_delay_seconds = 2;
|
||||
period_seconds = 10;
|
||||
timeout_seconds = 4;
|
||||
success_threshold = 1;
|
||||
failure_threshold = 5;
|
||||
};
|
||||
|
||||
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
|
||||
availability.restart = "on_failure";
|
||||
};
|
||||
};
|
||||
}
|
10
nix/redis_test.nix
Normal file
10
nix/redis_test.nix
Normal file
@ -0,0 +1,10 @@
|
||||
{ config, ... }: {
|
||||
services.redis.enable = true;
|
||||
testScript = ''
|
||||
process_compose.wait_until(lambda procs:
|
||||
# TODO: Check for 'is_ready' instead of 'status'
|
||||
procs["redis"]["status"] == "Running"
|
||||
)
|
||||
machine.succeed("${config.services.redis.package}/bin/redis-cli ping | grep -q 'PONG'")
|
||||
'';
|
||||
}
|
@ -20,6 +20,12 @@
|
||||
../nix/postgres_test.nix
|
||||
];
|
||||
};
|
||||
redis = {
|
||||
imports = [
|
||||
inputs.services-flake.processComposeModules.default
|
||||
../nix/redis_test.nix
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user