| 409 | }); |
| 410 | |
| 411 | async function ensureTemporalNamespace(connection: NativeConnection, namespace: string) { |
| 412 | try { |
| 413 | await connection.workflowService.describeNamespace({ namespace }); |
| 414 | console.log(`✅ Temporal namespace "${namespace}" is ready`); |
| 415 | return; |
| 416 | } catch (error) { |
| 417 | if (!(isGrpcServiceError(error) && error.code === grpcStatus.NOT_FOUND)) { |
| 418 | throw error; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | console.warn(`⚠️ Temporal namespace "${namespace}" not found; attempting to create it`); |
| 423 | |
| 424 | try { |
| 425 | const defaultRetentionDays = 7; |
| 426 | await connection.workflowService.registerNamespace({ |
| 427 | namespace, |
| 428 | workflowExecutionRetentionPeriod: { |
| 429 | seconds: Long.fromNumber(defaultRetentionDays * 24 * 60 * 60), |
| 430 | nanos: 0, |
| 431 | }, |
| 432 | }); |
| 433 | console.log(`✅ Temporal namespace "${namespace}" created`); |
| 434 | } catch (error) { |
| 435 | if (isGrpcServiceError(error) && error.code === grpcStatus.ALREADY_EXISTS) { |
| 436 | console.log(`✅ Temporal namespace "${namespace}" already exists`); |
| 437 | return; |
| 438 | } |
| 439 | throw error; |
| 440 | } |
| 441 | } |