|
| 1 | +# Power Module |
| 2 | + |
| 3 | +## ZBus |
| 4 | + |
| 5 | +Power module is available on zbus over the following channel |
| 6 | + |
| 7 | +| module | object | version | |
| 8 | +|--------|-------------------|---------------- | |
| 9 | +| power |[power](#interface)| 0.0.1 | |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +Power module handles the node power events (node uptime reporting, wake on lan, etc...) |
| 14 | + |
| 15 | +The power daemon should start after: |
| 16 | + |
| 17 | +- **boot** : to ensures the base system is initialized |
| 18 | +- **noded** : to ensure the node daemon is running and the node is registered on grid, since powerd depends on node state synchronization with the chain |
| 19 | + |
| 20 | + |
| 21 | +```yaml |
| 22 | +exec: powerd --broker unix://var/run/redis.sock |
| 23 | +after: |
| 24 | + - boot |
| 25 | + - noded |
| 26 | +``` |
| 27 | +
|
| 28 | +### Power Server |
| 29 | +
|
| 30 | +The PowerServer is responsible for managing and synchronizing the power state of a node with its target state defined on the blockchain. It continuously listens to power events , updates its internal state, and performs power actions. |
| 31 | +
|
| 32 | +```go |
| 33 | +type PowerServer struct { |
| 34 | + consumer *events.RedisConsumer |
| 35 | + substrateGateway *stubs.SubstrateGatewayStub |
| 36 | + |
| 37 | + // enabled means the node can power off! |
| 38 | + enabled bool |
| 39 | + farm pkg.FarmID |
| 40 | + node uint32 |
| 41 | + twin uint32 |
| 42 | + ut *Uptime |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +// This is the main entrypoint for the PowerServer runtime |
| 47 | +func (p *PowerServer) Start(ctx context.Context) error |
| 48 | + |
| 49 | + |
| 50 | +// event handles a single PowerTargetChangeEvent received from Redis |
| 51 | +func (p *PowerServer) event(event *pkg.PowerTargetChangeEvent) error |
| 52 | + |
| 53 | + |
| 54 | +// events manages continuous processing of power-related events |
| 55 | +// It automatically retries the event stream loop if it fails, unless the context is canceled |
| 56 | +func (p *PowerServer) events(ctx context.Context) error |
| 57 | + |
| 58 | + |
| 59 | +// powerUp sends a wake-on-LAN (WOL) signal to a target node using its MAC address |
| 60 | +func (p *PowerServer) powerUp(node *substrate.Node, reason string) error |
| 61 | + |
| 62 | +// recv listens for PowerTargetChange events from Redis and forwards each event to the event handler |
| 63 | +// It exits only when the context is canceled |
| 64 | +func (p *PowerServer) recv(ctx context.Context) error |
| 65 | + |
| 66 | + |
| 67 | +// Sets the node power state as provided or to up if power manegement is not enabled on this node |
| 68 | +// It makes sure to compare the state with on chain state to not do un-necessary transactions |
| 69 | +func (p *PowerServer) setNodePowerState(up bool) error |
| 70 | + |
| 71 | + |
| 72 | +// shutdown powers down the node when the target is down on the blockchain |
| 73 | +// It sends an uptime record first before shutting down |
| 74 | +// Only shutdown if power management is enabled |
| 75 | +func (p *PowerServer) shutdown() error |
| 76 | + |
| 77 | + |
| 78 | +// Syncs the actual node state with the target state |
| 79 | +// If target is up, and the node state is up, we do nothing |
| 80 | +// If target is up, but the node is down, we set the state to up and return |
| 81 | +// If target is down, we make sure state is down, then shutdown |
| 82 | +func (p *PowerServer) syncSelf() error |
| 83 | +``` |
| 84 | + |
| 85 | + |
0 commit comments