diff --git a/packages/fleet/lib/fleet.ts b/packages/fleet/lib/fleet.ts index 96b32ca05ed..1dc0550edb2 100644 --- a/packages/fleet/lib/fleet.ts +++ b/packages/fleet/lib/fleet.ts @@ -8,7 +8,7 @@ import type { Node } from './types.js'; import type { CommitHash, Deployment, RoutingId } from '@nangohq/types'; import { FleetError } from './utils/errors.js'; import { setTimeout } from 'node:timers/promises'; -import { Supervisor } from './supervisor.js'; +import { Supervisor } from './supervisor/supervisor.js'; import type { NodeProvider } from './node-providers/node_provider.js'; import type { FleetId } from './instances.js'; import { envs } from './env.js'; diff --git a/packages/fleet/lib/supervisor/operation.ts b/packages/fleet/lib/supervisor/operation.ts new file mode 100644 index 00000000000..ad108b35ee7 --- /dev/null +++ b/packages/fleet/lib/supervisor/operation.ts @@ -0,0 +1,45 @@ +import type { Node } from '../types.js'; +import type { Deployment, NodeConfig } from '@nangohq/types'; + +export type Operation = + | { type: 'CREATE'; routingId: Node['routingId']; deployment: Deployment; nodeConfig?: NodeConfig | undefined } + | { type: 'START'; node: Node } + | { type: 'FAIL'; node: Node; reason: 'starting_timeout_reached' | 'pending_timeout_reached' | 'idle_timeout_reached' } + | { type: 'OUTDATE'; node: Node } + | { type: 'FINISHING'; node: Node } + | { type: 'FINISHING_TIMEOUT'; node: Node } + | { type: 'TERMINATE'; node: Node } + | { type: 'REMOVE'; node: Node }; + +export const Operation = { + asSpanTags: (o: Operation): Record => { + switch (o.type) { + case 'CREATE': + return { + operation: o.type, + routingId: o.routingId, + deploymentId: o.deployment.id + }; + case 'FAIL': + return { + operation: o.type, + nodeId: o.node.id, + reason: o.reason + }; + case 'START': + return { + operation: o.type, + nodeId: o.node.id + }; + case 'OUTDATE': + case 'FINISHING': + case 'FINISHING_TIMEOUT': + case 'TERMINATE': + case 'REMOVE': + return { + operation: o.type, + nodeId: o.node.id + }; + } + } +}; diff --git a/packages/fleet/lib/supervisor.integration.test.ts b/packages/fleet/lib/supervisor/supervisor.integration.test.ts similarity index 95% rename from packages/fleet/lib/supervisor.integration.test.ts rename to packages/fleet/lib/supervisor/supervisor.integration.test.ts index b0a40252e2b..ab49838d917 100644 --- a/packages/fleet/lib/supervisor.integration.test.ts +++ b/packages/fleet/lib/supervisor/supervisor.integration.test.ts @@ -1,14 +1,14 @@ import { expect, describe, it, beforeEach, afterEach, vi } from 'vitest'; import { Ok } from '@nangohq/utils'; import { STATE_TIMEOUT_MS, Supervisor } from './supervisor.js'; -import { getTestDbClient } from './db/helpers.test.js'; -import * as deployments from './models/deployments.js'; -import * as nodes from './models/nodes.js'; -import * as nodeConfigOverrides from './models/node_config_overrides.js'; -import { generateCommitHash } from './models/helpers.js'; -import { createNodeWithAttributes } from './models/helpers.test.js'; +import { getTestDbClient } from '../db/helpers.test.js'; +import * as deployments from '../models/deployments.js'; +import * as nodes from '../models/nodes.js'; +import * as nodeConfigOverrides from '../models/node_config_overrides.js'; +import { generateCommitHash } from '../models/helpers.js'; +import { createNodeWithAttributes } from '../models/helpers.test.js'; import type { Deployment } from '@nangohq/types'; -import { FleetError } from './utils/errors.js'; +import { FleetError } from '../utils/errors.js'; const mockNodeProvider = { defaultNodeConfig: { diff --git a/packages/fleet/lib/supervisor.ts b/packages/fleet/lib/supervisor/supervisor.ts similarity index 89% rename from packages/fleet/lib/supervisor.ts rename to packages/fleet/lib/supervisor/supervisor.ts index 407692c1ee1..414775962d5 100644 --- a/packages/fleet/lib/supervisor.ts +++ b/packages/fleet/lib/supervisor/supervisor.ts @@ -1,62 +1,20 @@ import tracer from 'dd-trace'; -import type { DatabaseClient } from './db/client.js'; +import type { DatabaseClient } from '../db/client.js'; import type { Knex } from 'knex'; -import { logger } from './utils/logger.js'; -import * as nodes from './models/nodes.js'; -import * as deployments from './models/deployments.js'; -import * as nodeConfigOverrides from './models/node_config_overrides.js'; +import { logger } from '../utils/logger.js'; +import * as nodes from '../models/nodes.js'; +import * as deployments from '../models/deployments.js'; +import * as nodeConfigOverrides from '../models/node_config_overrides.js'; import { Err, Ok, retryWithBackoff } from '@nangohq/utils'; import type { Result } from '@nangohq/utils'; -import { FleetError } from './utils/errors.js'; -import type { Node, NodeConfigOverride } from './types.js'; +import { FleetError } from '../utils/errors.js'; +import type { Node, NodeConfigOverride } from '../types.js'; import type { Deployment, NodeConfig } from '@nangohq/types'; import { setTimeout } from 'node:timers/promises'; -import type { NodeProvider } from './node-providers/node_provider.js'; -import { envs } from './env.js'; -import { withPgLock } from './utils/locking.js'; - -type Operation = - | { type: 'CREATE'; routingId: Node['routingId']; deployment: Deployment; nodeConfig?: NodeConfig | undefined } - | { type: 'START'; node: Node } - | { type: 'FAIL'; node: Node; reason: 'starting_timeout_reached' | 'pending_timeout_reached' | 'idle_timeout_reached' } - | { type: 'OUTDATE'; node: Node } - | { type: 'FINISHING'; node: Node } - | { type: 'FINISHING_TIMEOUT'; node: Node } - | { type: 'TERMINATE'; node: Node } - | { type: 'REMOVE'; node: Node }; - -const Operation = { - asSpanTags: (o: Operation): Record => { - switch (o.type) { - case 'CREATE': - return { - operation: o.type, - routingId: o.routingId, - deploymentId: o.deployment.id - }; - case 'FAIL': - return { - operation: o.type, - nodeId: o.node.id, - reason: o.reason - }; - case 'START': - return { - operation: o.type, - nodeId: o.node.id - }; - case 'OUTDATE': - case 'FINISHING': - case 'FINISHING_TIMEOUT': - case 'TERMINATE': - case 'REMOVE': - return { - operation: o.type, - nodeId: o.node.id - }; - } - } -}; +import type { NodeProvider } from '../node-providers/node_provider.js'; +import { envs } from '../env.js'; +import { withPgLock } from '../utils/locking.js'; +import { Operation } from './operation.js'; type SupervisorState = 'stopped' | 'running' | 'stopping';