initialization

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

GoDoc

initialization

Is a package for initialization of the application modules. Ex. database connectors, service providers, etc.

Usage

package main

import (
	"errors"
	"log"
	"time"

	"github.com/lancer-kit/armory/initialization"
	"github.com/sirupsen/logrus"
)

func main() {
	moduleList := initialization.Modules{
		initialization.Module{
			Name:         "database_conn",
			DependsOn:    "",
			Timeout:      15 * time.Second,
			InitInterval: time.Second,
			Init: func(entry *logrus.Entry) error {
				entry.WithField("module", "database_conn").
					Info("try to initialize module")
				time.Sleep(500 * time.Millisecond)
				return errors.New("ha-ha-ha")
			},
		},

		initialization.Module{
			Name:         "rabbit",
			DependsOn:    "",
			Timeout:      15 * time.Second,
			InitInterval: time.Second,
			Init: func(entry *logrus.Entry) error {
				entry.WithField("module", "rabbit").
					Info("try to initialize module")
				time.Sleep(500 * time.Millisecond)
				return nil
			},
		},

		initialization.Module{
			Name:         "listener",
			DependsOn:    "database_conn",
			Timeout:      15 * time.Second,
			InitInterval: time.Second,
			Init: func(entry *logrus.Entry) error {
				entry.WithField("module", "listener").
					Info("try to initialize module")
				time.Sleep(500 * time.Millisecond)
				return nil
			},
		},
	}

	err := moduleList.InitAll()
	if err != nil {
		log.Fatal("Init failed with error: ", err.Error())
	}
}

Documentation

Overview

Example
moduleList := Modules{
	Module{
		Name:         "database_conn",
		DependsOn:    "",
		Timeout:      15 * time.Second,
		InitInterval: time.Second,
		Init: func(entry *logrus.Entry) error {
			entry.WithField("module", "database_conn").
				Info("try to initialize module")
			time.Sleep(500 * time.Millisecond)
			return errors.New("ha-ha-ha")
		},
	},

	Module{
		Name:         "rabbit",
		DependsOn:    "",
		Timeout:      15 * time.Second,
		InitInterval: time.Second,
		Init: func(entry *logrus.Entry) error {
			entry.WithField("module", "rabbit").
				Info("try to initialize module")
			time.Sleep(500 * time.Millisecond)
			return nil
		},
	},

	Module{
		Name:         "listener",
		DependsOn:    "database_conn",
		Timeout:      15 * time.Second,
		InitInterval: time.Second,
		Init: func(entry *logrus.Entry) error {
			entry.WithField("module", "listener").
				Info("try to initialize module")
			time.Sleep(500 * time.Millisecond)
			return nil
		},
	},
}

err := moduleList.InitAll()
if err != nil {
	log.Fatal("Init failed with error: ", err.Error())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Module

type Module struct {
	// Name unique identifier for module.
	Name string
	// DependsOn is a name of other module which should be initialized before this.
	DependsOn string
	// InitInterval is initial timeout before second attempt to perform initCall.
	InitInterval time.Duration
	// Timeout is a deadline for the initialization attempts.
	Timeout time.Duration
	// Init is a module init function.
	Init func(*logrus.Entry) error
}

Module is unit for initialization.

func (*Module) Run

func (mod *Module) Run(locks map[string]chan bool) error

Run tries to initialize the module, if the attempt failed, it will be repeated, but with an incremental delay. So it will be until the maximum initialization timeout is reached.

type Modules

type Modules []Module

Modules is a set of modules for initialization.

func (Modules) Add

func (modules Modules) Add(m *Module) Modules

Add push new module to set.

func (Modules) InitAll

func (modules Modules) InitAll() error

InitAll checks status and run initialization of all modules for the fist to last.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL