Class PeriodicScheduler
Utility to schedule and manage periodic tasks
Implements
Inherited Members
Namespace: Cognite.Extractor.Common
Assembly: Cognite.Common.dll
Syntax
public class PeriodicScheduler : IDisposable
Constructors
| Edit this page View SourcePeriodicScheduler(CancellationToken)
Constructor.
Declaration
public PeriodicScheduler(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | Cancellation token linked to all running tasks |
Properties
| Edit this page View SourceCount
Number of currently active tasks
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
| Edit this page View SourceCancelAndWaitForTermination(string)
Signal a task should terminate then wait. This will cancel the operation if it is running, then wait for it to terminate. Will throw an exception if the task has failed, except if it failed with OperationCancelledException.
Declaration
public Task CancelAndWaitForTermination(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task to cancel |
Returns
| Type | Description |
|---|---|
| Task | Task which completes once the task has terminated |
ContainsTask(string)
Returns true if a task with the given name exists
Declaration
public bool ContainsTask(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Task to check |
Returns
| Type | Description |
|---|---|
| bool | True if task identified by |
Dispose()
Dispose of scheduler. Will cancel all running tasks. Do not override this in subclasses.
Declaration
public void Dispose()
Dispose(bool)
Dispose of scheduler. Will cancel all running tasks. Override this in subclasses.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | True to dispose |
ExitAllAndWait(int, bool)
Same as calling ExitAndWaitForTermination on all tasks.
Declaration
public Task ExitAllAndWait(int timeoutMs = 0, bool cancel = false)
Parameters
| Type | Name | Description |
|---|---|---|
| int | timeoutMs | Timeout in milliseconds |
| bool | cancel | True to cancel running tasks, false to just signal them to exit once they are done |
Returns
| Type | Description |
|---|---|
| Task | Task which completes once all tasks are done |
ExitAndWaitForTermination(string)
Signal a task should terminate then wait. This will not cancel the operation if it is running, but wait for it to terminate. Will throw an exception if the task has failed.
Declaration
public Task ExitAndWaitForTermination(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task to cancel |
Returns
| Type | Description |
|---|---|
| Task | Task which completes once the task has terminated |
SchedulePeriodicTask(string?, ITimeSpanProvider, Action<CancellationToken>, bool)
Schedule a new periodic task to run with interval interval.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched.
Declaration
public string SchedulePeriodicTask(string? name, ITimeSpanProvider interval, Action<CancellationToken> operation, bool runImmediately = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task, used to refer to it later |
| ITimeSpanProvider | interval | Interval to schedule on |
| Action<CancellationToken> | operation | Function to call on each iteration |
| bool | runImmediately | True to execute the periodic task immediately, false to first wait until triggered by interval or manually |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
SchedulePeriodicTask(string?, ITimeSpanProvider, Func<CancellationToken, Task>, bool)
Schedule a new periodic task to run with interval interval.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched.
Declaration
public string SchedulePeriodicTask(string? name, ITimeSpanProvider interval, Func<CancellationToken, Task> operation, bool runImmediately = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task, used to refer to it later |
| ITimeSpanProvider | interval | Interval to schedule on |
| Func<CancellationToken, Task> | operation | Function to call on each iteration |
| bool | runImmediately | True to execute the periodic task immediately, false to first wait until triggered by interval or manually |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
SchedulePeriodicTask(string?, TimeSpan, Action<CancellationToken>, bool)
Schedule a new periodic task to run with interval interval.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched.
Declaration
public string SchedulePeriodicTask(string? name, TimeSpan interval, Action<CancellationToken> operation, bool runImmediately = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task, used to refer to it later |
| TimeSpan | interval | Interval to schedule on |
| Action<CancellationToken> | operation | Function to call on each iteration |
| bool | runImmediately | True to execute the periodic task immediately, false to first wait until triggered by interval or manually |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
SchedulePeriodicTask(string?, TimeSpan, Func<CancellationToken, Task>, bool)
Schedule a new periodic task to run with interval interval.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched.
Declaration
public string SchedulePeriodicTask(string? name, TimeSpan interval, Func<CancellationToken, Task> operation, bool runImmediately = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task, used to refer to it later |
| TimeSpan | interval | Interval to schedule on |
| Func<CancellationToken, Task> | operation | Function to call on each iteration |
| bool | runImmediately | True to execute the periodic task immediately, false to first wait until triggered by interval or manually |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
ScheduleTask(string?, Action<CancellationToken>)
Schedule a new task to run with on the scheduler.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched. Note that this method waits on operation to yield,
so make sure that it does not contain too much synchronous code.
Declaration
public string ScheduleTask(string? name, Action<CancellationToken> operation)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | |
| Action<CancellationToken> | operation |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
ScheduleTask(string?, Func<CancellationToken, Task<SchedulerTaskResult>>)
Schedule a new task to run with on the scheduler.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched. Note that this method waits on operation to yield,
so make sure that it does not contain too much synchronous code.
Declaration
public string ScheduleTask(string? name, Func<CancellationToken, Task<SchedulerTaskResult>> operation)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | |
| Func<CancellationToken, Task<SchedulerTaskResult>> | operation |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
ScheduleTask(string?, Func<CancellationToken, Task>, SchedulerTaskResult)
Schedule a new task to run with on the scheduler.
Exceptions are not caught, so operation should catch its own errors, or the
task from WaitForAll should be watched. Note that this method waits on operation to yield,
so make sure that it does not contain too much synchronous code.
Declaration
public string ScheduleTask(string? name, Func<CancellationToken, Task> operation, SchedulerTaskResult staticExpected = SchedulerTaskResult.Expected)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of the task, used to refer to it later |
| Func<CancellationToken, Task> | operation | Interval to schedule on |
| SchedulerTaskResult | staticExpected | Whether the task is expected to complete or not. |
Returns
| Type | Description |
|---|---|
| string | The name of the scheduled task |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
TryPauseTask(string, bool)
Set the paused state of the named task. In this state it will only trigger when manually triggered. Same as setting the timespan to infinite when creating the task. The task will trigger when unpaused.
Declaration
public bool TryPauseTask(string name, bool paused)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task to pause |
| bool | paused | True to pause the task, false to unpause |
Returns
| Type | Description |
|---|---|
| bool | True if the task was paused |
TryTriggerTask(string)
Manually trigger a task. The task will always run after this, but may run twice if it is about to run due to timeout. Either way the task will always run after this.
Declaration
public bool TryTriggerTask(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name |
Returns
| Type | Description |
|---|---|
| bool | True if the task was triggered |
WaitForAll()
Returns the internal task monitoring all running tasks. It will fail if any internal task fails.
Declaration
public Task WaitForAll()
Returns
| Type | Description |
|---|---|
| Task |
WaitForTermination(string)
Waits for a task to terminate. If it is periodic this may never happen.
Declaration
public Task WaitForTermination(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Name of task to wait for |
Returns
| Type | Description |
|---|---|
| Task |