Skip to content

Commit

Permalink
Fix dont show logs if no changes (#49)
Browse files Browse the repository at this point in the history
* fix: types

* feat: added equal helper

* refactor: extracted TaskRunner types
fix: show log message only if there are changes.

* chore: fix linting

* fix: missing line break
  • Loading branch information
Sudakatux authored Sep 7, 2023
1 parent 90f8b3a commit 9ef9bb5
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 37 deletions.
18 changes: 12 additions & 6 deletions src/core/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
StartWorkflowRequest,
SkipTaskRequest,
WorkflowRun,
WorkflowStatus,
} from "../common/open-api";
import { TaskResultStatus } from "./types";
import { errorMapper,tryCatchReThrow } from "./helpers";
import { errorMapper, tryCatchReThrow } from "./helpers";

const RETRY_TIME_IN_MILLISECONDS = 10000;


export class WorkflowExecutor {
public readonly _client: ConductorClient;

Expand Down Expand Up @@ -52,10 +52,16 @@ export class WorkflowExecutor {
name: string,
version: number,
requestId: string,
waitUntilTaskRef: string = '',
waitUntilTaskRef: string = ""
): Promise<WorkflowRun> {
return tryCatchReThrow(() =>
this._client.workflowResource.executeWorkflow(workflowRequest, name, version, requestId, waitUntilTaskRef)
this._client.workflowResource.executeWorkflow(
workflowRequest,
name,
version,
requestId,
waitUntilTaskRef
)
);
}

Expand Down Expand Up @@ -111,7 +117,7 @@ export class WorkflowExecutor {
workflowInstanceId: string,
includeOutput: boolean,
includeVariables: boolean
) {
): Promise<WorkflowStatus> {
return tryCatchReThrow(() =>
this._client.workflowResource.getWorkflowStatusSummary(
workflowInstanceId,
Expand Down Expand Up @@ -141,7 +147,7 @@ export class WorkflowExecutor {
public reRun(
workflowInstanceId: string,
rerunWorkflowRequest: Partial<RerunWorkflowRequest> = {}
) {
): Promise<string> {
return tryCatchReThrow(() =>
this._client.workflowResource.rerun(
workflowInstanceId,
Expand Down
3 changes: 1 addition & 2 deletions src/task/TaskManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os from "os";
import {
TaskRunner,
TaskRunnerOptions,
TaskErrorHandler,
noopErrorHandler,
} from "./TaskRunner";
import { ConductorLogger, DefaultLogger } from "../common";
Expand All @@ -13,6 +11,7 @@ import {
DEFAULT_BATCH_POLLING_TIMEOUT,
DEFAULT_CONCURRENCY,
} from "./constants";
import { TaskErrorHandler, TaskRunnerOptions } from "./types";

export type TaskManagerOptions = TaskRunnerOptions;

Expand Down
39 changes: 14 additions & 25 deletions src/task/TaskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@ import {
DEFAULT_BATCH_POLLING_TIMEOUT,
DEFAULT_CONCURRENCY,
} from "./constants";
import { TaskErrorHandler, TaskRunnerOptions, RunnerArgs } from "./types";
import { optionEquals } from "./helpers";

const DEFAULT_ERROR_MESSAGE = "An unknown error occurred";
const MAX_RETRIES = 3;

export type TaskErrorHandler = (error: Error, task?: Task) => void;

export interface TaskRunnerOptions {
workerID: string;
domain: string | undefined;
pollInterval?: number;
concurrency?: number;
batchPollingTimeout?: number;
}
export interface RunnerArgs {
worker: ConductorWorker;
taskResource: TaskResourceService;
options: TaskRunnerOptions;
logger?: ConductorLogger;
onError?: TaskErrorHandler;
concurrency?: number;
}

//eslint-disable-next-line
export const noopErrorHandler: TaskErrorHandler = (__error: Error) => {};

Expand Down Expand Up @@ -103,13 +87,18 @@ export class TaskRunner {

updateOptions(options: Partial<TaskRunnerOptions>) {
const newOptions = { ...this.options, ...options };
this.poller.updateOptions({
concurrency: newOptions.concurrency,
pollInterval: newOptions.pollInterval,
});
this.logger.info(
`TaskWorker ${this.worker.taskDefName} configuration updated with concurrency of ${this.poller.options.concurrency} and poll interval of ${this.poller.options.pollInterval}`
);
const isOptionsUpdated = !optionEquals(this.options, newOptions);

if (isOptionsUpdated) {
this.poller.updateOptions({
concurrency: newOptions.concurrency,
pollInterval: newOptions.pollInterval,
});
this.logger.info(
`TaskWorker ${this.worker.taskDefName} configuration updated with concurrency of ${this.poller.options.concurrency} and poll interval of ${this.poller.options.pollInterval}`
);
}

this.options = newOptions;
}

Expand Down
3 changes: 2 additions & 1 deletion src/task/__tests__/TaskRunner.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { jest, test, expect } from "@jest/globals";
import type { Mocked } from "jest-mock";

import { RunnerArgs, TaskRunner } from "../TaskRunner";
import { TaskRunner } from "../TaskRunner";
import { RunnerArgs } from "../types";
import { mockLogger } from "./mockLogger";
import { TaskResourceService } from "../../common/open-api";

Expand Down
55 changes: 55 additions & 0 deletions src/task/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { expect, describe, it } from "@jest/globals";
import { optionEquals } from "../helpers";
import { TaskRunnerOptions } from "../types";

describe("helpers", () => {
it("Should return true if both options are equals", () => {
const someOptions: TaskRunnerOptions = {
workerID: "some-worker-id",
domain: "mydomain",
pollInterval: 1000,
concurrency: 1,
batchPollingTimeout: 1000,
};
const otherOptions: TaskRunnerOptions = {
...someOptions,
};
expect(optionEquals(someOptions, otherOptions)).toBeTruthy();

expect(optionEquals(otherOptions, someOptions)).toBeTruthy();
});

it("Should return true if both options are equals", () => {
const someOptions: TaskRunnerOptions = {
workerID: "some-worker-id",
domain: "mydomain",
pollInterval: 1000,
concurrency: 1,
batchPollingTimeout: 1000,
};
const otherOptions: TaskRunnerOptions = {
...someOptions,
batchPollingTimeout: 2000,
};
expect(optionEquals(someOptions, otherOptions)).not.toBeTruthy();

expect(optionEquals(otherOptions, someOptions)).not.toBeTruthy();
});
it("Should return false if options are only equal in some properties", () => {
const someOptions: TaskRunnerOptions = {
workerID: "some-worker-id",
domain: "mydomain",
pollInterval: 1000,
concurrency: 1,
batchPollingTimeout: 1000,
};

const someOptionsPrime: TaskRunnerOptions = {
workerID: "some-worker-id",
domain: "mydomain",
};

expect(optionEquals(someOptions, someOptionsPrime)).not.toBeTruthy();
expect(optionEquals(someOptionsPrime, someOptions)).not.toBeTruthy();
});
});
22 changes: 22 additions & 0 deletions src/task/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TaskRunnerOptions } from "./types";
type OptionEntries = Array<
[keyof TaskRunnerOptions, string | number | undefined]
>;


/**
* Compares if the new options are really new
* @param oldOptions
* @param newOptions
*/
export const optionEquals = (
oldOptions: Partial<TaskRunnerOptions>,
newOptions: Partial<TaskRunnerOptions>
) => {
const newOptionEntries = Object.entries(newOptions) as OptionEntries;
const oldOptionsEntries = Object.entries(oldOptions) as OptionEntries;

return newOptionEntries.length === oldOptionsEntries.length && newOptionEntries.every(
([key, value]) => (oldOptions[key] as unknown) === value
);
};
7 changes: 4 additions & 3 deletions src/task/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./TaskRunner"
export * from "./TaskManager"
export * from "./Worker"
export * from "./TaskRunner";
export * from "./TaskManager";
export * from "./Worker";
export * from "./types";
20 changes: 20 additions & 0 deletions src/task/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ConductorLogger } from "../common";
import type { ConductorWorker } from "./Worker";
import type { Task, TaskResourceService } from "../common/open-api";

export type TaskErrorHandler = (error: Error, task?: Task) => void;
export interface TaskRunnerOptions {
workerID: string;
domain: string | undefined;
pollInterval?: number;
concurrency?: number;
batchPollingTimeout?: number;
}
export interface RunnerArgs {
worker: ConductorWorker;
taskResource: TaskResourceService;
options: TaskRunnerOptions;
logger?: ConductorLogger;
onError?: TaskErrorHandler;
concurrency?: number;
}

0 comments on commit 9ef9bb5

Please sign in to comment.