@@ -1494,68 +1494,83 @@ export class AdapterClass extends EventEmitter {
14941494 }
14951495 this . terminated = true ;
14961496
1497- this . pluginHandler && this . pluginHandler . destroyAll ( ) ;
1498-
1499- if ( this . _reportInterval ) {
1500- clearInterval ( this . _reportInterval ) ;
1501- this . _reportInterval = null ;
1502- }
1503- if ( this . _restartScheduleJob ) {
1504- this . _restartScheduleJob . cancel ( ) ;
1505- this . _restartScheduleJob = null ;
1506- }
1497+ let shutdownStarted = false ;
1498+ const shutdownLogic : ( ) => void = ( ) => {
1499+ if ( shutdownStarted ) {
1500+ return ;
1501+ }
1502+ shutdownStarted = true ;
15071503
1508- let _reason = 'Without reason' ;
1509- let _exitCode : number ;
1504+ if ( this . _reportInterval ) {
1505+ clearInterval ( this . _reportInterval ) ;
1506+ this . _reportInterval = null ;
1507+ }
1508+ if ( this . _restartScheduleJob ) {
1509+ this . _restartScheduleJob . cancel ( ) ;
1510+ this . _restartScheduleJob = null ;
1511+ }
15101512
1511- if ( typeof reason === 'number' ) {
1512- // Only the exit code was passed
1513- exitCode = reason ;
1514- _reason = 'Without reason' ;
1515- } else if ( reason && typeof reason === 'string' ) {
1516- _reason = reason ;
1517- }
1513+ let _reason = 'Without reason' ;
1514+ let _exitCode : number ;
15181515
1519- if ( typeof exitCode !== 'number' ) {
1520- _exitCode = ! this . _config . isInstall ? EXIT_CODES . ADAPTER_REQUESTED_TERMINATION : EXIT_CODES . NO_ERROR ;
1521- } else {
1522- _exitCode = exitCode ;
1523- }
1524-
1525- const isNotCritical =
1526- _exitCode === EXIT_CODES . ADAPTER_REQUESTED_TERMINATION ||
1527- _exitCode === EXIT_CODES . START_IMMEDIATELY_AFTER_STOP ||
1528- _exitCode === EXIT_CODES . NO_ERROR ;
1529- const text = `${ this . namespaceLog } Terminated (${ Validator . getErrorText ( _exitCode ) } ): ${ _reason } ` ;
1530- if ( isNotCritical ) {
1531- this . _logger . info ( text ) ;
1532- } else {
1533- this . _logger . warn ( text ) ;
1534- }
1535- setTimeout ( async ( ) => {
1536- // give last states some time to get handled
1537- if ( this . #states) {
1538- try {
1539- await this . #states. destroy ( ) ;
1540- } catch {
1541- // ignore
1542- }
1516+ if ( typeof reason === 'number' ) {
1517+ // Only the exit code was passed
1518+ exitCode = reason ;
1519+ _reason = 'Without reason' ;
1520+ } else if ( reason && typeof reason === 'string' ) {
1521+ _reason = reason ;
15431522 }
1544- if ( this . #objects) {
1545- try {
1546- await this . #objects. destroy ( ) ;
1547- } catch {
1548- //ignore
1549- }
1523+
1524+ if ( typeof exitCode !== 'number' ) {
1525+ _exitCode = ! this . _config . isInstall ? EXIT_CODES . ADAPTER_REQUESTED_TERMINATION : EXIT_CODES . NO_ERROR ;
1526+ } else {
1527+ _exitCode = exitCode ;
15501528 }
1551- if ( this . startedInCompactMode ) {
1552- this . emit ( 'exit' , _exitCode , reason ) ;
1553- this . #states = null ;
1554- this . #objects = null ;
1529+
1530+ const isNotCritical =
1531+ _exitCode === EXIT_CODES . ADAPTER_REQUESTED_TERMINATION ||
1532+ _exitCode === EXIT_CODES . START_IMMEDIATELY_AFTER_STOP ||
1533+ _exitCode === EXIT_CODES . NO_ERROR ;
1534+ const text = `${ this . namespaceLog } Terminated (${ Validator . getErrorText ( _exitCode ) } ): ${ _reason } ` ;
1535+ if ( isNotCritical ) {
1536+ this . _logger . info ( text ) ;
15551537 } else {
1556- process . exit ( _exitCode ) ;
1538+ this . _logger . warn ( text ) ;
15571539 }
1558- } , 500 ) ;
1540+ setTimeout ( async ( ) => {
1541+ // give last states some time to get handled
1542+ if ( this . #states) {
1543+ try {
1544+ await this . #states. destroy ( ) ;
1545+ } catch {
1546+ // ignore
1547+ }
1548+ }
1549+ if ( this . #objects) {
1550+ try {
1551+ await this . #objects. destroy ( ) ;
1552+ } catch {
1553+ //ignore
1554+ }
1555+ }
1556+ if ( this . startedInCompactMode ) {
1557+ this . emit ( 'exit' , _exitCode , reason ) ;
1558+ this . #states = null ;
1559+ this . #objects = null ;
1560+ } else {
1561+ process . exit ( _exitCode ) ;
1562+ }
1563+ } , 500 ) ;
1564+ } ;
1565+
1566+ if ( this . pluginHandler ) {
1567+ this . pluginHandler
1568+ . destroyAll ( )
1569+ . then ( ( ) => shutdownLogic ( ) )
1570+ . catch ( ( ) => shutdownLogic ( ) ) ;
1571+ } else {
1572+ shutdownLogic ( ) ;
1573+ }
15591574 }
15601575
15611576 // external signature
@@ -11071,10 +11086,10 @@ export class AdapterClass extends EventEmitter {
1107111086 thisDir ,
1107211087 ) ;
1107311088 this . pluginHandler . setDatabaseForPlugin ( pluginName , this . #objects, this . #states) ;
11074- this . pluginHandler . initPlugin ( pluginName , this . adapterConfig || { } ) ;
11089+ await this . pluginHandler . initPlugin ( pluginName , this . adapterConfig || { } ) ;
1107511090 }
1107611091 } else {
11077- if ( ! this . pluginHandler . destroy ( pluginName ) ) {
11092+ if ( ! ( await this . pluginHandler . destroy ( pluginName ) ) ) {
1107811093 this . _logger . info (
1107911094 `${ this . namespaceLog } Plugin ${ pluginName } could not be disabled. Please restart adapter to disable it.` ,
1108011095 ) ;
0 commit comments