Skip to content

Commit

Permalink
temporal: add temporal service
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed May 31, 2023
1 parent e206d8f commit 539c18b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/temporal/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh
set -x

# Start the services in the background and store the PID
echo "Starting temporal service..."
devenv up &
DEVENV_PID=$!

# temporal status and store its exit status
check_temporal_status() {
echo "Waiting for service to become available..."
TEMPORAL_OUTPUT=$(temporal operator cluster health)
TEMPORAL_EXIT_STATUS=$?
}

# Continuously check temporal status until it returns successfully (up to a maximum of 20 times)
for i in $(seq 1 20); do
check_temporal_status
if [ $TEMPORAL_EXIT_STATUS -eq 0 ]; then
echo "Service is up..."
break
else
sleep 1
fi
done

echo "Checking namespace..."
temporal operator namespace describe mynamespace

# Print the captured output when temporal status succeeds
echo "Startup complete..."
temporal operator cluster system
echo "$TEMPORAL_OUTPUT"

# Clean up by terminating all spawned processes
pkill -P $DEVENV_PID
wait $DEVENV_PID&>/dev/null

# Exit the script
exit $TEMPORAL_EXIT_STATUS
10 changes: 10 additions & 0 deletions examples/temporal/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ pkgs, ... }:
{
services.temporal = {
enable = true;

namespaces = [ "mynamespace" ];

ephemeral = false;
};
}
3 changes: 3 additions & 0 deletions examples/temporal/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
92 changes: 92 additions & 0 deletions src/modules/services/temporal.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{ pkgs, lib, config, ... }:

let
cfg = config.services.temporal;
types = lib.types;

databaseFile = config.env.DEVENV_STATE + "/temporal.sqlite";

commandArgs = [
"--log-format=pretty"
"--ip=${cfg.ip}"
"--port=${toString cfg.port}"
"--headless=${lib.boolToString (!cfg.ui.enable)}"
"--ui-ip=${cfg.ui.ip}"
"--ui-port=${toString cfg.ui.port}"
] ++
(lib.forEach cfg.namespaces (namespace: "--namespace=${namespace}")) ++
(lib.optionals (!cfg.ephemeral) [ "--db-filename=${databaseFile}" ]);
in
{
options.services.temporal = {
enable = lib.mkEnableOption "Temporal process";

package = lib.mkOption {
type = types.package;
description = "Which package of Temporal to use.";
default = pkgs.temporal-cli;
defaultText = lib.literalExpression "pkgs.temporal-cli";
};

ip = lib.mkOption {
type = types.str;
default = "127.0.0.1";
description = "IPv4 address to bind the frontend service to.";
};

port = lib.mkOption {
type = types.port;
default = 7233;
description = "Port for the frontend gRPC service.";
};

ui = lib.mkOption {
type = types.submodule {
options = {
enable = lib.mkOption {
type = types.bool;
default = true;
description = "Enable the Web UI.";
};

ip = lib.mkOption {
type = types.str;
default = cfg.ip;
description = "IPv4 address to bind the Web UI to.";
};

port = lib.mkOption {
type = types.port;
default = cfg.port + 1000;
description = "Port for the Web UI.";
};
};
};
default = { };
description = "UI configuration.";
};

ephemeral = lib.mkOption {
type = types.bool;
default = true;
description = "When enabled, the Temporal state gets lost when the process exists.";
};

namespaces = lib.mkOption {
type = types.listOf types.str;
default = [ ];
description = "Specify namespaces that should be pre-created (namespace \"default\" is always created).";
example = lib.literalExpression ''
[
"my-namespace"
"my-other-namespace"
]
'';
};
};

config = lib.mkIf cfg.enable {
packages = [ cfg.package ];
processes.temporal.exec = "${cfg.package}/bin/temporal server start-dev ${lib.concatStringsSep " " commandArgs}";
};
}

0 comments on commit 539c18b

Please sign in to comment.