@outputai/core 0.3.3-dev.422151e.0 → 0.3.3-next.32f4d87.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@outputai/core",
3
- "version": "0.3.3-dev.422151e.0",
3
+ "version": "0.3.3-next.32f4d87.0",
4
4
  "description": "The core module of the output framework",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,4 +1,4 @@
1
- import { Client } from '@temporalio/client';
1
+ import { Client, WorkflowNotFoundError } from '@temporalio/client';
2
2
  import { WorkflowIdConflictPolicy } from '@temporalio/common';
3
3
  import { WORKFLOW_CATALOG } from '#consts';
4
4
  import { catalogId, taskQueue } from './configs.js';
@@ -8,12 +8,29 @@ const log = createChildLogger( 'Catalog' );
8
8
 
9
9
  export const startCatalog = async ( { connection, namespace, catalog } ) => {
10
10
  const client = new Client( { connection, namespace } );
11
+ const catalogWorkflowHandle = client.workflow.getHandle( catalogId );
12
+
13
+ try {
14
+ const catalogWorkflowDescription = await catalogWorkflowHandle.describe();
15
+ if ( !catalogWorkflowDescription.closeTime ) {
16
+ log.info( 'Completing previous catalog workflow...' );
17
+ await catalogWorkflowHandle.executeUpdate( 'complete', { args: [] } );
18
+ }
19
+ } catch ( error ) {
20
+ // When "not found", it's either a cold start or the catalog was already stopped/terminated, ignore it.
21
+ // Otherwise, create a log and try the next operation:
22
+ // A. If the workflow is still running, the start() will fail and throw;
23
+ // B. If the workflow is no running, the start() will succeed, and the error was transient;
24
+ if ( !( error instanceof WorkflowNotFoundError ) ) {
25
+ log.warn( 'Error interacting with previous catalog workflow', { error } );
26
+ }
27
+ }
11
28
 
12
29
  log.info( 'Starting catalog workflow...' );
13
30
  await client.workflow.start( WORKFLOW_CATALOG, {
14
31
  taskQueue,
15
- workflowId: catalogId,
16
- workflowIdConflictPolicy: WorkflowIdConflictPolicy.TERMINATE_EXISTING,
32
+ workflowId: catalogId, // use the name of the task queue as the catalog name, ensuring uniqueness
33
+ workflowIdConflictPolicy: WorkflowIdConflictPolicy.FAIL,
17
34
  args: [ catalog ]
18
35
  } );
19
36
  };
@@ -1,4 +1,5 @@
1
1
  import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { WorkflowNotFoundError } from '@temporalio/client';
2
3
 
3
4
  const mockLog = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
4
5
  vi.mock( '#logger', () => ( { createChildLogger: () => mockLog } ) );
@@ -9,18 +10,25 @@ const catalogId = 'test-catalog';
9
10
  const taskQueue = 'test-queue';
10
11
  vi.mock( './configs.js', () => ( { catalogId, taskQueue } ) );
11
12
 
13
+ const describeMock = vi.fn();
14
+ const executeUpdateMock = vi.fn();
12
15
  const workflowStartMock = vi.fn().mockResolvedValue( undefined );
13
16
  vi.mock( '@temporalio/client', async importOriginal => {
14
17
  const actual = await importOriginal();
15
18
  return {
16
19
  ...actual,
17
20
  Client: vi.fn().mockImplementation( function () {
18
- return { workflow: { start: workflowStartMock } };
21
+ return {
22
+ workflow: {
23
+ start: workflowStartMock,
24
+ getHandle: () => ( { describe: describeMock, executeUpdate: executeUpdateMock } )
25
+ }
26
+ };
19
27
  } )
20
28
  };
21
29
  } );
22
30
 
23
- vi.mock( '@temporalio/common', () => ( { WorkflowIdConflictPolicy: { TERMINATE_EXISTING: 'TERMINATE_EXISTING' } } ) );
31
+ vi.mock( '@temporalio/common', () => ( { WorkflowIdConflictPolicy: { FAIL: 'FAIL' } } ) );
24
32
 
