harness

package module
v0.0.0-...-ab833c4 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 9 Imported by: 0

README

Harness Package

The harness package provides functionality to manage and handle multiple runners concurrently. It allows graceful shutdown of these runners in response to termination signals or cancellation events.

Usage

To use the harness package, follow the steps below:

  1. Import the harness package:
import "github.com/netologist/harness"
  1. Create a new handler using the New function, passing one or more Runner instances:
runners := []harness.Runner{
    // Initialize your runners here
}

handler := harness.New(runners...)
  1. Start the handler by calling the Start method, passing a context:
ctx := context.Background()
handler.Start(ctx)
  1. Graceful shutdown:

    • If a termination signal (e.g., SIGINT or SIGTERM) is received, the handler will initiate a graceful shutdown by calling the Shutdown method on each runner.
    • If cancellation is triggered on the provided context, the handler will also initiate a graceful shutdown.

Example Use

Here's an example use case to illustrate how the harness package can be used:

package main

import (
	"context"
	"fmt"
	"github.com/netologist/harness"
	"os"
	"os/signal"
	"syscall"
)

type TestRunner struct {
}
func (r *TestRunner) Name() string {
	return "test runner"
}
func (r *TestRunner) Run(ctx context.Context) error {
	return nil
}
func (r *TestRunner) Shutdown(exitType harness.ExitType) {
	log.Printf("NAME: '%s' - EXIT_TYPE: %d", r.Name(), exitType)
}

func (r *TestRunner) OnError(err error) {
	log.Printf("NAME: '%s' - ERROR: %+v", r.Name(), err)
}

func main() {
    ctx := context.Background()

	// Create a new TestRunner instance
	testRunner := &TestRunner{
		// Initialize your runner
		// ...
	}

	// Create the handler with the runner
	harness.New(
		harness.Register(testRunner),
		harness.OnError(func(err error) {
			log.Printf("error: %+v", err)
		}),
		harness.OnCompleted(func() {
			log.Printf("successfully completed")
		}),
		harnes.SetSignal(os.Interrupt, syscall.SIGINT, syscall.SIGTERM), // if you want customise signals
	).Start(context.Background())
}

In this example, we create a custom TestRunner struct that implements the Runner interface required by the harness package. We then create a handler with the testRunner instance and start it in a separate goroutine. We handle termination signals and cancellation requests, triggering the corresponding actions to gracefully shut down the runners. Finally, we wait for the handler to complete and perform any necessary cleanup or exit operations.

Feel free to customize the example and adapt it to your specific use case.

Please note that this is a simplified example, and you will need to implement the Runner interface methods and define your custom logic within the TestRunner struct based on your requirements.

I hope this helps! Let me know if you have any further questions.

Documentation

Overview

Package harness is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExitType

type ExitType int
const (
	ExitTypeNormal ExitType = iota
	ExitTypeCancel
	ExitTypeSignal
)

type Handler

type Handler interface {
	Start(ctx context.Context)
}

func New

func New(options ...Option) Handler

type MockRunner

type MockRunner struct {
	// contains filtered or unexported fields
}

MockRunner is a mock of Runner interface.

func NewMockRunner

func NewMockRunner(ctrl *gomock.Controller) *MockRunner

NewMockRunner creates a new mock instance.

func (*MockRunner) EXPECT

func (m *MockRunner) EXPECT() *MockRunnerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockRunner) Name

func (m *MockRunner) Name() string

Name mocks base method.

func (*MockRunner) OnError

func (m *MockRunner) OnError(err error)

OnError mocks base method.

func (*MockRunner) Run

func (m *MockRunner) Run(ctx context.Context) error

Run mocks base method.

func (*MockRunner) Shutdown

func (m *MockRunner) Shutdown(exitType ExitType)

Shutdown mocks base method.

type MockRunnerMockRecorder

type MockRunnerMockRecorder struct {
	// contains filtered or unexported fields
}

MockRunnerMockRecorder is the mock recorder for MockRunner.

func (*MockRunnerMockRecorder) Name

func (mr *MockRunnerMockRecorder) Name() *gomock.Call

Name indicates an expected call of Name.

func (*MockRunnerMockRecorder) OnError

func (mr *MockRunnerMockRecorder) OnError(err interface{}) *gomock.Call

OnError indicates an expected call of OnError.

func (*MockRunnerMockRecorder) Run

func (mr *MockRunnerMockRecorder) Run(ctx interface{}) *gomock.Call

Run indicates an expected call of Run.

func (*MockRunnerMockRecorder) Shutdown

func (mr *MockRunnerMockRecorder) Shutdown(exitType interface{}) *gomock.Call

Shutdown indicates an expected call of Shutdown.

type Option

type Option func(o *options)

func OnCompleted

func OnCompleted(fnOnCompleted func()) Option

func OnError

func OnError(fnOnError func(error)) Option

func Register

func Register(runner ...Runner) Option

func SetSignal

func SetSignal(signal ...os.Signal) Option

type Runner

type Runner interface {
	Name() string
	Run(ctx context.Context) error
	Shutdown(exitType ExitType)
	OnError(err error)
}

Jump to

Keyboard shortcuts

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