-
Notifications
You must be signed in to change notification settings - Fork 8.6k
🌊 [Feature identification] Run as background task #245728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
781fead
23c35a8
b618a57
cdcb828
7bfca3f
cd32af4
b799135
ae6015d
8409cde
6433089
f000a52
b86da49
685d87a
aee0775
89b0de3
0acfc5e
3f63a0e
8bbc46e
d35dc0a
1d5de00
c7c5db4
a459e56
6707f55
bde52ba
46eb4ee
f95c745
d0f0c8b
c3e550e
253ca61
2cf8132
22f8921
3f132bc
d9c934c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import { Streams } from '../models/streams'; | ||
|
|
||
| export type StreamType = 'wired' | 'classic' | 'unknown'; | ||
|
|
||
| export function getStreamTypeFromDefinition(definition: Streams.all.Definition): StreamType { | ||
| if (Streams.WiredStream.Definition.is(definition)) { | ||
| return 'wired'; | ||
| } | ||
|
|
||
| if (Streams.ClassicStream.Definition.is(definition)) { | ||
| return 'classic'; | ||
| } | ||
|
|
||
| return 'unknown'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * 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 class AcknowledgingIncompleteError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'AcknowledgingIncompleteError'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import type { RunContext } from '@kbn/task-manager-plugin/server'; | ||
| import type { RunFunction } from '@kbn/task-manager-plugin/server/task'; | ||
| import type { TaskContext } from './task_definitions'; | ||
|
|
||
| export function cancellableTask( | ||
| run: RunFunction, | ||
| runContext: RunContext, | ||
| taskContext: TaskContext | ||
| ) { | ||
| return async () => { | ||
| if (!runContext.fakeRequest) { | ||
| throw new Error('Request is required to run this task'); | ||
| } | ||
|
|
||
| const { taskClient } = await taskContext.getScopedClients({ | ||
| request: runContext.fakeRequest, | ||
| }); | ||
|
|
||
| try { | ||
|
miltonhultgren marked this conversation as resolved.
|
||
| let intervalId: NodeJS.Timeout; | ||
| const cancellationPromise = new Promise<'canceled'>((resolve) => { | ||
| taskContext.logger.debug('Starting cancellable task check loop'); | ||
| intervalId = setInterval(async () => { | ||
| const task = await taskClient.get(runContext.taskInstance.id); | ||
| taskContext.logger.trace( | ||
| `Cancellable task check loop for task ${runContext.taskInstance.id}: status is ${task.status}` | ||
| ); | ||
| if (task.status === 'being_canceled') { | ||
| runContext.abortController.abort(); | ||
| await taskClient.update({ | ||
| ...task, | ||
| status: 'canceled', | ||
| }); | ||
| resolve('canceled' as const); | ||
| } | ||
| }, 5000); | ||
| }); | ||
|
|
||
| taskContext.logger.debug( | ||
| `Running task ${runContext.taskInstance.id} with cancellation support (race)` | ||
| ); | ||
| const result = await Promise.race([run(), cancellationPromise]).finally(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me that the actual task is going to be cancelled, if the cancellation promise goes off. I weould expect the AbortController sent into the task to get signalled, to indicate to the task itself that it's cancelled. And then the task has to actually USE that AbortController, as needed, to check to see if the task has been cancelled.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand, there are two sources that would call
In either case, the actual tasks themselves should be using Does that address your concern?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clearing that up. I think I must have missed the |
||
| clearInterval(intervalId); | ||
| }); | ||
|
|
||
| if (result === 'canceled') { | ||
| taskContext.logger.debug(`Task ${runContext.taskInstance.id} canceled`); | ||
| return undefined; | ||
| } | ||
|
|
||
| taskContext.logger.debug(`Task ${runContext.taskInstance.id} completed`); | ||
| return result; | ||
| } catch (error) { | ||
| taskContext.logger.error(`Task ${runContext.taskInstance.id} failed unexpectedly`, { error }); | ||
|
|
||
| try { | ||
| await taskClient.update({ | ||
| id: runContext.taskInstance.id, | ||
| status: 'failed', | ||
| task: { | ||
| params: {}, | ||
| error: error.message, | ||
| }, | ||
| created_at: new Date().toISOString(), | ||
| space: '', | ||
| type: '', | ||
| stream: '', | ||
| }); | ||
| } catch (updateError) { | ||
| taskContext.logger.error('Failed to update task status after error', { | ||
| error: updateError, | ||
| }); | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * 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 class CancellationInProgressError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'CancellationInProgressError'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| const fiveMinutesInMs = 5 * 60 * 1000; | ||
|
|
||
| export function isStale(taskCreatedAt: string) { | ||
|
miltonhultgren marked this conversation as resolved.
|
||
| const createdAt = new Date(taskCreatedAt).getTime(); | ||
| const now = Date.now(); | ||
| return now - createdAt > fiveMinutesInMs; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.