25
33
  describe( 'worker/start_catalog', () => {
26
34
  const mockConnection = {};
@@ -32,24 +40,79 @@ describe( 'worker/start_catalog', () => {
32
40
  workflowStartMock.mockResolvedValue( undefined );
33
41
  } );
34
42
 
35
- it( 'starts catalog workflow with TERMINATE_EXISTING policy', async () => {
43
+ it( 'when previous catalog still running: completes it then starts catalog workflow', async () => {
44
+ describeMock.mockResolvedValue( { closeTime: undefined } );
45
+ executeUpdateMock.mockResolvedValue( undefined );
46
+
47
+ const { startCatalog } = await import( './start_catalog.js' );
48
+ await startCatalog( { connection: mockConnection, namespace, catalog } );
49
+
50
+ expect( describeMock ).toHaveBeenCalled();
51
+ expect( mockLog.info ).toHaveBeenCalledWith( 'Completing previous catalog workflow...' );
52
+ expect( executeUpdateMock ).toHaveBeenCalledWith( 'complete', { args: [] } );
53
+ expect( mockLog.info ).toHaveBeenCalledWith( 'Starting catalog workflow...' );
54
+ expect( workflowStartMock ).toHaveBeenCalledWith( 'catalog', {
55
+ taskQueue,
56
+ workflowId: catalogId,
57
+ workflowIdConflictPolicy: 'FAIL',
58
+ args: [ catalog ]
59
+ } );
60
+ } );
61
+
62
+ it( 'when no previous catalog: ignores and starts catalog workflow', async () => {
63
+ describeMock.mockRejectedValue( new WorkflowNotFoundError( 'not found' ) );
64
+
65
+ const { startCatalog } = await import( './start_catalog.js' );
66
+ await startCatalog( { connection: mockConnection, namespace, catalog } );
67
+
68
+ expect( describeMock ).toHaveBeenCalled();
69
+ expect( mockLog.warn ).not.toHaveBeenCalled();
70
+ expect( mockLog.info ).toHaveBeenCalledWith( 'Starting catalog workflow...' );
71
+ expect( executeUpdateMock ).not.toHaveBeenCalled();
72
+ expect( workflowStartMock ).toHaveBeenCalledWith( 'catalog', {
73
+ taskQueue,
74
+ workflowId: catalogId,
75
+ workflowIdConflictPolicy: 'FAIL',
76
+ args: [ catalog ]
77
+ } );
78
+ } );
79
+
80
+ it( 'when previous catalog already closed: skips complete and starts catalog workflow', async () => {
81
+ describeMock.mockResolvedValue( { closeTime: '2024-01-01T00:00:00Z' } );
82
+
36
83
  const { startCatalog } = await import( './start_catalog.js' );
37
84
  await startCatalog( { connection: mockConnection, namespace, catalog } );
38
85
 
86
+ expect( describeMock ).toHaveBeenCalled();
87
+ expect( mockLog.info ).not.toHaveBeenCalledWith( 'Completing previous catalog workflow...' );
88
+ expect( executeUpdateMock ).not.toHaveBeenCalled();
39
89
  expect( mockLog.info ).toHaveBeenCalledWith( 'Starting catalog workflow...' );
40
90
  expect( workflowStartMock ).toHaveBeenCalledWith( 'catalog', {
41
91
  taskQueue,
42
92
  workflowId: catalogId,
43
- workflowIdConflictPolicy: 'TERMINATE_EXISTING',
93
+ workflowIdConflictPolicy: 'FAIL',
44
94
  args: [ catalog ]
45
95
  } );
46
96
  } );
47
97
 
48
- it( 'propagates errors from workflow.start', async () => {
49
- workflowStartMock.mockRejectedValue( new Error( 'Connection refused' ) );
98
+ it( 'when describe or complete fails with other error: logs warn and still starts catalog workflow', async () => {
99
+ describeMock.mockResolvedValue( { closeTime: undefined } );
100
+ executeUpdateMock.mockRejectedValue( new Error( 'Connection refused' ) );
50
101
 
51
102
  const { startCatalog } = await import( './start_catalog.js' );
52
- await expect( startCatalog( { connection: mockConnection, namespace, catalog } ) )
53
- .rejects.toThrow( 'Connection refused' );
103
+ await startCatalog( { connection: mockConnection, namespace, catalog } );
104
+
105
+ expect( describeMock ).toHaveBeenCalled();
106
+ expect( executeUpdateMock ).toHaveBeenCalledWith( 'complete', { args: [] } );
107
+ expect( mockLog.warn ).toHaveBeenCalledWith( 'Error interacting with previous catalog workflow', {
108
+ error: expect.any( Error )
109
+ } );
110
+ expect( mockLog.info ).toHaveBeenCalledWith( 'Starting catalog workflow...' );
111
+ expect( workflowStartMock ).toHaveBeenCalledWith( 'catalog', {
112
+ taskQueue,
113
+ workflowId: catalogId,
114
+ workflowIdConflictPolicy: 'FAIL',
115
+ args: [ catalog ]
116
+ } );
54
117
  } );
55
118
  } );