/** * @license * Copyright 2023 Open Ag Data Alliance * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import "@oada/pino-debug"; import type { Config } from "@oada/client"; import { OADAClient } from "@oada/client"; import { Gauge, Histogram } from "@oada/lib-prom"; import { type Logger } from "@oada/pino-debug"; import type { Json } from "./index.js"; import type { Job } from "./Job.js"; import { Report, type ReportConstructor } from "./Report.js"; export type Domain = string; export type Type = string; export type QueueId = string; export type JobId = string; export interface WorkerContext { jobId: string; log: Logger; oada: OADAClient; } export type WorkerFunction = (job: Job, context: WorkerContext) => Promise; export interface Worker { timeout: number; work: WorkerFunction; } export interface FinishReporter { type: "slack"; status: "success" | "failure"; posturl?: string; } export interface FinishReporters extends Array { } export interface ServiceOptions { finishReporters?: FinishReporters; skipQueueOnStartup?: boolean; } export interface ConstructorArguments { name: string; oada?: OADAClient | Config; concurrency?: number; opts?: ServiceOptions; log?: Logger; } export declare const defaultServiceQueueName = "default-service-queue"; /** * Manages an @oada/jobs based service's queue endpoints. This Service class * manages: * 1. Watching a configurable list of Job queues, * 2. Calling worker functions based on job types, * 3. Updating and filing job objects based on the worker's output. */ export declare class Service { #private; name: string; concurrency: number; domain: string; token: string; opts: ServiceOptions | undefined; metrics: { jobs: Gauge<"type" | "service" | "state">; "job-times": Histogram<"status" | "type" | "service">; }; log: Logger; /** * Creates a Service. Two possible call signatures: * - new Service(name, domain, token, concurrency, opts?) * - new Service(name, oada, concurrency, opts?) * @param name Name of service * @param oada Either an existing OADAClient or a connection object used to call oada.connect * @param opts ServiceOpts (finish reporter, etc) */ constructor(object: ConstructorArguments); /** * Add a report to the service. See ReportConstructor for parameters. */ addReport(rc: Omit): Report; /** * Get a registered report * @param name The name of the report */ getReport(name: string): Report | undefined; start(): Promise; stop(): Promise; /** * Register a worker for a job type. * @param type Type of job the worker is for * @param timeout Max worker runtime in ms * @param worker Worker function */ on(type: string, timeout: number, work: WorkerFunction): void; /** * De-register a worker for a job type. * @param type Type of job to de-register worker for */ off(type: string): void; /** * Fetch the registered worker for a job type * @param type Type of job */ getWorker(type: string): Worker; /** * Obtain an OADAClient by domain, creating if needed */ getClient(): OADAClient; }