|
50 | 50 | repoStore: RepoStore |
51 | 51 | maintenance: BlockMaintainer |
52 | 52 | taskpool: Taskpool |
| 53 | + started: bool # Track whether the node was started |
| 54 | + discoveryStore: Datastore # Store reference to close explicitly |
53 | 55 |
|
54 | 56 | NodePrivateKey* = libp2p.PrivateKey # alias |
55 | 57 |
|
@@ -118,24 +120,50 @@ proc start*(s: NodeServer) {.async.} = |
118 | 120 | await s.connectMarketplace() |
119 | 121 | await s.archivistNode.start() |
120 | 122 | s.restServer.start() |
| 123 | + s.started = true |
121 | 124 |
|
122 | 125 | proc stop*(s: NodeServer) {.async.} = |
123 | 126 | notice "Stopping node" |
124 | 127 |
|
125 | | - let res = await noCancel allFinishedFailed[void]( |
126 | | - @[ |
127 | | - s.restServer.stop(), |
128 | | - s.archivistNode.switch.stop(), |
129 | | - s.archivistNode.stop(), |
130 | | - s.repoStore.stop(), |
131 | | - s.maintenance.stop(), |
132 | | - ] |
133 | | - ) |
134 | | - |
135 | | - if res.failure.len > 0: |
136 | | - error "Failed to stop node", failures = res.failure.len |
137 | | - raiseAssert "Failed to stop node" |
138 | | - |
| 128 | + if not s.started: |
| 129 | + # Close the discovery store to release the LevelDB lock |
| 130 | + if not s.discoveryStore.isNil: |
| 131 | + try: |
| 132 | + discard await s.discoveryStore.close() |
| 133 | + except Exception as e: |
| 134 | + error "Failed to close discovery store", error = e.msg |
| 135 | + if not s.taskpool.isNil: |
| 136 | + s.taskpool.shutdown() |
| 137 | + return |
| 138 | + |
| 139 | + var futures: seq[Future[void]] = @[] |
| 140 | + |
| 141 | + if not s.restServer.isNil: |
| 142 | + futures.add(s.restServer.stop()) |
| 143 | + |
| 144 | + if not s.archivistNode.isNil: |
| 145 | + futures.add(s.archivistNode.switch.stop()) |
| 146 | + futures.add(s.archivistNode.stop()) |
| 147 | + |
| 148 | + if not s.repoStore.isNil: |
| 149 | + futures.add(s.repoStore.stop()) |
| 150 | + |
| 151 | + if not s.maintenance.isNil: |
| 152 | + futures.add(s.maintenance.stop()) |
| 153 | + |
| 154 | + if futures.len > 0: |
| 155 | + let res = await noCancel allFinishedFailed[void](futures) |
| 156 | + |
| 157 | + if res.failure.len > 0: |
| 158 | + error "Failed to stop node", failures = res.failure.len |
| 159 | + raiseAssert "Failed to stop node" |
| 160 | + |
| 161 | + # Close the discovery store to release the LevelDB lock |
| 162 | + if not s.discoveryStore.isNil: |
| 163 | + try: |
| 164 | + discard await s.discoveryStore.close() |
| 165 | + except Exception as e: |
| 166 | + error "Failed to close discovery store", error = e.msg |
139 | 167 | if not s.taskpool.isNil: |
140 | 168 | s.taskpool.shutdown() |
141 | 169 |
|
@@ -168,24 +196,27 @@ proc new*( |
168 | 196 | except CatchableError as exc: |
169 | 197 | raiseAssert("Failure in tp initialization:" & exc.msg) |
170 | 198 |
|
171 | | - info "Threadpool started", numThreads = tp.numThreads |
172 | | - |
173 | 199 | let discoveryDir = config.dataDir / ArchivistDhtNamespace |
174 | 200 |
|
175 | 201 | if io2.createPath(discoveryDir).isErr: |
176 | | - trace "Unable to create discovery directory for block store", |
177 | | - discoveryDir = discoveryDir |
178 | 202 | raise (ref Defect)( |
179 | 203 | msg: "Unable to create discovery directory for block store: " & discoveryDir |
180 | 204 | ) |
181 | 205 |
|
| 206 | + let discoveryProvidersDir = config.dataDir / ArchivistDhtProvidersNamespace |
| 207 | + if io2.createPath(discoveryProvidersDir).isErr: |
| 208 | + raise (ref Defect)( |
| 209 | + msg: "Unable to create discovery providers directory: " & discoveryProvidersDir |
| 210 | + ) |
| 211 | + |
182 | 212 | let |
183 | 213 | discoveryStore = Datastore( |
184 | | - LevelDbDatastore.new(config.dataDir / ArchivistDhtProvidersNamespace).expect( |
| 214 | + LevelDbDatastore.new(discoveryProvidersDir).expect( |
185 | 215 | "Should create discovery datastore!" |
186 | 216 | ) |
187 | 217 | ) |
188 | 218 |
|
| 219 | + let |
189 | 220 | discovery = Discovery.new( |
190 | 221 | switch.peerInfo.privateKey, |
191 | 222 | announceAddrs = config.listenAddrs, |
@@ -276,4 +307,6 @@ proc new*( |
276 | 307 | repoStore: repoStore, |
277 | 308 | maintenance: maintenance, |
278 | 309 | taskpool: tp, |
| 310 | + discoveryStore: discoveryStore, |
| 311 | + started: false, |
279 | 312 | ) |
0 commit comments