Skip to content

Commit

Permalink
feat(asset-server-plugin): Add health check
Browse files Browse the repository at this point in the history
Relates to #289
  • Loading branch information
michaelbromley committed May 5, 2020
1 parent 47a8cb9 commit 05820f4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/asset-server-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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 */
Expand All @@ -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`),
);
}
});
}
Expand Down

0 comments on commit 05820f4

Please sign in to comment.