Skip to content

Commit

Permalink
ping reporter
Browse files Browse the repository at this point in the history
traviskuhl committed Oct 29, 2024
1 parent 28163e5 commit 1f9f8f8
Showing 18 changed files with 132 additions and 26 deletions.
1 change: 1 addition & 0 deletions bin/run.ts
Original file line number Diff line number Diff line change
@@ -31,4 +31,5 @@ await main({
verbose,
reportFile: report,
remoteUrl,
include: [],
});
6 changes: 5 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
"ffr": "deno run -A --unstable-worker-options ./bin/ffr.ts"
},
"exclude": [
"./www"
"./www"
],
"compilerOptions": {
"strict": true,
@@ -34,6 +34,10 @@
"noUncheckedIndexedAccess": true
},
"lint": {
"include": [
"src/**/*.ts",
"!src/schema/*.ts"
],
"exclude": [
"tmp*"
]
17 changes: 12 additions & 5 deletions src/cli/ffr/execute.ts
Original file line number Diff line number Diff line change
@@ -70,6 +70,12 @@ export default async function main(ctx: FFrCliContext) {

const foundFiles: Array<{ name: string; size: number }> = [];

const spin = new Spinner({
message: "Building run manifest...",
});

spin.start();

// make sure all of the files exist
// if there are any missing files, push
// them to the missing array so we can
@@ -87,6 +93,7 @@ export default async function main(ctx: FFrCliContext) {
size: f.size,
});
} catch (_) {
spin.stop();
const value = await confirm(
`Unable to open file "${inputPath}" (${p}). Would you like to ignore this file?`,
{
@@ -99,9 +106,13 @@ export default async function main(ctx: FFrCliContext) {
Deno.exit(1);
}
foundFiles.push({ name: inputPath, size: 0 });
} finally {
spin.start();
}
}

spin.message = "Sending build manifest for compilation...";

// we need to get sts federated token
// that is tied to this user's storage bucket
// we'll also let them know what files are
@@ -123,11 +134,7 @@ export default async function main(ctx: FFrCliContext) {

assert(response.config, "Missing response config");

const spin = new Spinner({
message: "Preparing to upload files...",
});

spin.start();
spin.message = "Preparing to upload files...";

try {
// create our sts client
2 changes: 2 additions & 0 deletions src/cli/ffr/watch.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,8 @@ export default async function main(ctx: FFrCliContext) {

let spin: Spinner | undefined;

console.log(status);

if (status === "pending") {
spin = new Spinner({
message: "Waiting for worker initialization...",
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ export enum ReporterName {
Console = "console",
File = "file",
Supabase = "supabase",
Ping = "ping",
}

export enum LaunchMode {
2 changes: 1 addition & 1 deletion src/launch/worker.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import {
evaluateExpression,
isExpressionResultTruthy,
} from "../libs/expression/expression.ts";
import { LaunchWorkerOptions } from "../types.ts";
import type { LaunchWorkerOptions } from "../types.ts";

type PossibleConfigurationFormat = {
format: "yaml";
4 changes: 4 additions & 0 deletions src/reporters/create.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { ReporterName } from "../constants.ts";
import { FileReporter } from "./file.ts";
import { SupabaseReporter } from "./supabase.ts";
import { ConsoleReporter } from "./console.ts";
import { PingReporter } from "./ping.ts";

export function createReporter(
name: string,
@@ -18,6 +19,9 @@ export function createReporter(
case ReporterName.Console:
return new ConsoleReporter();

case ReporterName.Ping:
return new PingReporter();

default: {
throw new Error(`Unknown reporter: ${name}`);
}
68 changes: 68 additions & 0 deletions src/reporters/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { AbstractReporter } from "./abstract.ts";
import type { ReporterChangeData, Workflow } from "../types.ts";
import { assert } from "../deps.ts";

export interface PingReporterOptions {
url: string;
interval?: number;
}

export class PingReporter extends AbstractReporter<PingReporterOptions> {
#lock = false;
#pingInterval: number | null = null;

override setOptions(options: PingReporterOptions) {
super.setOptions(options);

assert(options.url, "Ping URL is required for PingReporter");

this._startPingInterval();
}

override async destroy() {
await Promise.resolve(() => this._stopPingInterval());
}

_startPingInterval() {
this._stopPingInterval();
this.#pingInterval = setInterval(() => {
this._ping();
}, 1000 * (this.options.interval ?? 60));
}

_stopPingInterval() {
this.#pingInterval && clearInterval(this.#pingInterval);
}

async execute() {
}

async report(
_report: Workflow.Report,
_configuration?: Workflow.Configuration,
): Promise<void> {
}

async change(_type: string, _data: ReporterChangeData): Promise<void> {
}

async _ping(): Promise<void> {
if (this.#lock) {
return;
}

try {
await fetch(this.options.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(new Date().getTime().toString()),
});
} catch (error) {
console.error("Error pinging", error);
} finally {
this.#lock = false;
}
}
}
7 changes: 3 additions & 4 deletions src/reporters/supabase.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ export interface SupabaseReporterOptions {
url: string;
anon_key: string;
service_key: string;
interval?: number;
}

type Client = supabase.SupabaseClient<Json, "public">;
@@ -62,9 +63,7 @@ export class SupabaseReporter
},
});

this.#changeQueueInterval = setInterval(() => {
this._flush();
}, 1000 * 60 * 1);
this._startChangeQueue();
}

override async destroy() {
@@ -76,7 +75,7 @@ export class SupabaseReporter
this._stopChangeQueue();
this.#changeQueueInterval = setInterval(() => {
this._flush();
}, 1000 * 60 * 1);
}, 1000 * (this.options.interval ?? 30));
}

