55# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
66# at your option. This file may not be copied, modified, or distributed except according to those terms.
77
8+ {.push raises : [].}
9+
810import
9- std/ [concurrency/ atomics, os],
11+ std/ [concurrency/ atomics, os, exitprocs ],
1012 chronicles,
1113 execution/ execution_layer,
12- consensus/ consensus_layer,
14+ consensus/ [ consensus_layer, wrapper_consensus] ,
1315 common/ utils,
1416 conf,
1517 confutils/ cli_parser,
18+ stew/ io2,
1619 beacon_chain/ conf,
1720 ../ execution_chain/ config
1821
1922# ------------------------------------------------------------------------------
2023# Private
2124# ------------------------------------------------------------------------------
2225
26+ # beacon node db lock
27+ var beaconNodeLock {.global .}: string
28+
29+ proc createBeaconNodeFileLock (filename: string ) {.raises : [IOError ].} =
30+ shouldCreatePid.store (false )
31+
32+ writeFile filename, $ os.getCurrentProcessId ()
33+ beaconNodeLock = filename
34+
35+ addExitProc proc () {.noconv .} =
36+ if beaconNodeLock.len > 0 :
37+ discard io2.removeFile (beaconNodeLock)
38+
2339# # create and configure service
2440proc startService (nimbus: var Nimbus , service: var NimbusService ) =
2541 # channel creation (shared memory)
@@ -32,7 +48,10 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
3248 isConfigRead.store (false )
3349
3450 # start thread
35- createThread (service.serviceHandler, service.serviceFunc, serviceChannel)
51+ try :
52+ createThread (service.serviceHandler, service.serviceFunc, serviceChannel)
53+ except Exception as e:
54+ fatal " error creating thread" , err = e.msg
3655
3756 let optionsTable = block :
3857 case service.layerConfig.kind
@@ -64,7 +83,10 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
6483 writeConfigString (writeOffset, opt)
6584 writeConfigString (writeOffset, arg)
6685
67- serviceChannel[].send (byteArray)
86+ try :
87+ serviceChannel[].send (byteArray)
88+ except Exception as e:
89+ fatal " channel error: " , err = e.msg
6890
6991 # wait for service read ack
7092 while not isConfigRead.load ():
@@ -115,19 +137,15 @@ proc controlCHandler() {.noconv.} =
115137
116138 notice " \t Ctrl+C pressed. Shutting down services ..."
117139
118- # WA to shutdown client(exceptions thrown)
119- # issues related with nat.nim shutdown procedure (nim-eth
120- quit 0
121-
122140 shutdownExecution ()
123141 shutdownConsensus ()
124142
125143# ------------------------------------------------------------------------------
126144# Public
127145# ------------------------------------------------------------------------------
128146
129- # Setup services
130- proc setup * (nimbus: var Nimbus ) =
147+ # Setup nimbus and services
148+ proc setup (nimbus: var Nimbus ) {. raises : [ CatchableError ].} =
131149 let
132150 executionConfigNames = extractFieldNames (NimbusConf )
133151 consensusConfigNames = extractFieldNames (BeaconNodeConf )
@@ -164,6 +182,9 @@ proc setup*(nimbus: var Nimbus) =
164182 nimbus.serviceList.add (executionService)
165183 nimbus.serviceList.add (consensusService)
166184
185+ # todo : replace path with config,datadir when creating Nimbus config
186+ createBeaconNodeFileLock (" .beacon_node.pid" )
187+
167188# # start nimbus client
168189proc run * (nimbus: var Nimbus ) =
169190 try :
@@ -181,12 +202,17 @@ proc run*(nimbus: var Nimbus) =
181202 # wait for shutdown
182203 nimbus.monitorServices ()
183204
184- # ------
205+ {.pop .}
206+ # -----
207+
185208when isMainModule :
186209 notice " Starting Nimbus"
187210
188211 setupFileLimits ()
189212
213+ # todo : replace path with config after creating Nimbus config
214+ # setupLogging(config.logLevel, config.logStdout, config.logFile)
215+
190216 var nimbus = Nimbus ()
191217 nimbus.setup ()
192218 nimbus.run ()
0 commit comments