Skip to content

Daemon templates ​

A daemon template defines a set of daemons once. Pitchfork runs one copy of the set for each of the template's instances. Use a template to run several copies of a stack side by side, such as one backend per tenant or several workers, each with its own API server.

Define a template ​

Put the daemons under [daemon_templates.<template>.daemons] and set instances:

toml
[daemons.redis]
run = "redis-server --port 6379"

[daemon_templates.workers]
instances = 2

[daemon_templates.workers.daemons.api]
run = "./api --port $PORT"
port = { expect = [3000], bump = 10 }

[daemon_templates.workers.daemons.worker]
run = "./worker --api http://127.0.0.1:{{ daemons.api.port }} --id {{ instance }}"
depends = ["api", "redis"]

This config defines these daemons and groups:

NameKindContains
redisdaemon
workers.0.api, workers.0.workerdaemonsinstance 0
workers.1.api, workers.1.workerdaemonsinstance 1
workers.0groupworkers.0.api, workers.0.worker
workers.1groupworkers.1.api, workers.1.worker
workersgroupthe daemons of every instance

The copies are ordinary daemons: start, stop, logs, status, the TUI and the web UI all work with them.

sh
pitchfork start --group workers.1   # one instance
pitchfork start --group workers     # every instance
pitchfork logs workers.0.worker

The template itself is not a daemon. A template without instances, or with an empty list, adds nothing.

Instances ​

instances is either a count or a list of names:

toml
[daemon_templates.workers]
instances = 3                     # instances 0, 1 and 2

[daemon_templates.tenants]
instances = ["acme", "globex"]    # instances acme and globex

A template can have at most 1000 instances. Template, instance and template daemon names may use ASCII letters, digits, _ and -, and may not start or end with - or contain --. Config parsing fails on other names, on an instance listed twice and on more instances than the limit.

Dependencies within an instance ​

In a copy's depends, the name of another daemon of the same template means that daemon's copy in the same instance. In the example, workers.1.worker depends on workers.1.api and on the regular redis daemon. A template daemon's name takes precedence over a regular daemon with the same name.

Templates in a copy use the same names, so {{ daemons.api.port }} in workers.1.worker is the port of workers.1.api. Once they have started, the other daemons of the instance are available by their template names too, including those the copy only depends on indirectly or not at all. A template name always means the copy: while workers.1.api has not started, {{ daemons.api.port }} fails to render in workers.1.worker even if a regular api daemon is running. A copy is also available under its full name, such as {{ daemons["workers.1.api"].port }}. An as name in depends that points to another template daemon also points to the copy in the same instance. An as name that is the name of another template daemon must point to that daemon; config parsing fails otherwise.

Template variables ​

Fields that accept templates can use two more variables in a copy:

VariableValueExample
{{ instance }}Instance name"1", "acme"
{{ instance_index }}Position in instances, from 01

Both are null in a daemon that is not a copy, so {% if instance %} works in any daemon.

Ports and local URLs ​

Each copy is configured with the same ports. Give a template daemon that listens on a port a bump range, as in the example, so each copy starts on the next free port. {{ daemons.api.port }} and $PORT have the port the copy started on. See port management for bump.

The reverse proxy names each copy from its full name, so workers.1.api gets a hostname such as workers-1-api.myproj.localhost. Dots become - in the hostname, so pick names that stay distinct: workers.blue.green-api and workers.blue-green.api both become workers-blue-green-api, and neither copy gets a hostname. Do not set a label with proxy = "<label>" in a template daemon: every copy would ask for the same label.

Configuration files ​

Copies are made after all config files are merged:

  • A later file of the same project can define a template with the same name. Its instances replaces the earlier value, and its daemons are added to the template or replace those with the same name. For example, a pitchfork.local.toml with instances = ["mine"] runs a single copy for you only.
  • A template from another project's config replaces the template.
  • A copy or group whose name is already defined, such as a regular daemon named workers.0.api or a group named workers, is a config error.

Commands that edit config files, such as pitchfork daemons add, keep [daemon_templates] as written and never write copies to the file.

Instances are fixed in the config: there is no command to add an instance while pitchfork runs.

MIT LicenseCopyright © 2026jdx.dev