-
Notifications
You must be signed in to change notification settings - Fork 14
Add external error handler #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,97 @@ | ||
| import os from "os" | ||
| import {TaskRunner} from "./TaskRunner" | ||
| import {ConductorLogger, DefaultLogger} from "../common" | ||
| import {ConductorWorker} from "./Worker" | ||
| import {ConductorClient} from "../common/open-api" | ||
| import os from "os"; | ||
| import { TaskRunner, TaskErrorHandler, noopErrorHandler } from "./TaskRunner"; | ||
| import { ConductorLogger, DefaultLogger } from "../common"; | ||
| import { ConductorWorker } from "./Worker"; | ||
| import { ConductorClient } from "../common/open-api"; | ||
|
|
||
| export interface TaskManagerOptions { | ||
| workerID: string | ||
| domain: string | undefined | ||
| pollInterval?: number, | ||
| concurrency?: number | ||
| workerID: string; | ||
| domain: string | undefined; | ||
| pollInterval?: number; | ||
| concurrency?: number; | ||
| } | ||
|
|
||
| export interface TaskManagerConfig { | ||
| logger?: ConductorLogger | ||
| options?: Partial<TaskManagerOptions> | ||
| logger?: ConductorLogger; | ||
| options?: Partial<TaskManagerOptions>; | ||
| onError?: TaskErrorHandler; | ||
| } | ||
|
|
||
| const defaultManagerOptions: Required<TaskManagerOptions> = { | ||
| workerID: '', | ||
| workerID: "", | ||
| pollInterval: 1000, | ||
| domain: undefined, | ||
| concurrency: 1 | ||
| } | ||
| concurrency: 1, | ||
| }; | ||
|
|
||
| function workerId (options: Partial<TaskManagerOptions>) { | ||
| return options.workerID ?? os.hostname() | ||
| function workerId(options: Partial<TaskManagerOptions>) { | ||
| return options.workerID ?? os.hostname(); | ||
| } | ||
|
|
||
| /** | ||
| * Responsible for initializing and managing the runners that poll and work different task queues. | ||
| */ | ||
| export class TaskManager { | ||
| private tasks: Record<string, Array<TaskRunner>> = {} | ||
| private readonly client: ConductorClient | ||
| private readonly logger: ConductorLogger | ||
| private workers: Array<ConductorWorker> | ||
| private readonly taskManageOptions: Required<TaskManagerOptions> | ||
| private tasks: Record<string, Array<TaskRunner>> = {}; | ||
| private readonly client: ConductorClient; | ||
| private readonly logger: ConductorLogger; | ||
| private readonly errorHandler: TaskErrorHandler; | ||
| private workers: Array<ConductorWorker>; | ||
| private readonly taskManageOptions: Required<TaskManagerOptions>; | ||
|
|
||
| constructor(client: ConductorClient, workers: Array<ConductorWorker>, config: TaskManagerConfig = {}) { | ||
| if (!workers) { throw new Error("No workers supplied to TaskManager. Please pass an array of workers.") } | ||
| this.client = client | ||
| this.logger = config.logger ?? new DefaultLogger() | ||
| this.workers = workers | ||
| const providedOptions = config.options ?? {} | ||
| constructor( | ||
| client: ConductorClient, | ||
| workers: Array<ConductorWorker>, | ||
| config: TaskManagerConfig = {} | ||
| ) { | ||
| if (!workers) { | ||
| throw new Error( | ||
| "No workers supplied to TaskManager. Please pass an array of workers." | ||
| ); | ||
| } | ||
| this.client = client; | ||
| this.logger = config.logger ?? new DefaultLogger(); | ||
| this.errorHandler = config.onError ?? noopErrorHandler; | ||
| this.workers = workers; | ||
| const providedOptions = config.options ?? {}; | ||
| this.taskManageOptions = { | ||
| ...defaultManagerOptions, | ||
| ...providedOptions, | ||
| workerID: workerId(providedOptions), | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| startPolling = () => { | ||
| this.workers.forEach(worker => { | ||
| this.tasks[worker.taskDefName] = [] | ||
| this.workers.forEach((worker) => { | ||
| this.tasks[worker.taskDefName] = []; | ||
| const options = { | ||
| ...this.taskManageOptions, | ||
| concurrency: worker.concurrency ?? this.taskManageOptions.concurrency, | ||
| domain: worker.domain ?? this.taskManageOptions.domain | ||
| } | ||
| this.logger.debug(`Starting taskDefName=${worker.taskDefName} concurrency=${options.concurrency} domain=${options.domain}`) | ||
| domain: worker.domain ?? this.taskManageOptions.domain, | ||
| }; | ||
| this.logger.debug( | ||
| `Starting taskDefName=${worker.taskDefName} concurrency=${options.concurrency} domain=${options.domain}` | ||
| ); | ||
| for (let i = 0; i < options.concurrency; i++) { | ||
| const runner = new TaskRunner({ | ||
| worker, | ||
| options, | ||
| taskResource: this.client.taskResource, | ||
| logger: this.logger | ||
| }) | ||
| logger: this.logger, | ||
| onError: this.errorHandler, | ||
| }); | ||
| // TODO(@ntomlin): right now we aren't handling these promises | ||
| // which will inevitably lead to chaos | ||
| runner.startPolling() | ||
| this.tasks[worker.taskDefName].push(runner) | ||
| runner.startPolling(); | ||
| this.tasks[worker.taskDefName].push(runner); | ||
| } | ||
| }) | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| stopPolling = () => { | ||
| for (const taskType in this.tasks) { | ||
| this.tasks[taskType].forEach(runner => runner.stopPolling()) | ||
| this.tasks[taskType] = [] | ||
| this.tasks[taskType].forEach((runner) => runner.stopPolling()); | ||
| this.tasks[taskType] = []; | ||
| } | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { expect, describe, test, jest } from "@jest/globals"; | ||
| import { OrkesApiConfig, orkesConductorClient } from "../../common"; | ||
| import { WorkflowExecutor } from "../../core"; | ||
| import { TaskManager } from "../TaskManager"; | ||
| import { ConductorWorker } from "../Worker"; | ||
|
|
||
| const playConfig: Partial<OrkesApiConfig> = { | ||
| keyId: `${process.env.KEY_ID}`, | ||
| keySecret: `${process.env.KEY_SECRET}`, | ||
| serverUrl: "https://pg-staging.orkesconductor.com/api", | ||
| }; | ||
|
|
||
| describe("TaskManager", () => { | ||
| const clientPromise = orkesConductorClient(playConfig); | ||
|
|
||
| jest.setTimeout(10000); | ||
| test("Should run workflow with worker", async () => { | ||
| const client = await clientPromise; | ||
| const executor = new WorkflowExecutor(client); | ||
|
|
||
| const worker: ConductorWorker = { | ||
| taskDefName: "taskmanager-test", | ||
| execute: async () => { | ||
| return { | ||
| outputData: { | ||
| hello: "From your worker", | ||
| }, | ||
| status: "COMPLETED", | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| const manager = new TaskManager(client, [worker]); | ||
| manager.startPolling(); | ||
|
|
||
| const executionId = await executor.startWorkflow({ | ||
| name: "TaskManagerTest", | ||
| input: {}, | ||
| version: 1, | ||
| }); | ||
| await new Promise((r) => setTimeout(() => r(true), 2500)); | ||
| const workflowStatus = await client.workflowResource.getExecutionStatus( | ||
| executionId, | ||
| true | ||
| ); | ||
| expect(workflowStatus.status).toEqual("COMPLETED"); | ||
| manager.stopPolling(); | ||
| }); | ||
|
|
||
| test("On error it should call the errorHandler provided", async () => { | ||
| const client = await clientPromise; | ||
| const executor = new WorkflowExecutor(client); | ||
|
|
||
| const worker: ConductorWorker = { | ||
| taskDefName: "taskmanager-test", | ||
| execute: async () => { | ||
| throw Error("This is a forced error"); | ||
| }, | ||
| }; | ||
|
|
||
| const errorHandler = jest.fn(); | ||
|
|
||
| const manager = new TaskManager(client, [worker], { | ||
| onError: errorHandler, | ||
| }); | ||
|
|
||
| manager.startPolling(); | ||
|
|
||
| await executor.startWorkflow({ | ||
| name: "TaskManagerTest", | ||
| input: {}, | ||
| version: 1, | ||
| }); | ||
| await new Promise((r) => setTimeout(() => r(true), 3500)); | ||
| expect(errorHandler).toBeCalledTimes(1); | ||
| manager.stopPolling(); | ||
| }); | ||
|
|
||
| test("If no error handler provided. it should just update the task", async () => { | ||
| const client = await clientPromise; | ||
| const executor = new WorkflowExecutor(client); | ||
|
|
||
| const worker: ConductorWorker = { | ||
| taskDefName: "taskmanager-test", | ||
| execute: async () => { | ||
| throw Error("This is a forced error"); | ||
| }, | ||
| }; | ||
|
|
||
| const manager = new TaskManager(client, [worker]); | ||
|
|
||
| manager.startPolling(); | ||
|
|
||
| await executor.startWorkflow({ | ||
| name: "TaskManagerTest", | ||
| input: {}, | ||
| version: 1, | ||
| }); | ||
| await new Promise((r) => setTimeout(() => r(true), 3500)); | ||
| manager.stopPolling(); | ||
| }); | ||
| }); | ||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.