diff --git a/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.test.ts b/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.test.ts index d8734683985a6..cf219a6885cda 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.test.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.test.ts @@ -375,5 +375,26 @@ describe('Buffered Task Store', () => { expect(await results[2]).toMatchObject(partialTasks[2]); expect(await results[3]).toMatchObject(partialTasks[3]); }); + + test(`updates the stateVersion`, async () => { + const taskStore = taskStoreMock.create({ stateVersion: 2 }); + const bufferedStore = new BufferedTaskStore(taskStore, {}); + + const task = taskManagerMock.createTask(); + const partialTask = { + id: task.id, + version: task.version, + status: 'running' as TaskStatus, + }; + + taskStore.bulkPartialUpdate.mockResolvedValue([asOk(partialTask)]); + + expect( + await bufferedStore.partialUpdate(partialTask, { validate: false, doc: task }) + ).toMatchObject(partialTask); + expect(taskStore.bulkPartialUpdate).toHaveBeenCalledWith([ + { ...partialTask, stateVersion: 2 }, + ]); + }); }); }); diff --git a/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.ts b/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.ts index d5b2dbd20a922..5394b33e876c6 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/buffered_task_store.ts @@ -73,12 +73,14 @@ export class BufferedTaskStore implements Updatable { options: { validate: boolean; doc: ConcreteTaskInstance } ): Promise { // merge the partial updates with the doc and validate - this.taskStore.taskValidator.getValidatedTaskInstanceForUpdating( + const { stateVersion } = this.taskStore.taskValidator.getValidatedTaskInstanceForUpdating( { ...options.doc, ...partialDoc }, { validate: options.validate } ); - const result = await unwrapPromise(this.bufferedPartialUpdate(partialDoc)); + const result = await unwrapPromise( + this.bufferedPartialUpdate({ ...partialDoc, ...(stateVersion ? { stateVersion } : {}) }) + ); // merge the partial update result with the doc and validate return this.taskStore.taskValidator.getValidatedTaskInstanceFromReading( diff --git a/x-pack/platform/plugins/shared/task_manager/server/task_store.mock.ts b/x-pack/platform/plugins/shared/task_manager/server/task_store.mock.ts index e20c32aa39cfb..62c5159360536 100644 --- a/x-pack/platform/plugins/shared/task_manager/server/task_store.mock.ts +++ b/x-pack/platform/plugins/shared/task_manager/server/task_store.mock.ts @@ -11,13 +11,16 @@ import { TaskStore } from './task_store'; interface TaskStoreOptions { index?: string; taskManagerId?: string; + stateVersion?: number; } export const taskStoreMock = { - create({ index = '', taskManagerId = '' }: TaskStoreOptions = {}) { + create({ index = '', taskManagerId = '', stateVersion }: TaskStoreOptions = {}) { const mocked = { taskValidator: { getValidatedTaskInstanceFromReading: jest.fn().mockImplementation((task) => task), - getValidatedTaskInstanceForUpdating: jest.fn().mockImplementation((task) => task), + getValidatedTaskInstanceForUpdating: jest + .fn() + .mockImplementation((task) => ({ ...task, ...(stateVersion ? { stateVersion } : {}) })), }, convertToSavedObjectIds: jest.fn(), update: jest.fn(),