@@ -10,6 +10,7 @@ import process from 'process';
1010import Logger from '@matrixai/logger' ;
1111import { DB } from '@matrixai/db' ;
1212import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop' ;
13+ import { WorkerManager } from '@matrixai/workers' ;
1314import * as networkUtils from './network/utils' ;
1415import KeyRing from './keys/KeyRing' ;
1516import CertManager from './keys/CertManager' ;
@@ -37,6 +38,7 @@ import * as errors from './errors';
3738import * as utils from './utils' ;
3839import * as keysUtils from './keys/utils' ;
3940import * as nodesUtils from './nodes/utils' ;
41+ import * as workersUtils from './workers/utils' ;
4042import TaskManager from './tasks/TaskManager' ;
4143
4244type NetworkConfig = {
@@ -81,6 +83,7 @@ class PolykeyAgent {
8183 proxyConfig = { } ,
8284 nodeConnectionManagerConfig = { } ,
8385 seedNodes = { } ,
86+ workers,
8487 // Optional dependencies
8588 status,
8689 schema,
@@ -134,6 +137,7 @@ class PolykeyAgent {
134137 } ;
135138 networkConfig ?: NetworkConfig ;
136139 seedNodes ?: SeedNodes ;
140+ workers ?: number ;
137141 status ?: Status ;
138142 schema ?: Schema ;
139143 keyRing ?: KeyRing ;
@@ -455,6 +459,7 @@ class PolykeyAgent {
455459 await pkAgent . start ( {
456460 password,
457461 networkConfig,
462+ workers,
458463 fresh,
459464 } ) ;
460465 logger . info ( `Created ${ this . name } ` ) ;
@@ -485,6 +490,7 @@ class PolykeyAgent {
485490 public readonly events : EventBus ;
486491 public readonly fs : FileSystem ;
487492 public readonly logger : Logger ;
493+ protected workerManager : PolykeyWorkerManagerInterface | undefined ;
488494
489495 constructor ( {
490496 nodePath,
@@ -566,10 +572,12 @@ class PolykeyAgent {
566572 public async start ( {
567573 password,
568574 networkConfig = { } ,
575+ workers,
569576 fresh = false ,
570577 } : {
571578 password : string ;
572579 networkConfig ?: NetworkConfig ;
580+ workers ?: number ;
573581 fresh ?: boolean ;
574582 } ) {
575583 try {
@@ -748,6 +756,15 @@ class PolykeyAgent {
748756 await this . notificationsManager . start ( { fresh } ) ;
749757 await this . sessionManager . start ( { fresh } ) ;
750758 await this . taskManager . startProcessing ( ) ;
759+ if ( workers != null ) {
760+ this . workerManager = await workersUtils . createWorkerManager ( {
761+ // 0 means max workers
762+ cores : workers === 0 ? undefined : workers ,
763+ logger : this . logger . getChild ( WorkerManager . name ) ,
764+ } ) ;
765+ this . vaultManager . setWorkerManager ( this . workerManager ) ;
766+ this . db . setWorkerManager ( this . workerManager ) ;
767+ }
751768 await this . status . finishStart ( {
752769 pid : process . pid ,
753770 nodeId : this . keyRing . getNodeId ( ) ,
@@ -786,6 +803,9 @@ class PolykeyAgent {
786803 await this . db ?. stop ( ) ;
787804 await this . keyRing ?. stop ( ) ;
788805 await this . schema ?. stop ( ) ;
806+ this . vaultManager . unsetWorkerManager ( ) ;
807+ this . db . unsetWorkerManager ( ) ;
808+ await this . workerManager ?. destroy ( ) ;
789809 await this . status ?. stop ( { } ) ;
790810 throw e ;
791811 }
@@ -819,6 +839,9 @@ class PolykeyAgent {
819839 await this . db . stop ( ) ;
820840 await this . keyRing . stop ( ) ;
821841 await this . schema . stop ( ) ;
842+ this . vaultManager . unsetWorkerManager ( ) ;
843+ this . db . unsetWorkerManager ( ) ;
844+ await this . workerManager ?. destroy ( ) ;
822845 await this . status . stop ( { } ) ;
823846 this . logger . info ( `Stopped ${ this . constructor . name } ` ) ;
824847 }
@@ -870,16 +893,6 @@ class PolykeyAgent {
870893 await this . schema . destroy ( ) ;
871894 this . logger . info ( `Destroyed ${ this . constructor . name } ` ) ;
872895 }
873-
874- public setWorkerManager ( workerManager : PolykeyWorkerManagerInterface ) {
875- this . db . setWorkerManager ( workerManager ) ;
876- this . vaultManager . setWorkerManager ( workerManager ) ;
877- }
878-
879- public unsetWorkerManager ( ) {
880- this . db . unsetWorkerManager ( ) ;
881- this . vaultManager . unsetWorkerManager ( ) ;
882- }
883896}
884897
885898export default PolykeyAgent ;
0 commit comments