Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const DEFAULT_TASK_INTERVAL_MINUTES = 10;
export const DEFAULT_TASK_START_DELAY_MINUTES = 10;
8 changes: 6 additions & 2 deletions x-pack/platform/plugins/shared/cases/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import type { TypeOf } from '@kbn/config-schema';
import { schema } from '@kbn/config-schema';
import { ALLOWED_MIME_TYPES } from '../common/constants/mime_types';
import {
DEFAULT_TASK_INTERVAL_MINUTES,
DEFAULT_TASK_START_DELAY_MINUTES,
} from '../common/constants/incremental_id';

export const ConfigSchema = schema.object({
markdownPlugins: schema.object({
Expand All @@ -32,14 +36,14 @@ export const ConfigSchema = schema.object({
* The interval that the task should be scheduled at
*/
taskIntervalMinutes: schema.number({
defaultValue: 10,
defaultValue: DEFAULT_TASK_INTERVAL_MINUTES,
min: 5,
}),
/**
* The initial delay the task will be started with
*/
taskStartDelayMinutes: schema.number({
defaultValue: 10,
defaultValue: DEFAULT_TASK_START_DELAY_MINUTES,
min: 1,
}),
}),
Expand Down
17 changes: 9 additions & 8 deletions x-pack/platform/plugins/shared/cases/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ export class CasePlugin
}

if (plugins.taskManager) {
if (this.caseConfig.incrementalId.enabled) {
this.incrementalIdTaskManager = new IncrementalIdTaskManager(
plugins.taskManager,
this.caseConfig.incrementalId,
this.logger
);
}

if (plugins.usageCollection) {
createCasesTelemetry({
core,
Expand All @@ -158,6 +150,15 @@ export class CasePlugin
kibanaVersion: this.kibanaVersion,
});
}

if (this.caseConfig.incrementalId.enabled) {
this.incrementalIdTaskManager = new IncrementalIdTaskManager(
plugins.taskManager,
this.caseConfig.incrementalId,
this.logger,
plugins.usageCollection
);
}
}

const router = core.http.createRouter<CasesRequestHandlerContext>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
type TaskManagerSetupContract,
type TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server';
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import type { IUsageCounter } from '@kbn/usage-collection-plugin/server/usage_counters/usage_counter';
import { CASE_SAVED_OBJECT, CASE_ID_INCREMENTER_SAVED_OBJECT } from '../../../common/constants';
import { CasesIncrementalIdService } from '../../services/incremental_id';
import type { ConfigType } from '../../config';
Expand All @@ -24,16 +26,21 @@ export class IncrementalIdTaskManager {
private logger: Logger;
private internalSavedObjectsClient?: SavedObjectsClient;
private taskManager?: TaskManagerStartContract;

private successErrorUsageCounter?: IUsageCounter;
constructor(
taskManager: TaskManagerSetupContract,
config: ConfigType['incrementalId'],
logger: Logger
logger: Logger,
usageCollection?: UsageCollectionSetup
) {
this.config = config;
this.logger = logger.get('incremental_id_task');
this.logger.info('Registering Case Incremental ID Task Manager');

if (usageCollection) {
this.successErrorUsageCounter = usageCollection?.createUsageCounter('CasesIncrementalId');
}

taskManager.registerTaskDefinitions({
[CASES_INCREMENTAL_ID_SYNC_TASK_TYPE]: {
title: 'Cases Numerical ID assignment',
Expand Down Expand Up @@ -64,20 +71,32 @@ export class IncrementalIdTaskManager {
this.logger.debug(
`${casesWithoutIncrementalId.length} cases without incremental ids`
);
// Increment the case ids
const processedAmount = await casesIncrementService.incrementCaseIds(
casesWithoutIncrementalId
);
this.logger.debug(
`Applied incremental ids to ${processedAmount} out of ${casesWithoutIncrementalId.length} cases`
);

const endTime = performance.now();
this.logger.debug(
`Task terminated ${CASES_INCREMENTAL_ID_SYNC_TASK_ID}. Task run took ${
endTime - startTime
}ms [ started: ${initializedTime}, ended: ${new Date().toISOString()} ]`
);
try {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped this in a try/catch, therefore the whitespace changed.

// Increment the case ids
const processedAmount = await casesIncrementService.incrementCaseIds(
casesWithoutIncrementalId
);
this.logger.debug(
`Applied incremental ids to ${processedAmount} out of ${casesWithoutIncrementalId.length} cases`
);

const endTime = performance.now();
this.logger.debug(
`Task terminated ${CASES_INCREMENTAL_ID_SYNC_TASK_ID}. Task run took ${
endTime - startTime
}ms [ started: ${initializedTime}, ended: ${new Date().toISOString()} ]`
);
this.successErrorUsageCounter?.incrementCounter({
counterName: 'incrementIdTaskSuccess',
incrementBy: 1,
});
} catch (_) {
this.successErrorUsageCounter?.incrementCounter({
counterName: 'incrementIdTaskError',
incrementBy: 1,
});
Comment on lines +90 to +98
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual change

}
},
cancel: async () => {
casesIncrementService.stopService();
Expand Down Expand Up @@ -125,7 +144,7 @@ export class IncrementalIdTaskManager {
id: CASES_INCREMENTAL_ID_SYNC_TASK_ID,
taskType: CASES_INCREMENTAL_ID_SYNC_TASK_TYPE,
// start delayed to give the system some time to start up properly
runAt: new Date(new Date().getTime() + this.config.taskIntervalMinutes * 60 * 1000),
runAt: new Date(new Date().getTime() + this.config.taskStartDelayMinutes * 60 * 1000),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was forgotten in a previous PR

schedule: {
interval: `${this.config.taskIntervalMinutes}m`,
},
Expand Down