Skip to content

Commit

Permalink
[Cloud Security] Restarting transform on failure (#161192)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSh authored Jul 6, 2023
1 parent b641a22 commit 9c8b63a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('startTransformIfNotStarted', () => {
jest.resetAllMocks();
});

['failed', 'stopping', 'started', 'aborting', 'indexing'].forEach((state) =>
['stopping', 'started', 'aborting', 'indexing'].forEach((state) =>
it(`expect not to start if state is ${state}`, async () => {
mockEsClient.transform.getTransformStats.mockResolvedValue({
transforms: [getTransformWithState(state)],
Expand Down Expand Up @@ -125,4 +125,20 @@ describe('startTransformIfNotStarted', () => {
transform_id: latestFindingsTransform.transform_id,
});
});

it('expect to attempt restart if state is failed', async () => {
mockEsClient.transform.getTransformStats.mockResolvedValue({
transforms: [getTransformWithState('failed')],
count: 1,
});
await startTransformIfNotStarted(mockEsClient, latestFindingsTransform.transform_id, logger);
expect(mockEsClient.transform.getTransformStats).toHaveBeenCalledTimes(1);
expect(mockEsClient.transform.getTransformStats).toHaveBeenCalledWith({
transform_id: latestFindingsTransform.transform_id,
});
expect(mockEsClient.transform.startTransform).toHaveBeenCalledTimes(1);
expect(mockEsClient.transform.startTransform).toHaveBeenCalledWith({
transform_id: latestFindingsTransform.transform_id,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,27 @@ export const startTransformIfNotStarted = async (
const transformStats = await esClient.transform.getTransformStats({
transform_id: transformId,
});

if (transformStats.count <= 0) {
logger.error(`Failed starting transform ${transformId}: couldn't find transform`);
return;
}

const fetchedTransformStats = transformStats.transforms[0];
if (fetchedTransformStats.state === 'stopped') {

// trying to restart the transform in case it comes to a full stop or failure
if (fetchedTransformStats.state === 'stopped' || fetchedTransformStats.state === 'failed') {
try {
return await esClient.transform.startTransform({ transform_id: transformId });
} catch (startErr) {
const startError = transformError(startErr);
logger.error(`Failed starting transform ${transformId}: ${startError.message}`);
logger.error(
`Failed to start transform ${transformId}. Transform State: Transform State: ${fetchedTransformStats.state}. Error: ${startError.message}`
);
}
} else if (
fetchedTransformStats.state === 'stopping' ||
fetchedTransformStats.state === 'aborting' ||
fetchedTransformStats.state === 'failed'
) {
}

if (fetchedTransformStats.state === 'stopping' || fetchedTransformStats.state === 'aborting') {
logger.error(
`Not starting transform ${transformId} since it's state is: ${fetchedTransformStats.state}`
);
Expand Down

0 comments on commit 9c8b63a

Please sign in to comment.