GithubHelp home page GithubHelp logo

hozgan / harness Goto Github PK

View Code? Open in Web Editor NEW

This project forked from netologist/harness

0.0 0.0 0.0 8 KB

Harness is a graceful shutdown handler for Go projects

License: MIT License

Go 95.36% Makefile 4.64%

harness's Introduction

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.

harness's People

Contributors

netologist avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.