mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-28 01:51:12 +02:00
This PR introduces dependencies between the services. Now each service has two virtual events associated with it: 'up' (running and healthy) and 'down' (finished or failed). These events are used to establish correct order via conditions abstraction. Service image unpacking was moved into 'pre' stage simplifying `init/main.go`, service images are now closer to the code which runs the service itself. Step 'pre' now runs after 'wait' step, and service dependencies are now mixed into other conditions of 'wait' step on startup. Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
package system
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/talos-systems/talos/internal/app/init/pkg/system/conditions"
|
|
)
|
|
|
|
// StateEvent is a service event (e.g. 'up', 'down')
|
|
type StateEvent string
|
|
|
|
// Service event list
|
|
const (
|
|
StateEventUp = StateEvent("up")
|
|
StateEventDown = StateEvent("down")
|
|
)
|
|
|
|
type serviceCondition struct {
|
|
event StateEvent
|
|
service string
|
|
}
|
|
|
|
func (sc *serviceCondition) Wait(ctx context.Context) error {
|
|
instance.mu.Lock()
|
|
svcrunner := instance.State[sc.service]
|
|
instance.mu.Unlock()
|
|
|
|
if svcrunner == nil {
|
|
return errors.Errorf("service %q is not registered", sc.service)
|
|
}
|
|
|
|
notifyCh := make(chan struct{}, 1)
|
|
svcrunner.Subscribe(sc.event, notifyCh)
|
|
defer svcrunner.Unsubscribe(sc.event, notifyCh)
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-notifyCh:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (sc *serviceCondition) String() string {
|
|
return fmt.Sprintf("service %q to be %q", sc.service, string(sc.event))
|
|
}
|
|
|
|
// WaitForService waits for service to reach some state event
|
|
func WaitForService(event StateEvent, service string) conditions.Condition {
|
|
return &serviceCondition{event, service}
|
|
}
|