github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/cmd/flowd-v1alpha1/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  
     8  	"github.com/awesome-flow/flow/pkg/cast"
     9  	"github.com/awesome-flow/flow/pkg/cfg"
    10  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
    11  	pipeline "github.com/awesome-flow/flow/pkg/corev1alpha1/pipeline"
    12  	"github.com/awesome-flow/flow/pkg/types"
    13  	"github.com/awesome-flow/flow/pkg/util"
    14  	webapp "github.com/awesome-flow/flow/web/app"
    15  )
    16  
    17  func main() {
    18  	repo := cfg.NewRepository()
    19  	repo.DefineSchema(cast.ConfigSchema)
    20  
    21  	if err := util.ExecEnsure(
    22  		func() error { _, err := cfg.NewDefaultProvider(repo, 0); return err },
    23  		func() error { _, err := cfg.NewEnvProvider(repo, 10); return err },
    24  		func() error { _, err := cfg.NewYamlProvider(repo, 20); return err },
    25  		func() error { _, err := cfg.NewCliProvider(repo, 30); return err },
    26  	); err != nil {
    27  		panic(fmt.Sprintf("config init failed: %s", err.Error()))
    28  	}
    29  
    30  	config := core.NewConfig(repo)
    31  	context, err := core.NewContext(config)
    32  	if err != nil {
    33  		panic(fmt.Sprintf("failed to init context: %s", err))
    34  	}
    35  
    36  	if err := context.Start(); err != nil {
    37  		panic(fmt.Sprintf("failed to start context: %s", err))
    38  	}
    39  
    40  	logger := context.Logger()
    41  
    42  	logger.Info("initializing the pipeline")
    43  	pipeline, err := pipeline.NewPipeline(context)
    44  	if err != nil {
    45  		logger.Fatal("failed to init pipeline: %s", err)
    46  	}
    47  	logger.Info("pipeline was initialized")
    48  
    49  	logger.Info("starting the pipeline")
    50  	if err := pipeline.Start(); err != nil {
    51  		logger.Fatal("failed to start pipeline: %s", err)
    52  	}
    53  	logger.Info("pipeline is active")
    54  
    55  	syscfgval, ok := repo.Get(types.NewKey("system"))
    56  	if !ok {
    57  		logger.Fatal("failed to get system config")
    58  	}
    59  	syscfg := syscfgval.(types.CfgBlockSystem)
    60  	var adminmux *webapp.HttpMux
    61  	if syscfg.Admin.Enabled {
    62  		var err error
    63  		logger.Info("starting admin interface on %s", syscfg.Admin.Bind)
    64  		adminmux, err = webapp.NewHttpMux(context)
    65  		if err != nil {
    66  			logger.Fatal("failed to initialize admin interface: %s", err)
    67  		}
    68  		if err := adminmux.Start(); err != nil {
    69  			logger.Fatal("failed to start admin interface: %s", err)
    70  		}
    71  	}
    72  
    73  	c := make(chan os.Signal, 1)
    74  	signal.Notify(c, os.Interrupt)
    75  	<-c
    76  
    77  	logger.Info("terminating")
    78  
    79  	if adminmux != nil {
    80  		logger.Info("stopping admin interface")
    81  		if err := adminmux.Stop(); err != nil {
    82  			logger.Error("failed to stop admin interface: %s", err.Error())
    83  		} else {
    84  			logger.Info("admin interface was successfully stopped")
    85  		}
    86  	}
    87  
    88  	if err := util.ExecEnsure(
    89  		pipeline.Stop,
    90  		context.Stop,
    91  	); err != nil {
    92  		logger.Fatal("failed to terminate pipeline: %s", err)
    93  	}
    94  
    95  	os.Exit(0)
    96  }