Hosting your wisp
A wisp you run for real keeps its id and secret out of its code, keeps its place across restarts, stops cleanly when you deploy, and starts again on its own if it ever stops. A wisp in many servers runs one process for each slice (Many servers).
Keeping its secret
Section titled “Keeping its secret”On your own machine, in user secrets:
dotnet user-secrets set Wisp:Id <id>dotnet user-secrets set Wisp:Secret <secret>A host reads user secrets only in the Development environment, which dotnet run uses in a
project made with dotnet new worker. A console project needs dotnet add package Microsoft.Extensions.Hosting, using Microsoft.Extensions.Hosting;, dotnet user-secrets init,
and DOTNET_ENVIRONMENT=Development when you run it.
On a server, in the environment: Wisp__Id and Wisp__Secret.
On your own machine, in a .env file kept out of your repository:
WISP_ID=<id>WISP_SECRET=<secret>Node reads it when you start your wisp with it:
node --env-file=.env wisp.tsOn a server, in the environment: WISP_ID and WISP_SECRET.
Running it
Section titled “Running it”Start from the worker template and add the hosting package:
dotnet new worker -n Luckycd Luckydotnet add package Woosh.Wisp.Hostingusing Woosh.Wisp;
var builder = Host.CreateApplicationBuilder(args);var games = builder.Configuration.GetConnectionString("Game");builder.Services.AddDbContext<GameDb>(o => o.UseNpgsql(games));
builder.Services .AddWisp(o => o with { Activity = "Dealing blackjack", CursorFile = "wisp-cursor.json", }) .AddCommands<Gifts>() .Configure((wisp, services) => wisp.When<ServerJoined>() .Do(e => Console.WriteLine($"Added to {e.Name}")));
await builder.Build().RunAsync();What the host does:
- The connection follows the host. It opens when the host starts and closes cleanly when it stops, with your wisp’s place kept (Running well). A wrong or reset secret stops the host.
- Each run, press and form sending gets an instance of its own, made in a scope of the host’s services and disposed when it is done. A class of commands can take a database context, or any scoped service, through its constructor. Register the class as a singleton yourself to share one.
WispClientis a service. Inject it anywhere to post, such as from a timer that announces a draw. AnICursorStoreyou register is used for your wisp’s place.
Configuration:
| Key | |
|---|---|
Wisp:Id, Wisp:Secret |
who your wisp is, from developer.woosh.no |
Wisp:Activity |
its own words |
Wisp:CursorFile |
where it keeps its place between restarts |
Wisp:Slice |
which slice this process runs, for a wisp in many servers (Many servers) |
WISP_ID and WISP_SECRET work too, as they do without a host. Run the host as a service, or in a
container, so it starts again if it stops.
A wisp is a program that makes its Wisp, gives its handlers what they need, and starts it. Here
the slice comes from the environment too, so one program runs every slice:
import { Wisp } from "@woosh-no/wisp";import { connect } from "./db.ts";import { gifts } from "./gifts.ts";
const db = await connect(process.env.GAME_DB!);const slice = Number(process.env.WISP_SLICE ?? 0);
const wisp = new Wisp({ id: process.env.WISP_ID!, secret: process.env.WISP_SECRET!, slice, cursor: `wisp-cursor-${slice}.json`, activity: "Dealing blackjack",});gifts(wisp, db);wisp.on("serverJoined", (e) => console.log(`Added to ${e.name}`));
await wisp.start();await wisp.stopped; // until Ctrl+C, or a stop from a process manager or a containerawait db.close();- It stops cleanly on Ctrl+C and on the stop a process manager or a container sends (SIGINT and SIGTERM): handlers running finish, its place is kept, and the line closes.
- Run it under something that starts it again: a process manager such as systemd or pm2, or a container with a restart policy, with the id and secret in its environment.
- One process per slice, each started with its own
WISP_SLICE. - A wrong or reset secret ends it.
wisp.start()throws, or laterwisp.stoppedrejects with the reason, which ends the process when nothing handles it, so your process manager shows why. - Logs go to the console, each line starting with
wisp:; give theloggeroption your own.
How to keep a wisp quick, keep its place and deploy without a gap is on Running well.