Skip to content

Commit

Permalink
Merge pull request #633 from sagikazarmark/add-temporal-service
Browse files Browse the repository at this point in the history
temporal: add temporal service
  • Loading branch information
domenkozar committed Jun 18, 2023
2 parents fda406a + 1fc5976 commit 9284e05
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
42 changes: 42 additions & 0 deletions examples/temporal/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh
set -x

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

export TEMPORAL_ADDRESS=127.0.0.1:17233

# 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
17 changes: 17 additions & 0 deletions examples/temporal/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ pkgs, ... }:
{
services.temporal = {
enable = true;

port = 17233;

namespaces = [ "mynamespace" ];

state = {
ephemeral = false;
sqlite-pragma = {
journal_mode = "wal";
};
};
};
}
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
110 changes: 110 additions & 0 deletions src/modules/services/temporal.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{ 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.state.ephemeral) [ "--db-filename=${databaseFile}" ]) ++
(lib.mapAttrsToList (name: value: "--sqlite-pragma ${name}=${value}") cfg.state.sqlite-pragma);
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;
defaultText = lib.literalMD "[`services.temporal.port`](#servicestemporalport) + 1000";
description = "Port for the Web UI.";
};
};
};
default = { };
description = "UI configuration.";
};

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

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

sqlite-pragma = lib.mkOption {
type = types.attrsOf types.str;
default = { };
description = "Sqlite pragma statements";
example = {
journal_mode = "wal";
synchronous = "2";
};
};
};
};
default = { };
description = "State configuration.";
};
};

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 9284e05

Please sign in to comment.