1.3 KiB
DTP Base: Services
Services are common logic implemented in a centralized location made accessible to the rest of the application to perform tasks in a common way. They live in app/services, and are scripts that export a specific structure that identifies the service and provides a way to create, start, and stop them.
Services can't reference each other in their constructors, but they can reference each other in their start
method with the caveat that the service you are referencing may not have had it's start
method called, yet. DTP loads services in one loop, then starts them in a separate loop after they are loaded.
All other service methods implemented can reference all other services with the assumption that they are started and ready to provide full service.
// myservice.js
// Copyright (C) 2021 Digital Telepresence, LLC
// License: Apache-2.0
'use strict';
const { SiteService } = require('../../lib/site-lib');
class MyService extends SiteService {
constructor (dtp) {
super(dtp, module.exports);
}
async start ( ) {
/*
* perform service initialization here
*/
}
/*
* Implement service methods here
*/
}
module.exports = {
slug: 'my-service',
name: 'myService',
create: (dtp) => { return new MyService(dtp); },
};