@@ -15,7 +15,7 @@ import fs from 'fs-extra';
1515import type { CommandResult } from '@alcalzone/pak' ;
1616import * as url from 'node:url' ;
1717
18- import { PluginHandler } from '@iobroker/plugin-base' ;
18+ import { PluginHandler , type IoPackageFile } from '@iobroker/plugin-base' ;
1919import {
2020 EXIT_CODES ,
2121 getObjectsConstructor ,
@@ -61,7 +61,7 @@ import {
6161 SYSTEM_ADMIN_GROUP ,
6262 SYSTEM_ADMIN_USER ,
6363} from '@/lib/adapter/constants.js' ;
64- import type { PluginHandlerSettings } from '@iobroker/plugin-base/types ' ;
64+ import type { PluginHandlerSettings } from '@iobroker/plugin-base' ;
6565import type {
6666 AdapterOptions ,
6767 AliasDetails ,
@@ -706,7 +706,7 @@ export class AdapterClass extends EventEmitter {
706706 /** An array of instances, that support auto subscribe */
707707 private autoSubscribe : string [ ] = [ ] ;
708708 private defaultHistory : null | string = null ;
709- private pluginHandler ?: InstanceType < typeof PluginHandler > ;
709+ private pluginHandler ?: PluginHandler ;
710710 private _reportInterval ?: null | NodeJS . Timeout ;
711711 private getPortRunning : null | InternalGetPortOptions = null ;
712712 private readonly _namespaceRegExp : RegExp ;
@@ -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- }
1507-
1508- let _reason = 'Without reason' ;
1509- let _exitCode : number ;
1497+ let shutdownStarted = false ;
1498+ const shutdownLogic : ( ) => void = ( ) => {
1499+ if ( shutdownStarted ) {
1500+ return ;
1501+ }
1502+ shutdownStarted = true ;
15101503
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- }
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+ }
15181512
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- }
1513+ let _reason = 'Without reason' ;
1514+ let _exitCode : number ;
15241515
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
@@ -11068,11 +11083,16 @@ export class AdapterClass extends EventEmitter {
1106811083 this . pluginHandler . getPluginConfig ( pluginName ) || { } ,
1106911084 thisDir ,
1107011085 ) ;
11086+ // @ts -expect-error objects and state object version conflicts that are none
1107111087 this . pluginHandler . setDatabaseForPlugin ( pluginName , this . #objects, this . #states) ;
11072- this . pluginHandler . initPlugin ( pluginName , this . adapterConfig || { } ) ;
11088+
11089+ await this . pluginHandler . initPlugin (
11090+ pluginName ,
11091+ ( this . adapterConfig || { } ) as IoPackageFile ,
11092+ ) ;
1107311093 }
1107411094 } else {
11075- if ( ! this . pluginHandler . destroy ( pluginName ) ) {
11095+ if ( ! ( await this . pluginHandler . destroy ( pluginName ) ) ) {
1107611096 this . _logger . info (
1107711097 `${ this . namespaceLog } Plugin ${ pluginName } could not be disabled. Please restart adapter to disable it.` ,
1107811098 ) ;
@@ -11489,8 +11509,9 @@ export class AdapterClass extends EventEmitter {
1148911509 if ( ! this . pluginHandler ) {
1149011510 return ;
1149111511 }
11512+ // @ts -expect-error objects and state object version conflicts that are none
1149211513 this . pluginHandler . setDatabaseForPlugins ( this . #objects, this . #states) ;
11493- await this . pluginHandler . initPlugins ( adapterConfig || { } ) ;
11514+ await this . pluginHandler . initPlugins ( ( adapterConfig || { } ) as IoPackageFile ) ;
1149411515 if ( ! this . #states || ! this . #objects || this . terminated ) {
1149511516 // if adapterState was destroyed, we should not continue
1149611517 return ;
@@ -12160,8 +12181,7 @@ export class AdapterClass extends EventEmitter {
1216012181 // @ts -expect-error
1216112182 log : this . _logger ,
1216212183 iobrokerConfig : this . _config ,
12163- // @ts -expect-error
12164- parentPackage : this . pack ,
12184+ parentPackage : this . pack ! ,
1216512185 controllerVersion,
1216612186 } ;
1216712187
0 commit comments