forked from cerc-io/plugeth
		
	interrupt handlers now ordered
This commit is contained in:
		
							parent
							
								
									9a06efd080
								
							
						
					
					
						commit
						2f96652bb4
					
				| @ -8,10 +8,12 @@ import ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	qml.Init(nil) |  | ||||||
| 
 |  | ||||||
| 	runtime.GOMAXPROCS(runtime.NumCPU()) | 	runtime.GOMAXPROCS(runtime.NumCPU()) | ||||||
| 
 | 
 | ||||||
|  | 	utils.HandleInterrupt() | ||||||
|  | 
 | ||||||
|  | 	qml.Init(nil) | ||||||
|  | 
 | ||||||
| 	// precedence: code-internal flag default < config file < environment variables < command line
 | 	// precedence: code-internal flag default < config file < environment variables < command line
 | ||||||
| 	Init() // parsing command line
 | 	Init() // parsing command line
 | ||||||
| 	utils.InitConfig(ConfigFile, Datadir, Identifier, "ETH") | 	utils.InitConfig(ConfigFile, Datadir, Identifier, "ETH") | ||||||
| @ -33,8 +35,10 @@ func main() { | |||||||
| 		utils.StartRpc(ethereum, RpcPort) | 		utils.StartRpc(ethereum, RpcPort) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	utils.StartEthereum(ethereum, UseSeed) |  | ||||||
| 
 |  | ||||||
| 	gui := ethui.New(ethereum, LogLevel) | 	gui := ethui.New(ethereum, LogLevel) | ||||||
| 	gui.Start(AssetPath) | 	gui.Start(AssetPath) | ||||||
|  | 
 | ||||||
|  | 	utils.StartEthereum(ethereum, UseSeed) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,7 +12,6 @@ func InitJsConsole(ethereum *eth.Ethereum) { | |||||||
|   go repl.Start() |   go repl.Start() | ||||||
|   utils.RegisterInterrupt(func(os.Signal) { |   utils.RegisterInterrupt(func(os.Signal) { | ||||||
|     repl.Stop() |     repl.Stop() | ||||||
|     ethereum.Stop() |  | ||||||
|   }) |   }) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -11,6 +11,8 @@ var logger = ethlog.NewLogger("CLI") | |||||||
| func main() { | func main() { | ||||||
| 	runtime.GOMAXPROCS(runtime.NumCPU()) | 	runtime.GOMAXPROCS(runtime.NumCPU()) | ||||||
| 
 | 
 | ||||||
|  | 	utils.HandleInterrupt() | ||||||
|  | 
 | ||||||
| 	// precedence: code-internal flag default < config file < environment variables < command line
 | 	// precedence: code-internal flag default < config file < environment variables < command line
 | ||||||
| 	Init() // parsing command line
 | 	Init() // parsing command line
 | ||||||
| 	utils.InitConfig(ConfigFile, Datadir, Identifier, "ETH") | 	utils.InitConfig(ConfigFile, Datadir, Identifier, "ETH") | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								utils/cmd.go
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								utils/cmd.go
									
									
									
									
									
								
							| @ -18,17 +18,24 @@ import ( | |||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var logger = ethlog.NewLogger("CLI") | var logger = ethlog.NewLogger("CLI") | ||||||
|  | var interruptCallbacks = []func(os.Signal){} | ||||||
| 
 | 
 | ||||||
| // Register interrupt handlers
 | // Register interrupt handlers callbacks
 | ||||||
| func RegisterInterrupt(cb func(os.Signal)) { | func RegisterInterrupt(cb func(os.Signal)) { | ||||||
|   go func() { |   interruptCallbacks = append(interruptCallbacks, cb) | ||||||
|     // Buffered chan of one is enough
 | } | ||||||
|  | 
 | ||||||
|  | // go routine that call interrupt handlers in order of registering
 | ||||||
|  | func HandleInterrupt() { | ||||||
|   c := make(chan os.Signal, 1) |   c := make(chan os.Signal, 1) | ||||||
|     // Notify about interrupts for now
 |   go func() { | ||||||
|     signal.Notify(c, os.Interrupt) |     signal.Notify(c, os.Interrupt) | ||||||
|     for sig := range c { |     for sig := range c { | ||||||
|  |       logger.Errorf("Shutting down (%v) ... \n", sig) | ||||||
|  |       for _, cb := range interruptCallbacks { | ||||||
|         cb(sig) |         cb(sig) | ||||||
|       } |       } | ||||||
|  |     } | ||||||
|   }() |   }() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -109,13 +116,12 @@ func NewEthereum(UseUPnP bool, OutboundPort string, MaxPeer int) *eth.Ethereum { | |||||||
| func StartEthereum(ethereum *eth.Ethereum, UseSeed bool) { | func StartEthereum(ethereum *eth.Ethereum, UseSeed bool) { | ||||||
|   logger.Infof("Starting Ethereum v%s", ethutil.Config.Ver) |   logger.Infof("Starting Ethereum v%s", ethutil.Config.Ver) | ||||||
|   ethereum.Start(UseSeed) |   ethereum.Start(UseSeed) | ||||||
|   // Wait for shutdown
 |  | ||||||
|   ethereum.WaitForShutdown() |  | ||||||
|   RegisterInterrupt(func(sig os.Signal) { |   RegisterInterrupt(func(sig os.Signal) { | ||||||
|     logger.Errorf("Shutting down (%v) ... \n", sig) |  | ||||||
|     ethereum.Stop() |     ethereum.Stop() | ||||||
|     ethlog.Flush() |     ethlog.Flush() | ||||||
|   }) |   }) | ||||||
|  |   // this blocks the thread
 | ||||||
|  |   ethereum.WaitForShutdown() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func ShowGenesis(ethereum *eth.Ethereum) { | func ShowGenesis(ethereum *eth.Ethereum) { | ||||||
| @ -174,9 +180,6 @@ func StartRpc(ethereum *eth.Ethereum, RpcPort int) { | |||||||
|     logger.Errorf("Could not start RPC interface (port %v): %v", RpcPort, err) |     logger.Errorf("Could not start RPC interface (port %v): %v", RpcPort, err) | ||||||
|   } else { |   } else { | ||||||
|     go ethereum.RpcServer.Start() |     go ethereum.RpcServer.Start() | ||||||
|     RegisterInterrupt(func(os.Signal) { |  | ||||||
|       ethereum.RpcServer.Stop() |  | ||||||
|     }) |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user