11import fs from "fs" ;
22import * as eventBus from "../eventBus" ;
33import params from "../params" ;
4- // Modules
5- import { dockerComposeDown , dockerRm } from "../modules/docker/dockerCommands" ;
6- // Utils
4+ import {
5+ dockerComposeDown ,
6+ dockerRm ,
7+ dockerStop
8+ } from "../modules/docker/dockerCommands" ;
79import * as getPath from "../utils/getPath" ;
810import shell from "../utils/shell" ;
911import { listPackage } from "../modules/docker/listContainers" ;
10- import { restartPackageVolumes } from "../modules/docker/restartPackageVolumes" ;
1112import { logs } from "../logs" ;
1213
1314/**
@@ -33,54 +34,39 @@ export async function packageRemove({
3334 throw Error ( "Core packages cannot be cannot be removed" ) ;
3435 }
3536
36- // Only no-cores will
37+ // Only no-cores reach this block
3738 const composePath = getPath . dockerCompose ( dnp . dnpName , false ) ;
3839 const packageRepoDir = getPath . packageRepoDir ( dnp . dnpName , false ) ;
3940
40- // Necessary for eventBus dependants to know the exact list of deleted DNPs
41- let removedDnps : string [ ] = [ dnp . dnpName ] ;
41+ // [NOTE] Not necessary to close the ports since they will just
42+ // not be renewed in the next interval
4243
43- /**
44- * [NOTE] Not necessary to close the ports since they will just
45- * not be renewed in the next interval
46- */
47-
48- // Call restartPackageVolumes to safely delete dependant volumes
49- if ( deleteVolumes ) {
50- // Note: restartPackageVolumes may remove additional DNPs
51- removedDnps = (
52- await restartPackageVolumes ( {
53- dnpName : dnp . dnpName ,
54- doNotRestart : true
55- } )
56- ) . removedDnps ;
57- } else {
58- /**
59- * If there is no docker-compose, do a docker rm directly
60- * Otherwise, try to do a docker-compose down and if it fails,
61- * log to console and do docker-rm
62- */
63- if ( fs . existsSync ( composePath ) )
64- try {
65- await dockerComposeDown ( composePath , {
66- volumes : deleteVolumes ,
67- timeout
68- } ) ;
69- } catch ( e ) {
70- logs . error ( `Error on dockerComposeDown of ${ dnp . dnpName } ` , e ) ;
71- for ( const container of dnp . containers )
72- await dockerRm ( container . containerName , { volumes : deleteVolumes } ) ;
73- }
74- else {
75- for ( const container of dnp . containers )
76- await dockerRm ( container . containerName , { volumes : deleteVolumes } ) ;
44+ // If there is no docker-compose, do a docker rm directly
45+ // Otherwise, try to do a docker-compose down and if it fails,
46+ // log to console and do docker-rm
47+ let hasRemoved = false ;
48+ if ( fs . existsSync ( composePath ) ) {
49+ try {
50+ await dockerComposeDown ( composePath , {
51+ volumes : deleteVolumes ,
52+ timeout
53+ } ) ;
54+ hasRemoved = true ; // To mimic an early return
55+ } catch ( e ) {
56+ logs . error ( `Error on dockerComposeDown of ${ dnp . dnpName } ` , e ) ;
7757 }
7858 }
7959
60+ if ( ! hasRemoved ) {
61+ const containerNames = dnp . containers . map ( c => c . containerName ) ;
62+ await dockerStop ( containerNames , { time : timeout } ) ;
63+ await dockerRm ( containerNames , { volumes : deleteVolumes } ) ;
64+ }
65+
8066 // Remove DNP folder and files
8167 if ( fs . existsSync ( packageRepoDir ) ) await shell ( `rm -r ${ packageRepoDir } ` ) ;
8268
8369 // Emit packages update
8470 eventBus . requestPackages . emit ( ) ;
85- eventBus . packagesModified . emit ( { dnpNames : removedDnps , removed : true } ) ;
71+ eventBus . packagesModified . emit ( { dnpNames : [ dnp . dnpName ] , removed : true } ) ;
8672}
0 commit comments