import { Option } from 'fp-ts/lib/Option' ; import { ConcreteTaskInstance } from './task' ; import { Result , Err } from './lib/result_type' ; import { ClaimAndFillPoolResult } from './lib/fill_pool' ; import { PollingError } from './polling' ; import { TaskRunResult } from './task_running' ; import { EphemeralTaskInstanceRequest } from './ephemeral_task_lifecycle' ; export enum TaskPersistence { Recurring = 'recurring' , NonRecurring = 'non_recurring' , Ephemeral = 'ephemeral' } export enum TaskEventType { TASK_CLAIM = 'TASK_CLAIM' , TASK_MARK_RUNNING = 'TASK_MARK_RUNNING' , TASK_RUN = 'TASK_RUN' , TASK_RUN_REQUEST = 'TASK_RUN_REQUEST' , TASK_POLLING_CYCLE = 'TASK_POLLING_CYCLE' , TASK_MANAGER_STAT = 'TASK_MANAGER_STAT' , EPHEMERAL_TASK_DELAYED_DUE_TO_CAPACITY = 'EPHEMERAL_TASK_DELAYED_DUE_TO_CAPACITY' } export enum TaskClaimErrorType { CLAIMED_BY_ID_OUT_OF_CAPACITY = 'CLAIMED_BY_ID_OUT_OF_CAPACITY' , CLAIMED_BY_ID_NOT_RETURNED = 'CLAIMED_BY_ID_NOT_RETURNED' , CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS = 'CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS' } export interface TaskTiming { start : number ; stop : number ; } export type WithTaskTiming < T > = T & { timing : TaskTiming ; } ; export function startTaskTimer ( ) { const start = Date . now ( ) ; return ( ) => ( { start , stop : Date . now ( ) } ) ; } export interface TaskEvent < OkResult , ErrorResult , ID = string > { id ? : ID ; timing ? : TaskTiming ; type : TaskEventType ; event : Result < OkResult , ErrorResult > ; } export interface RanTask { task : ConcreteTaskInstance ; persistence : TaskPersistence ; result : TaskRunResult ; } export type ErroredTask = RanTask & { error : Error ; } ; export interface ClaimTaskErr { task : Option < ConcreteTaskInstance > ; errorType : TaskClaimErrorType ; } export type TaskMarkRunning = TaskEvent < ConcreteTaskInstance , Error > ; export type TaskRun = TaskEvent < RanTask , ErroredTask > ; export type TaskClaim = TaskEvent < ConcreteTaskInstance , ClaimTaskErr > ; export type TaskRunRequest = TaskEvent < ConcreteTaskInstance , Error > ; export type EphemeralTaskRejectedDueToCapacity = TaskEvent < EphemeralTaskInstanceRequest , Error > ; export type TaskPollingCycle < T = string > = TaskEvent < ClaimAndFillPoolResult , PollingError < T > > ; export type TaskManagerStats = 'load' | 'pollingDelay' | 'claimDuration' | 'queuedEphemeralTasks' | 'ephemeralTaskDelay' ; export type TaskManagerStat = TaskEvent < number , never , TaskManagerStats > ; export type OkResultOf < EventType > = EventType extends TaskEvent < infer OkResult , infer ErrorResult > ? OkResult : never ; export type ErrResultOf < EventType > = EventType extends TaskEvent < infer OkResult , infer ErrorResult > ? ErrorResult : never ; export function asTaskMarkRunningEvent ( id , event , timing ? ) { return { id , type : TaskEventType . TASK_MARK_RUNNING , event , timing , } ; } export function asTaskRunEvent ( id , event , timing ? ) { return { id , type : TaskEventType . TASK_RUN , event , timing , } ; } export function asTaskClaimEvent ( id , event , timing ? ) { return { id , type : TaskEventType . TASK_CLAIM , event , timing , } ; } export function asTaskRunRequestEvent ( id , event , timing ? ) { return { id , type : TaskEventType . TASK_RUN_REQUEST , event , timing , } ; } export function asTaskPollingCycleEvent < T = string > ( event , timing ? ) { return { type : TaskEventType . TASK_POLLING_CYCLE , event , timing , } ; } export function asTaskManagerStatEvent ( id , event ) { return { id , type : TaskEventType . TASK_MANAGER_STAT , event , } ; } export function asEphemeralTaskRejectedDueToCapacityEvent ( id , event , timing ? ) { return { id , type : TaskEventType . EPHEMERAL_TASK_DELAYED_DUE_TO_CAPACITY , event , timing , } ; } export function isTaskMarkRunningEvent ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_MARK_RUNNING ; } export function isTaskRunEvent ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_RUN ; } export function isTaskClaimEvent ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_CLAIM ; } export function isTaskRunRequestEvent ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_RUN_REQUEST ; } export function isTaskPollingCycleEvent < T = string > ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_POLLING_CYCLE ; } export function isTaskManagerStatEvent ( taskEvent ) { return taskEvent . type === TaskEventType . TASK_MANAGER_STAT ; } export function isEphemeralTaskRejectedDueToCapacityEvent ( taskEvent ) { return taskEvent . type === TaskEventType . EPHEMERAL_TASK_DELAYED_DUE_TO_CAPACITY ; }