teardown.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // ████████╗███████╗ █████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗███╗ ██╗
  2. // ╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔═══██╗██║ ██║████╗ ██║
  3. // ██║ █████╗ ███████║██████╔╝██║ ██║██║ ██║██║ █╗ ██║██╔██╗ ██║
  4. // ██║ ██╔══╝ ██╔══██║██╔══██╗██║ ██║██║ ██║██║███╗██║██║╚██╗██║
  5. // ██║ ███████╗██║ ██║██║ ██║██████╔╝╚██████╔╝╚███╔███╔╝██║ ╚████║
  6. // ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═══╝
  7. //
  8. module.exports = require('machine').build({
  9. friendlyName: 'Teardown',
  10. description: 'Destroys a connection manager so that a server can be shut down cleanly.',
  11. inputs: {
  12. identity: {
  13. description: 'The datastore identity to teardown.',
  14. required: true,
  15. example: '==='
  16. },
  17. datastores: {
  18. description: 'An object containing all of the data stores that have been registered.',
  19. required: true,
  20. example: '==='
  21. },
  22. modelDefinitions: {
  23. description: 'An object containing all of the model definitions that have been registered.',
  24. required: true,
  25. example: '==='
  26. }
  27. },
  28. exits: {
  29. success: {
  30. description: 'The data store was initialized successfully.'
  31. },
  32. badConfiguration: {
  33. description: 'The configuration was invalid.'
  34. }
  35. },
  36. fn: function teardown(inputs, exits) {
  37. // Dependencies
  38. var Helpers = require('./private');
  39. var datastore = inputs.datastores[inputs.identity];
  40. if (!datastore) {
  41. return exits.error(new Error('Invalid data store identity. No data store exist with that identity.'));
  42. }
  43. // ╔╦╗╔═╗╔═╗╔╦╗╦═╗╔═╗╦ ╦ ┌┬┐┌─┐┌┐┌┌─┐┌─┐┌─┐┬─┐
  44. // ║║║╣ ╚═╗ ║ ╠╦╝║ ║╚╦╝ │││├─┤│││├─┤│ ┬├┤ ├┬┘
  45. // ═╩╝╚═╝╚═╝ ╩ ╩╚═╚═╝ ╩ ┴ ┴┴ ┴┘└┘┴ ┴└─┘└─┘┴└─
  46. var manager = datastore.manager;
  47. if (!manager) {
  48. return exits.error(new Error('Missing manager for this data store. The data store may be in the process of being destroyed.'));
  49. }
  50. Helpers.connection.destroyManager(manager, function destroyManagerCb(err) {
  51. if (err) {
  52. return exits.error(err);
  53. }
  54. // Delete the rest of the data from the data store
  55. delete inputs.datastores[inputs.identity];
  56. // Delete the model definitions
  57. delete inputs.modelDefinitions[inputs.identity];
  58. return exits.success();
  59. });
  60. }
  61. });