Skip to content

Commit

Permalink
replace any with unknown in task manager
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Apr 22, 2020
1 parent bf81639 commit 78d11a7
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@ module.exports = {
*/
{
// typescript only for front and back end
files: ['x-pack/{,legacy/}plugins/{alerting,alerting_builtins,actions}/**/*.{ts,tsx}'],
files: [
'x-pack/{,legacy/}plugins/{alerting,alerting_builtins,actions,task_manager}/**/*.{ts,tsx}',
],
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/task_manager/server/create_task_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
} from '../../../../src/core/server';
import { TaskManager } from './task_manager';
import { Logger } from './types';
import { TaskManagerConfig } from './config';

export interface LegacyDeps {
config: any;
config: unknown;
elasticsearch: Pick<IClusterClient, 'callAsInternalUser'>;
savedObjectsRepository: ISavedObjectsRepository;
savedObjectsSerializer: SavedObjectsSerializer;
Expand All @@ -33,7 +34,7 @@ export function createTaskManager(
) {
return new TaskManager({
taskManagerId: core.uuid.getInstanceUuid(),
config,
config: config as TaskManagerConfig,
savedObjectsRepository,
serializer: savedObjectsSerializer,
callAsInternalUser,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/task_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TaskManagerPlugin
this.currentConfig = {} as TaskManagerConfig;
}

public setup(core: CoreSetup, plugins: any): TaskManagerSetupContract {
public setup(core: CoreSetup, plugins: unknown): TaskManagerSetupContract {
const logger = this.initContext.logger.get('taskManager');
const config$ = this.initContext.config.create<TaskManagerConfig>();
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const IdleTaskWithExpiredRunAt: MustCondition<TermFilter | RangeFilter> =
};

// TODO: Fix query clauses to support this
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const InactiveTasks: BoolClauseWithAnyCondition<any> = {
bool: {
must_not: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type AnyBoolCondition<T extends BoolClauseFilter> = {
};

/**
* Describe a Bool Condition with any Condition on it, so it can handle both:
* Describe a Bool Condition with unknown Condition on it, so it can handle both:
* ```
* {
* bool: {
Expand Down
34 changes: 29 additions & 5 deletions x-pack/plugins/task_manager/server/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type Require<T extends object, P extends keyof T> = Omit<T, P> & Required<Pick<T
* A loosely typed definition of the elasticjs wrapper. It's beyond the scope
* of this work to try to make a comprehensive type definition of this.
*/
export type ElasticJs = (action: string, args: any) => Promise<any>;
// we have a wrapper around callCluster that makes this impossible to type correctly
// TODO: this should be replaced with the regulakr callCluster
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ElasticJs = (action: string, args: unknown) => Promise<unknown>;

/**
* The run context is passed into a task's run function as its sole argument.
Expand Down Expand Up @@ -61,12 +64,12 @@ export interface RunResult {
* The state which will be passed to the next run of this task (if this is a
* recurring task). See the RunContext type definition for more details.
*/
state: Record<string, any>;
state: Record<string, unknown>;
}

export interface SuccessfulRunResult {
runAt?: Date;
state?: Record<string, any>;
state?: Record<string, unknown>;
}

export interface FailedRunResult extends SuccessfulRunResult {
Expand Down Expand Up @@ -237,13 +240,19 @@ export interface TaskInstance {
* A task-specific set of parameters, used by the task's run function to tailor
* its work. This is generally user-input, such as { sms: '333-444-2222' }.
*/
// we allow any here as unknown will break current use in otehr plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params: Record<string, any>;

/**
* The state passed into the task's run function, and returned by the previous
* run. If there was no previous run, or if the previous run did not return
* any state, this will be the empy object: {}
* unknown state, this will be the empy object: {}
*/
// we allow any here as unknown will break current use in otehr plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: Record<string, any>;

/**
Expand Down Expand Up @@ -334,12 +343,27 @@ export interface ConcreteTaskInstance extends TaskInstance {
/**
* The state passed into the task's run function, and returned by the previous
* run. If there was no previous run, or if the previous run did not return
* any state, this will be the empy object: {}
* unknown state, this will be the empy object: {}
*/
// we allow any here as unknown will break current use in otehr plugins
// this can be fixed by supporting generics in the future
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: Record<string, any>;

/**
* The random uuid of the Kibana instance which claimed ownership of the task last
*/
ownerId: string | null;
}

export type SerializedConcreteTaskInstance = Omit<
ConcreteTaskInstance,
'state' | 'params' | 'scheduledAt' | 'startedAt' | 'retryAt' | 'runAt'
> & {
state: string;
params: string;
scheduledAt: string;
startedAt: string | null;
retryAt: string | null;
runAt: string;
};
10 changes: 6 additions & 4 deletions x-pack/plugins/task_manager/server/task_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ export function asTaskRunRequestEvent(
}

export function isTaskMarkRunningEvent(
taskEvent: TaskEvent<any, any>
taskEvent: TaskEvent<unknown, unknown>
): taskEvent is TaskMarkRunning {
return taskEvent.type === TaskEventType.TASK_MARK_RUNNING;
}
export function isTaskRunEvent(taskEvent: TaskEvent<any, any>): taskEvent is TaskRun {
export function isTaskRunEvent(taskEvent: TaskEvent<unknown, unknown>): taskEvent is TaskRun {
return taskEvent.type === TaskEventType.TASK_RUN;
}
export function isTaskClaimEvent(taskEvent: TaskEvent<any, any>): taskEvent is TaskClaim {
export function isTaskClaimEvent(taskEvent: TaskEvent<unknown, unknown>): taskEvent is TaskClaim {
return taskEvent.type === TaskEventType.TASK_CLAIM;
}
export function isTaskRunRequestEvent(taskEvent: TaskEvent<any, any>): taskEvent is TaskRunRequest {
export function isTaskRunRequestEvent(
taskEvent: TaskEvent<unknown, unknown>
): taskEvent is TaskRunRequest {
return taskEvent.type === TaskEventType.TASK_RUN_REQUEST;
}
17 changes: 9 additions & 8 deletions x-pack/plugins/task_manager/server/task_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SavedObjectsSerializer, SavedObjectTypeRegistry } from '../../../../src
import { mockLogger } from './test_utils';
import { asErr, asOk } from './lib/result_type';
import { ConcreteTaskInstance, TaskLifecycleResult, TaskStatus } from './task';
import { Middleware } from './lib/middleware';

const savedObjectsClient = savedObjectsRepositoryMock.create();
const serializer = new SavedObjectsSerializer(new SavedObjectTypeRegistry());
Expand Down Expand Up @@ -247,20 +248,20 @@ describe('TaskManager', () => {

test('allows middleware registration before starting', () => {
const client = new TaskManager(taskManagerOpts);
const middleware = {
beforeSave: async (saveOpts: any) => saveOpts,
beforeRun: async (runOpts: any) => runOpts,
beforeMarkRunning: async (runOpts: any) => runOpts,
const middleware: Middleware = {
beforeSave: jest.fn(async saveOpts => saveOpts),
beforeRun: jest.fn(async runOpts => runOpts),
beforeMarkRunning: jest.fn(async runOpts => runOpts),
};
expect(() => client.addMiddleware(middleware)).not.toThrow();
});

test('disallows middleware registration after starting', async () => {
const client = new TaskManager(taskManagerOpts);
const middleware = {
beforeSave: async (saveOpts: any) => saveOpts,
beforeRun: async (runOpts: any) => runOpts,
beforeMarkRunning: async (runOpts: any) => runOpts,
const middleware: Middleware = {
beforeSave: jest.fn(async saveOpts => saveOpts),
beforeRun: jest.fn(async runOpts => runOpts),
beforeMarkRunning: jest.fn(async runOpts => runOpts),
};

client.start();
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/task_manager/server/task_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
TaskLifecycle,
TaskLifecycleResult,
TaskStatus,
ElasticJs,
} from './task';
import { createTaskPoller, PollingError, PollingErrorType } from './task_poller';
import { TaskPool } from './task_pool';
Expand Down Expand Up @@ -109,7 +110,7 @@ export class TaskManager {
};

/**
* Initializes the task manager, preventing any further addition of middleware,
* Initializes the task manager, preventing unknown further addition of middleware,
* enabling the task manipulation methods, and beginning the background polling
* mechanism.
*/
Expand All @@ -129,7 +130,7 @@ export class TaskManager {
this.store = new TaskStore({
serializer: opts.serializer,
savedObjectsRepository: opts.savedObjectsRepository,
callCluster: opts.callAsInternalUser,
callCluster: (opts.callAsInternalUser as unknown) as ElasticJs,
index: opts.config.index,
maxAttempts: opts.config.max_attempts,
definitions: this.definitions,
Expand Down Expand Up @@ -273,7 +274,7 @@ export class TaskManager {
*/
public async schedule(
taskInstance: TaskInstanceWithDeprecatedFields,
options?: any
options?: object
): Promise<ConcreteTaskInstance> {
await this.waitUntilStarted();
const { taskInstance: modifiedTask } = await this.middleware.beforeSave({
Expand Down Expand Up @@ -308,7 +309,7 @@ export class TaskManager {
*/
public async ensureScheduled(
taskInstance: TaskInstanceWithId,
options?: any
options?: object
): Promise<TaskInstanceWithId> {
try {
return await this.schedule(taskInstance, options);
Expand Down Expand Up @@ -436,7 +437,7 @@ export async function awaitTaskRunResult(
}
},
async (error: Error) => {
// reject if any error event takes place for the requested task
// reject if unknown error event takes place for the requested task
subscription.unsubscribe();
if (isTaskRunRequestEvent(taskEvent)) {
return reject(
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/task_manager/server/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ export class TaskManagerRunner implements TaskRunner {
attempts,
addDuration,
}: {
error: any;
error: Error;
attempts: number;
addDuration?: string;
}): Date | null {
let result = null;

// Use custom retry logic, if any, otherwise we'll use the default logic
// Use custom retry logic, if unknown, otherwise we'll use the default logic
const retry: boolean | Date = this.definition.getRetry
? this.definition.getRetry(attempts, error)
: true;
Expand Down
Loading

0 comments on commit 78d11a7

Please sign in to comment.