Просмотр исходного кода

feat(core): Export startJobQueue helper from boostrapWorker()

Michael Bromley 4 лет назад
Родитель
Сommit
d6e4af5969

+ 15 - 5
packages/core/src/bootstrap.ts

@@ -15,6 +15,7 @@ import { coreEntitiesMap } from './entity/entities';
 import { registerCustomEntityFields } from './entity/register-custom-entity-fields';
 import { setEntityIdStrategy } from './entity/set-entity-id-strategy';
 import { validateCustomFieldsConfig } from './entity/validate-custom-fields-config';
+import { JobQueueService } from './job-queue/job-queue.service';
 import { getConfigurationFunction, getEntitiesFromPlugins } from './plugin/plugin-metadata';
 import { getPluginStartupMessages } from './plugin/plugin-utils';
 
@@ -70,20 +71,28 @@ export async function bootstrap(userConfig: Partial<VendureConfig>): Promise<INe
 
 /**
  * @description
- * Bootstraps the Vendure worker. Read more about the [Vendure Worker]({{< relref "vendure-worker" >}})
+ * Bootstraps the Vendure worker. Resolves to an object containing a reference to the underlying
+ * NestJs [standalone application](https://docs.nestjs.com/standalone-applications) as well as
+ * a function used to start listening for and processing jobs in the job queue.
+ *
+ * Read more about the [Vendure Worker]({{< relref "vendure-worker" >}}).
  *
  * @example
  * ```TypeScript
  * import { bootstrapWorker } from '\@vendure/core';
  * import { config } from './vendure-config';
  *
- * bootstrapWorker(config).catch(err => {
+ * bootstrapWorker(config)
+ *   .then(worker => worker.startJobQueue())
+ *   .catch(err => {
  *     console.log(err);
- * });
+ *   });
  * ```
  * @docsCategory worker
  * */
-export async function bootstrapWorker(userConfig: Partial<VendureConfig>): Promise<INestApplicationContext> {
+export async function bootstrapWorker(
+    userConfig: Partial<VendureConfig>,
+): Promise<{ app: INestApplicationContext; startJobQueue: () => Promise<void> }> {
     const vendureConfig = await preBootstrapConfig(userConfig);
     const config = disableSynchronize(vendureConfig);
     (config.logger as any).setDefaultContext('Vendure Worker');
@@ -99,7 +108,8 @@ export async function bootstrapWorker(userConfig: Partial<VendureConfig>): Promi
     workerApp.useLogger(new Logger());
     workerApp.enableShutdownHooks();
     await validateDbTablesForWorker(workerApp);
-    return workerApp;
+    const startJobQueue = () => workerApp.get(JobQueueService).start();
+    return { app: workerApp, startJobQueue };
 }
 
 /**

+ 1 - 3
packages/create/templates/index-worker.hbs

@@ -2,9 +2,7 @@
 {{#if isTs }}import { config } from './vendure-config';{{else}}const { config } = require('./vendure-config');{{/if}}
 
 bootstrapWorker(config)
-.then(await app => {
-  await app.get(JobQueueService).start();
-})
+.then(worker => worker.startJobQueue())
 .catch(err => {
     // tslint:disable-next-line:no-console
     console.log(err);

+ 2 - 4
packages/dev-server/index-worker.ts

@@ -1,4 +1,4 @@
-import { bootstrapWorker, JobQueueService } from '@vendure/core';
+import { bootstrapWorker } from '@vendure/core';
 
 import { devConfig } from './dev-config';
 
@@ -7,9 +7,7 @@ import { devConfig } from './dev-config';
 devConfig.dbConnectionOptions = { ...devConfig.dbConnectionOptions, synchronize: false };
 
 bootstrapWorker(devConfig)
-    .then(app => {
-        app.get(JobQueueService).start();
-    })
+    .then(worker => worker.startJobQueue())
     .catch(err => {
         // tslint:disable-next-line
         console.log(err);