Skip to content

Commit

Permalink
Merge pull request #16 from Prashant-Surya/feat/temporal-sdk-upgrade
Browse files Browse the repository at this point in the history
Upgrade library to support temporal sdk v1.4.0
  • Loading branch information
KurtzL authored Oct 14, 2022
2 parents 0987039 + 17c867d commit 30a232e
Show file tree
Hide file tree
Showing 19 changed files with 3,168 additions and 1,751 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,48 @@ export class AppController {
}
```

## Advanced Options

```ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TemporalModule } from 'nestjs-temporal';
import { bundleWorkflowCode, NativeConnection, Runtime } from '@temporalio/worker';
import * as path from 'path';

@Module({
imports: [
TempModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
Runtime.install({});
const temporalHost = config.get('app.temporalHost');
const connection = await NativeConnection.connect({
address: temporalHost,
});
const workflowBundle = await bundleWorkflowCode({
workflowsPath: path.join(__dirname, './workflows'),
});

return {
connection,
taskQueue: 'default',
workflowBundle,
};
},
}),
],
})
export class AppModule {}
```

## People

- Author - [Zegue kurt](https://github.com/KurtzL)
- Contributor - [Surya Prashanth](https://github.com/Prashant-Surya)
- Contributor - [AmirSaber Sharifi](https://github.com/amirsaber)
- Contributor - [J.D Nicholls](https://github.com/jdnichollsc)

## License

Expand Down
3 changes: 2 additions & 1 deletion lib/decorators/activities.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Scope, SetMetadata } from '@nestjs/common';
import { SCOPE_OPTIONS_METADATA } from '@nestjs/common/constants';
import { TEMPORAL_MODULE_ACTIVITIES } from '../temporal.constants';
import { ActivityOptions } from '@temporalio/workflow';

import { TEMPORAL_MODULE_ACTIVITIES } from '../temporal.constants';

export interface ActivitiesOptions extends ActivityOptions {
/**
* Specifies the name of the queue to subscribe to.
Expand Down
1 change: 1 addition & 0 deletions lib/decorators/activity.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SetMetadata } from '@nestjs/common';

import { TEMPORAL_MODULE_ACTIVITY } from '../temporal.constants';

export interface ActivityOptions {
Expand Down
1 change: 1 addition & 0 deletions lib/decorators/inject-temporal-client.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject } from '@nestjs/common';

import { getQueueToken } from '../utils';

export const InjectTemporalClient = (name?: string): ParameterDecorator =>
Expand Down
1 change: 1 addition & 0 deletions lib/decorators/workflow.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SetMetadata } from '@nestjs/common';

import { TEMPORAL_MODULE_WORKFLOW_METHOD } from '../temporal.constants';

export interface WorkflowMethodOptions {
Expand Down
1 change: 1 addition & 0 deletions lib/decorators/workflows.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Scope, SetMetadata } from '@nestjs/common';
import { SCOPE_OPTIONS_METADATA } from '@nestjs/common/constants';

import { TEMPORAL_MODULE_WORKFLOW } from '../temporal.constants';

export interface WorkflowsOptions {
Expand Down
4 changes: 3 additions & 1 deletion lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './temporal-module-options.interface';
export * from './shared-temporal-config.interface';
export * from './shared-worker-config.interface';
export * from './shared-runtime-config.interface';
export * from './shared-connection-config.interface';
export * from './temporal-module.interface';
26 changes: 26 additions & 0 deletions lib/interfaces/shared-connection-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FactoryProvider, ModuleMetadata, Type } from '@nestjs/common';
import { NativeConnectionOptions } from '@temporalio/worker';

export interface SharedConnectionConfigurationFactory {
createSharedConfiguration(): Promise<NativeConnectionOptions> | NativeConnectionOptions;
}

export interface SharedConnectionAsyncConfiguration
extends Pick<ModuleMetadata, 'imports'> {
/**
* Existing Provider to be used.
*/
useExisting?: Type<SharedConnectionConfigurationFactory>;
/**
* Type (class name) of provider (instance to be registered and injected).
*/
useClass?: Type<SharedConnectionConfigurationFactory>;
/**
* Factory function that returns an instance of the provider to be injected.
*/
useFactory?: (...args: any[]) => Promise<NativeConnectionOptions> | NativeConnectionOptions;
/**
* Optional list of providers to be injected into the context of the Factory function.
*/
inject?: FactoryProvider['inject'];
}
26 changes: 26 additions & 0 deletions lib/interfaces/shared-runtime-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FactoryProvider, ModuleMetadata, Type } from '@nestjs/common';
import { RuntimeOptions } from '@temporalio/worker';

export interface SharedRuntimeConfigurationFactory {
createSharedConfiguration(): Promise<RuntimeOptions> | RuntimeOptions;
}

export interface SharedRuntimeAsyncConfiguration
extends Pick<ModuleMetadata, 'imports'> {
/**
* Existing Provider to be used.
*/
useExisting?: Type<SharedRuntimeConfigurationFactory>;
/**
* Type (class name) of provider (instance to be registered and injected).
*/
useClass?: Type<SharedRuntimeConfigurationFactory>;
/**
* Factory function that returns an instance of the provider to be injected.
*/
useFactory?: (...args: any[]) => Promise<RuntimeOptions> | RuntimeOptions;
/**
* Optional list of providers to be injected into the context of the Factory function.
*/
inject?: FactoryProvider['inject'];
}
3 changes: 2 additions & 1 deletion lib/interfaces/temporal-module.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DynamicModule } from '@nestjs/common';
import { WorkerOptions } from '@temporalio/worker';
import { SharedWorkerAsyncConfiguration } from './shared-temporal-config.interface';

import { SharedWorkerAsyncConfiguration } from './shared-worker-config.interface';
import { TemporalModuleOptions } from './temporal-module-options.interface';

export interface ITemporalModule {
Expand Down
1 change: 1 addition & 0 deletions lib/temporal-metadata.accessors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable, Type } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

import {
TEMPORAL_MODULE_ACTIVITIES,
TEMPORAL_MODULE_ACTIVITY,
Expand Down
1 change: 1 addition & 0 deletions lib/temporal.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const TEMPORAL_MODULE_WORKFLOW_METHOD =
export const TEMPORAL_WORKER_CONFIG = '_temporal_worker_config';
export const TEMPORAL_CORE_CONFIG = '_temporal_core_config';
export const TEMPORAL_CLIENT_CONFIG = '_temporal_client_config';
export const TEMPORAL_CONNECTION_CONFIG = '_temporal_connection_config';
73 changes: 55 additions & 18 deletions lib/temporal.explorer.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import {
Injectable,
Logger,
OnApplicationBootstrap,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { DiscoveryService, MetadataScanner, ModuleRef } from '@nestjs/core';
import { Injector } from '@nestjs/core/injector/injector';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { TemporalMetadataAccessor } from './temporal-metadata.accessors';
import { Worker, WorkerOptions, Core, CoreOptions } from '@temporalio/worker';
import { ActivityInterface } from '@temporalio/activity';
import {
NativeConnection,
NativeConnectionOptions,
Runtime,
RuntimeOptions,
Worker,
WorkerOptions,
} from '@temporalio/worker';

import {
TEMPORAL_CONNECTION_CONFIG,
TEMPORAL_CORE_CONFIG,
TEMPORAL_WORKER_CONFIG,
} from './temporal.constants';
import { TemporalMetadataAccessor } from './temporal-metadata.accessors';

@Injectable()
export class TemporalExplorer
implements OnModuleInit, OnModuleDestroy, OnApplicationBootstrap
{
private readonly logger = new Logger(TemporalExplorer.name);
private readonly injector = new Injector();
private worker: Worker;
private timerId: ReturnType<typeof setInterval>;

constructor(
private readonly moduleRef: ModuleRef,
Expand All @@ -29,35 +40,55 @@ export class TemporalExplorer
private readonly metadataScanner: MetadataScanner,
) {}

clearInterval() {
this.timerId && clearInterval(this.timerId);
this.timerId = null;
}

async onModuleInit() {
await this.explore();
}

onModuleDestroy() {
this.worker.shutdown();
this.worker?.shutdown();
this.clearInterval();
}

onApplicationBootstrap() {
setTimeout(() => {
this.worker.run();
this.timerId = setInterval(() => {
if (this.worker) {
this.worker.run();
this.clearInterval();
}
}, 1000);
}

async explore() {
const workerConfig: WorkerOptions = this.getWorkerConfigOptions();
const coreConfig: CoreOptions = this.getCoreConfigOptions();
const workerConfig = this.getWorkerConfigOptions();
const runTimeOptions = this.getRuntimeOptions();
const connectionOptions = this.getNativeConnectionOptions();

// should contain taskQueue
if (workerConfig.taskQueue) {
const activitiesFunc: ActivityInterface = await this.handleActivities();

await Core.install(coreConfig);

const activitiesFunc = await this.handleActivities();

if (runTimeOptions) {
this.logger.verbose('Instantiating a new Core object');
Runtime.install(runTimeOptions);
}

const workerOptions = {
activities: activitiesFunc,
} as WorkerOptions;
if (connectionOptions) {
this.logger.verbose('Connecting to the Temporal server');
workerOptions.connection = await NativeConnection.connect(connectionOptions);
}

this.logger.verbose('Creating a new Worker');
this.worker = await Worker.create(
Object.assign(
{
activities: activitiesFunc,
},
workerOptions,
workerConfig,
),
);
Expand All @@ -70,16 +101,22 @@ export class TemporalExplorer
});
}

getCoreConfigOptions(name?: string): CoreOptions {
getNativeConnectionOptions(name?: string): NativeConnectionOptions {
return this.moduleRef.get(TEMPORAL_CONNECTION_CONFIG || name, {
strict: false,
});
}

getRuntimeOptions(name?: string): RuntimeOptions {
return this.moduleRef.get(TEMPORAL_CORE_CONFIG || name, { strict: false });
}

/**
*
* @returns
*/
async handleActivities(): Promise<ActivityInterface> {
const activitiesMethod: ActivityInterface = {};
async handleActivities() {
const activitiesMethod = {};

const activities: InstanceWrapper[] = this.discoveryService
.getProviders()
Expand Down
Loading

0 comments on commit 30a232e

Please sign in to comment.