Class ExtractorTaskScheduler
Task scheduler that runs tasks and reports their status to the integrations API.
Implements
Inherited Members
Namespace: Cognite.Extractor.Utils.Unstable.Tasks
Assembly: ExtractorUtils.dll
Syntax
public class ExtractorTaskScheduler : IDisposable
Constructors
| Edit this page View SourceExtractorTaskScheduler(IIntegrationSink, ILogger<ExtractorTaskScheduler>)
Constructor.
Declaration
public ExtractorTaskScheduler(IIntegrationSink sink, ILogger<ExtractorTaskScheduler> logger)
Parameters
| Type | Name | Description |
|---|---|---|
| IIntegrationSink | sink | Sink for task updates. |
| ILogger<ExtractorTaskScheduler> | logger | Logger object. |
Properties
| Edit this page View SourceCompletedTask
Task that terminates once the run method terminates.
Declaration
public Task CompletedTask { get; }
Property Value
| Type | Description |
|---|---|
| Task |
Methods
| Edit this page View SourceAddScheduledTask(BaseSchedulableTask, bool)
Add a task to the scheduler.
Declaration
public void AddScheduledTask(BaseSchedulableTask operation, bool runImmediately)
Parameters
| Type | Name | Description |
|---|---|---|
| BaseSchedulableTask | operation | Type implementing the runnable task. |
| bool | runImmediately | Whether to run the task immediately. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | If a task with the same name already exists. |
CanTaskRunNow(string)
Check whether the task given by name currently reports itself as
able to run, via CanRunNow().
This is independent of whether the task has been queued to run (see TryScheduleTaskNow(string)): a task can be queued while CanRunNow() stays false for an extended, unbounded period (for example, a task that requires a live external connection before it can start). Callers that need a fast, explicit failure instead of hanging on WaitForNextEndOfTask(string, TimeSpan, CancellationToken) should check this immediately after a successful TryScheduleTaskNow(string) call.
Declaration
public bool CanTaskRunNow(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task to check. |
Returns
| Type | Description |
|---|---|
| bool |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | If no task with this name is registered. |
CancelInnerAndWait(int, BaseErrorReporter)
Cancel the running task, if it exists.
Declaration
public Task CancelInnerAndWait(int timeoutms, BaseErrorReporter outerReporter)
Parameters
| Type | Name | Description |
|---|---|---|
| int | timeoutms | |
| BaseErrorReporter | outerReporter |
Returns
| Type | Description |
|---|---|
| Task |
CancelTask(string, string?)
Cancel a task if it is currently running.
This is a thin wrapper around TryCancelTask(string, string?) that discards the
"did it actually do anything" outcome. Prefer TryCancelTask(string, string?) for any
new caller that needs to distinguish "cancelled a running task" from "there was
nothing to cancel" -- for example, a Stop action needs to report those as different
outcomes (succeeded vs. failed), not silently treat both as success.
Declaration
public void CancelTask(string name, string? reason = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task to cancel |
| string | reason | Reason for canceling the task |
Dispose()
Declaration
public void Dispose()
Dispose(bool)
Dispose of the scheduler.
Can be overridden in base classes.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | Whether to dispose managed resources. |
GetRegisteredTasks()
Get a list of registered tasks, used for reporting startup.
Declaration
public IEnumerable<IntegrationTask> GetRegisteredTasks()
Returns
| Type | Description |
|---|---|
| IEnumerable<IntegrationTask> |
Notify()
Wake up the scheduler and tell it to check its tasks again.
Declaration
public void Notify()
Run(CancellationToken)
Run the scheduler.
This should only be called once on a given scheduler.
To re-run it after a crash, the scheduler must be re-initialized.
Declaration
public Task<SchedulerTaskResult> Run(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | Global cancellation token for stopping the entire scheduler. |
Returns
| Type | Description |
|---|---|
| Task<SchedulerTaskResult> |
ScheduleTaskNow(string, bool)
Schedule a task now. If it is already running it will
Declaration
public void ScheduleTaskNow(string name, bool reScheduleIfRunning = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task to schedule |
| bool | reScheduleIfRunning | If true, re-schedule the task to run again once it finishes. If false, and the task is already running, nothing will happen. |
TryCancelTask(string, string?)
Cancel a task if it is currently running.
Declaration
public bool TryCancelTask(string name, string? reason = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task to cancel. |
| string | reason | Reason for canceling the task. |
Returns
| Type | Description |
|---|---|
| bool |
|
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | If no task with this name is registered. |
TryScheduleTaskNow(string)
Schedule a task to run now, unless it is already running.
Unlike ScheduleTaskNow(string, bool), this never re-schedules a task that is already
running -- it exists specifically to let a caller (e.g. a Start action handler)
distinguish "the task was queued to run" from "it was already running", which
ScheduleTaskNow(string, bool)'s void return cannot express.
A true return only means the task was queued to run on the scheduler's next
tick -- it does not mean the task actually started, since
CanRunNow() independently gates that. Use
CanTaskRunNow(string) immediately afterwards if the caller needs to know whether
the task is actually eligible to start right now, rather than merely queued.
Declaration
public bool TryScheduleTaskNow(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task to schedule. |
Returns
| Type | Description |
|---|---|
| bool |
|
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | If no task with this name is registered. |
WaitForNextEndOfTask(string, TimeSpan, CancellationToken)
Wait for the next time the task given by task ends.
If the task is not currently running, this will wait until it starts running and then ends.
Note that if the task fails, this will re-throw the exception that caused the task failure, or a TaskCanceledException if it was canceled.
Declaration
public Task WaitForNextEndOfTask(string task, TimeSpan timeout, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | task | Task to wait for, must be added to the scheduler. |
| TimeSpan | timeout | Timeout, set to Timeout.InfiniteTimespan to wait forever. |
| CancellationToken | token | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | If the task does not exist. |
| TimeoutException | If waiting for the task to end timed out. |