Skip to content

Commit

Permalink
Dependency update. Fix docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Apr 24, 2024
1 parent bbe521e commit 5f95842
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
46 changes: 29 additions & 17 deletions docs/src/examples/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,69 @@ nav_order: 6
This example demonstrates the telemetry feature of pup, which ...

- Automatically report process metrics, such as memory usage, back to Pup.
- Opens a channel for managed processes to communicate using a slow polling IPC mechanism.
- Opens a channel for managed processes to communicate using a slow polling IPC
mechanism.

The simplest use case, where you only want to monitor your client metrics is used like this:
The simplest use case, where you only want to monitor your client metrics is
used like this:

```ts
import { PupTelemetry } from "jsr:@pup/telemetry"
import { PupTelemetry } from "jsr:@pup/telemetry";

new PupTelemetry()
new PupTelemetry();

// The rest of your application
```

This will make your process report memory usage and current working directory back to the Pup main process, no further configuration needed. Now you can see memory usage for all processes running
telemetry (including main process) using `status` on the cli.
This will make your process report memory usage and current working directory
back to the Pup main process, no further configuration needed. Now you can see
memory usage for all processes running telemetry (including main process) using
`status` on the cli.

**To use the IPC mechanism:**

```ts
// PupTelemetry is a singleton, so it can be imported one or many times in your application
import { PupTelemetry } from "jsr:@pup/telemetry"
const telemetry = new PupTelemetry()
// - Installation instructions for different runtimes available at https://jsr.io/@pup/telemetry
// This example imports directly from jsr.io using Deno jsr:-specifier
import { PupTelemetry } from "jsr:@pup/telemetry";
const telemetry = new PupTelemetry();

// One part of your application ...

// Listen for ipc events
telemetry.on("my-event", (data) => {
console.log(
`Another process triggered 'my-event' with data ${JSON.stringify(data)}`,
)
})
);
});

// Send ipc events
telemetry.emit("another-process-id", "my-event", { data: { to: "send" } })
telemetry.emit("another-process-id", "my-event", { data: { to: "send" } });

// ... another part of your application
```

## Files

- [pup.json](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/pup.json) - Pup configuration, sets up `task-with-telemetry.ts` to run forever. `server.ts` to be kept alive forever.
- [task-with-telemetry-1.ts](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/task-with-telemetry-1.ts) - "task-1", script sending telemetry data to main process, and sending
messages over IPC to task-2
- [task-with-telemetry-2.ts](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/task-with-telemetry-2.ts) - "task-2", script sending telemetry data to main process, and receiving
- [pup.json](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/pup.json) -
Pup configuration, sets up `task-with-telemetry.ts` to run forever.
`server.ts` to be kept alive forever.
- [task-with-telemetry-1.ts](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/task-with-telemetry-1.ts) -
"task-1", script sending telemetry data to main process, and sending messages
over IPC to task-2
- [task-with-telemetry-2.ts](https://github.com/Hexagon/pup/tree/main/docs/src/examples/telemetry/task-with-telemetry-2.ts) -
"task-2", script sending telemetry data to main process, and receiving
messages over IPC from task-1

## Running

`cd` to `docs/examples/telemetry` directory.

Start example by running `pup run` if pup is installed, or something like `deno run -A ../../../pup.ts run` if not.
Start example by running `pup run` if pup is installed, or something like
`deno run -A ../../../pup.ts run` if not.

Now open another terminal and issue `pup status`, a brief overview of current status is shown, including memory usage.
Now open another terminal and issue `pup status`, a brief overview of current
status is shown, including memory usage.

Success!
18 changes: 10 additions & 8 deletions docs/src/examples/telemetry/task-with-telemetry-1.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// See docs/examples/telemetry/README.md for full documentation on telemetry, including using the IPC
// - Pin this to the latest version of pup, or include in import map
import { PupTelemetry } from "jsr:@pup/pup-telemetry"
const telemetry = new PupTelemetry(1)
import { PupTelemetry } from "jsr:@pup/telemetry";
const telemetry = new PupTelemetry(1);

// The task
let i = 0
console.log("Task running")
let i = 0;
console.log("Task running");

// Send data every 5th second
setInterval(() => {
i += 1
i += 1;
telemetry.emit(
"task-2",
"message",
`${Deno.env.get("PUP_PROCESS_ID")} sending "Hello" to 'task-2', iteration ${i}`,
)
}, 2000)
`${
Deno.env.get("PUP_PROCESS_ID")
} sending "Hello" to 'task-2', iteration ${i}`,
);
}, 2000);
14 changes: 7 additions & 7 deletions docs/src/examples/telemetry/task-with-telemetry-2.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// See docs/examples/telemetry/README.md for full documentation on telemetry, including using the IPC
// - Pin this to the latest version of pup, or include in import map
import { PupTelemetry } from "jsr:@pup/telemetry"
const telemetry = new PupTelemetry(1)
import { PupTelemetry } from "jsr:@pup/telemetry";
const telemetry = new PupTelemetry(1);

// The task
console.log("Process running")
console.log("Process running");

// Receive data
// deno-lint-ignore no-explicit-any
telemetry.on("message", (data: any) => {
console.log(`task-2 received: ${data}`)
})
console.log(`task-2 received: ${data}`);
});

// Wait 5 minutes
setTimeout(() => {
console.log("Done!")
}, 300_000)
console.log("Done!");
}, 300_000);

0 comments on commit 5f95842

Please sign in to comment.