Skip to content

Commit

Permalink
Better synchronize enabling of risk engine task
Browse files Browse the repository at this point in the history
If an error is raised during enablement (either updating the SO or
starting the task), we will undo enablement by disabling the engine.
  • Loading branch information
rylnd committed Aug 23, 2023
1 parent 0dcb486 commit 7cf14b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ describe('RiskEngineDataClient', () => {
mockTaskManagerStart = taskManagerMock.createStart();
});

it('should return error if saved object not exist', async () => {
it('returns an error if saved object does not exist', async () => {
mockSavedObjectClient.find.mockResolvedValue({
page: 1,
per_page: 20,
Expand All @@ -642,10 +642,10 @@ describe('RiskEngineDataClient', () => {

await expect(
riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart })
).rejects.toThrow('There no saved object configuration for risk engine');
).rejects.toThrow('Risk engine configuration not found');
});

it('should update saved object attrubute', async () => {
it('should update saved object attribute', async () => {
await riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart });

expect(mockSavedObjectClient.update).toHaveBeenCalledWith(
Expand All @@ -659,6 +659,29 @@ describe('RiskEngineDataClient', () => {
}
);
});

describe('if task manager throws an error', () => {
beforeEach(() => {
mockTaskManagerStart.ensureScheduled.mockRejectedValueOnce(new Error('Task Manager error'));
});

it('disables the risk engine and re-throws the error', async () => {
await expect(
riskEngineDataClient.enableRiskEngine({ taskManager: mockTaskManagerStart })
).rejects.toThrow('Task Manager error');

expect(mockSavedObjectClient.update).toHaveBeenCalledWith(
'risk-engine-configuration',
'de8ca330-2d26-11ee-bc86-f95bf6192ee6',
{
enabled: false,
},
{
refresh: 'wait_for',
}
);
});
});
});

describe('disableRiskEngine', () => {
Expand All @@ -680,7 +703,7 @@ describe('RiskEngineDataClient', () => {
try {
await riskEngineDataClient.disableRiskEngine({ taskManager: mockTaskManagerStart });
} catch (e) {
expect(e.message).toEqual('There no saved object configuration for risk engine');
expect(e.message).toEqual('Risk engine configuration not found');
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,29 @@ export class RiskEngineDataClient {
}

public async enableRiskEngine({ taskManager }: { taskManager: TaskManagerStartContract }) {
const configurationResult = await updateSavedObjectAttribute({
savedObjectsClient: this.options.soClient,
attributes: {
enabled: true,
},
});
try {
const configurationResult = await updateSavedObjectAttribute({
savedObjectsClient: this.options.soClient,
attributes: {
enabled: true,
},
});

await startRiskScoringTask({
logger: this.options.logger,
namespace: this.options.namespace,
riskEngineDataClient: this,
taskManager,
});
await startRiskScoringTask({
logger: this.options.logger,
namespace: this.options.namespace,
riskEngineDataClient: this,
taskManager,
});

return configurationResult;
return configurationResult;
} catch (e) {
this.options.logger.error(`Error while enabling risk engine: ${e.message}`);

await this.disableRiskEngine({ taskManager });

throw e;
}
}

public async disableRiskEngine({ taskManager }: { taskManager: TaskManagerStartContract }) {
Expand All @@ -184,7 +192,7 @@ export class RiskEngineDataClient {
logger: this.options.logger,
});

return updateSavedObjectAttribute({
return await updateSavedObjectAttribute({
savedObjectsClient: this.options.soClient,
attributes: {
enabled: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const updateSavedObjectAttribute = async ({
});

if (!savedObjectConfiguration) {
throw new Error('There no saved object configuration for risk engine');
throw new Error('Risk engine configuration not found');
}

const result = await savedObjectsClient.update(
Expand Down

0 comments on commit 7cf14b9

Please sign in to comment.