Browse Source

feat(asset-server-plugin): Add health check

Relates to #289
Michael Bromley 5 years ago
parent
commit
05820f43d2
1 changed files with 22 additions and 1 deletions
  1. 22 1
      packages/asset-server-plugin/src/plugin.ts

+ 22 - 1
packages/asset-server-plugin/src/plugin.ts

@@ -1,10 +1,13 @@
+import { DNSHealthIndicator, TerminusModule } from '@nestjs/terminus';
 import { Type } from '@vendure/common/lib/shared-types';
 import {
     AssetStorageStrategy,
     createProxyHandler,
+    HealthCheckRegistryService,
     Logger,
     OnVendureBootstrap,
     OnVendureClose,
+    PluginCommonModule,
     RuntimeVendureConfig,
     VendurePlugin,
 } from '@vendure/core';
@@ -119,6 +122,7 @@ import { AssetServerOptions, ImageTransformPreset } from './types';
  * @docsCategory AssetServerPlugin
  */
 @VendurePlugin({
+    imports: [PluginCommonModule, TerminusModule],
     configuration: config => AssetServerPlugin.configure(config),
 })
 export class AssetServerPlugin implements OnVendureBootstrap, OnVendureClose {
@@ -134,6 +138,11 @@ export class AssetServerPlugin implements OnVendureBootstrap, OnVendureClose {
     ];
     private static options: AssetServerOptions;
 
+    constructor(
+        private healthCheckRegistryService: HealthCheckRegistryService,
+        private dns: DNSHealthIndicator,
+    ) {}
+
     /**
      * @description
      * Set the plugin options.
@@ -178,6 +187,11 @@ export class AssetServerPlugin implements OnVendureBootstrap, OnVendureClose {
         const cachePath = path.join(AssetServerPlugin.options.assetUploadDir, this.cacheDir);
         fs.ensureDirSync(cachePath);
         this.createAssetServer();
+        const { hostname, port } = AssetServerPlugin.options;
+        // console.log('SWAGGGGGGGG!');
+        // this.healthCheckRegistryService.registerIndicatorFunction(() =>
+        //     this.dns.pingCheck('asset-server', `${hostname}:${port}`),
+        // );
     }
 
     /** @internal */
@@ -192,12 +206,19 @@ export class AssetServerPlugin implements OnVendureBootstrap, OnVendureClose {
      */
     private createAssetServer() {
         const assetServer = express();
+        assetServer.get('/health', (req, res) => {
+            res.send('ok');
+        });
         assetServer.use(this.serveStaticFile(), this.generateTransformedImage());
+
         this.server = assetServer.listen(AssetServerPlugin.options.port, () => {
             const addressInfo = this.server.address();
             if (addressInfo && typeof addressInfo !== 'string') {
                 const { address, port } = addressInfo;
-                Logger.info(`Asset server listening on ${address}:${port}`, loggerCtx);
+                Logger.info(`Asset server listening on "http://localhost:${port}"`, loggerCtx);
+                this.healthCheckRegistryService.registerIndicatorFunction(() =>
+                    this.dns.pingCheck('asset-server', `http://localhost:${port}/health`),
+                );
             }
         });
     }