2015-11-05 21:57:57 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package node_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/node"
|
2022-05-26 05:27:44 +00:00
|
|
|
"github.com/ledgerwatch/erigon/node/nodecfg"
|
2015-11-05 21:57:57 +00:00
|
|
|
)
|
|
|
|
|
2020-08-03 17:40:46 +00:00
|
|
|
// SampleLifecycle is a trivial network service that can be attached to a node for
|
2015-11-05 21:57:57 +00:00
|
|
|
// life cycle management.
|
|
|
|
//
|
2020-08-03 17:40:46 +00:00
|
|
|
// The following methods are needed to implement a node.Lifecycle:
|
2022-08-10 12:04:13 +00:00
|
|
|
// - Start() error - method invoked when the node is ready to start the service
|
|
|
|
// - Stop() error - method invoked when the node terminates the service
|
2020-08-03 17:40:46 +00:00
|
|
|
type SampleLifecycle struct{}
|
2015-11-05 21:57:57 +00:00
|
|
|
|
2020-08-03 17:40:46 +00:00
|
|
|
func (s *SampleLifecycle) Start() error { fmt.Println("Service starting..."); return nil }
|
|
|
|
func (s *SampleLifecycle) Stop() error { fmt.Println("Service stopping..."); return nil }
|
2015-11-05 21:57:57 +00:00
|
|
|
|
2020-08-03 17:40:46 +00:00
|
|
|
func ExampleLifecycle() {
|
2017-04-12 14:27:23 +00:00
|
|
|
// Create a network node to run protocols with the default values.
|
2022-05-26 05:27:44 +00:00
|
|
|
stack, err := node.New(&nodecfg.Config{})
|
2015-11-05 21:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create network node: %v", err)
|
|
|
|
}
|
2019-02-07 10:40:36 +00:00
|
|
|
defer stack.Close()
|
|
|
|
|
2020-08-03 17:40:46 +00:00
|
|
|
// Create and register a simple network Lifecycle.
|
|
|
|
service := new(SampleLifecycle)
|
|
|
|
stack.RegisterLifecycle(service)
|
|
|
|
|
2015-11-05 21:57:57 +00:00
|
|
|
// Boot up the entire protocol stack, do a restart and terminate
|
|
|
|
if err := stack.Start(); err != nil {
|
|
|
|
log.Fatalf("Failed to start the protocol stack: %v", err)
|
|
|
|
}
|
2020-08-03 17:40:46 +00:00
|
|
|
if err := stack.Close(); err != nil {
|
2015-11-05 21:57:57 +00:00
|
|
|
log.Fatalf("Failed to stop the protocol stack: %v", err)
|
|
|
|
}
|
|
|
|
// Output:
|
2015-11-17 16:33:25 +00:00
|
|
|
// Service starting...
|
|
|
|
// Service stopping...
|
2015-11-05 21:57:57 +00:00
|
|
|
}
|