_stopChangeQueue() {
4 changes: 2 additions & 2 deletions src/runtime/execution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert, dirname } from "../deps.ts";

import { Manager } from "./manager.ts";
import type { Manager } from "./manager.ts";
import type { JsonObject, ReporterChangeData, Workflow } from "../types.ts";
import { Job } from "./job.ts";
import { executeDenoCommand } from "../libs/deno/execute.ts";
@@ -10,7 +10,7 @@ import {
executeDenoRun,
type ExecuteDenoRunOptions,
} from "../libs/deno/execute.ts";
import { Folder } from "./folder.ts";
import type { Folder } from "./folder.ts";
import { RunnerResult, StateName } from "../constants.ts";
import { evaluateWhen } from "../libs/expression/when.ts";
import { asError } from "../libs/utils.ts";
3 changes: 2 additions & 1 deletion src/runtime/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// deno-lint-ignore-file require-await
import { RunnerResult, RunnerStatus, StateName } from "../constants.ts";
import type {
CombinedRuntimeState,
ReporterChangeData,
Result,
RuntimeState,
@@ -64,7 +65,7 @@ export abstract class State implements RuntimeState {
return this._data[name] ?? defaultValue;
}

getCombinedState() {
getCombinedState(): CombinedRuntimeState {
return {
id: this.id,
name: this.name,
8 changes: 6 additions & 2 deletions src/runtime/step.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { Job } from "./job.ts";
import type { ReporterChangeData, Workflow } from "../types.ts";
import type {
CombinedRuntimeState,
ReporterChangeData,
Workflow,
} from "../types.ts";
import {
resolveActionUrlForDenoCommand,
resolveActionUrlFromDefinition,
@@ -48,7 +52,7 @@ export class Step extends State {
return this.job.logger;
}

override getCombinedState() {
override getCombinedState(): CombinedRuntimeState {
return {
...super.getCombinedState(),
definition: this.def,
2 changes: 2 additions & 0 deletions src/schema/job.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// deno-lint-ignore-file

import { z } from "../deps.ts";

import { Description, Label, Name, When } from "./scalar.ts";
2 changes: 2 additions & 0 deletions src/schema/launch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// deno-lint-ignore-file

import { z } from "../deps.ts";
import { WorkflowSchema } from "./workflow.ts";

2 changes: 2 additions & 0 deletions src/schema/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// deno-lint-ignore-file

import { z } from "../deps.ts";

/**
2 changes: 2 additions & 0 deletions src/schema/step.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// deno-lint-ignore-file

import { z } from "../deps.ts";

import {
2 changes: 2 additions & 0 deletions src/schema/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// deno-lint-ignore-file

import { z } from "../deps.ts";
import { JobSchema } from "./job.ts";
import {
25 changes: 15 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// deno-lint-ignore-file no-namespace

import { z } from "./deps.ts";
import { type WorkflowSchema } from "./schema/workflow.ts";
import { type JobSchema } from "./schema/job.ts";
import {
type ExecuteSchema,
type ExecuteWithWorkflowFileSchema,
type ExecuteWithWorkflowSchema,
type LaunchSchema,
type ServerSchema,
type WorkerSchema,
import type { z } from "./deps.ts";
import type { WorkflowSchema } from "./schema/workflow.ts";
import type { JobSchema } from "./schema/job.ts";
import type {
ExecuteSchema,
ExecuteWithWorkflowFileSchema,
ExecuteWithWorkflowSchema,
LaunchSchema,
ServerSchema,
WorkerSchema,
} from "./schema/launch.ts";
import type * as step from "./schema/step.ts";
import type * as scalar from "./schema/scalar.ts";
@@ -51,6 +51,11 @@ export interface RuntimeState {
};
}

export type CombinedRuntimeState = RuntimeState["state"] & {
id: string;
name: string;
};

export namespace Workflow {
export type Configuration = z.infer<typeof WorkflowSchema>;

0 comments on commit 1f9f8f8

Please sign in to comment.