Skip to content

Commit

Permalink
fix(fleet): move supervisor to own folder and split operation
Browse files Browse the repository at this point in the history
  • Loading branch information
TBonnin committed Dec 13, 2024
1 parent 3039670 commit b9bee1c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/fleet/lib/fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
45 changes: 45 additions & 0 deletions packages/fleet/lib/supervisor/operation.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | number> => {
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
};
}
}
};
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, string | number> => {
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';

Expand Down

0 comments on commit b9bee1c

Please sign in to comment.