thread

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: Apache-2.0 Imports: 2 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Thread

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

Thread provides a background, periodic thread, which invokes the given function every supplied interval.

Sample usage:

monitorFunc := func() {
    //do monitoring logic
}
monitor := thread.New(log, "Health Monitor", time.Minute*2, monitorFunc)
monitor.Start()
defer monitor.Stop()
....
Example

ExampleThread is a testable example for the thread package. when executed with 'go test', the test will fail, if the 'output' remark, at the end of the function is not correct.

package main

import (
	"fmt"
	"io/ioutil"
	"time"

	"github.com/openshift/assisted-service/pkg/thread"
	"github.com/sirupsen/logrus"
)

var counter uint64

func useThread() {
	log := logrus.New()
	log.Out = ioutil.Discard
	counter = 0

	threadFunction := func() {
		counter += 1
	}
	m := thread.New(log, "health-monitor", time.Millisecond*100, threadFunction)

	m.Start()
	defer m.Stop()
	time.Sleep(time.Second * 1)
}

// ExampleThread is a testable example for the thread package.
// when executed with 'go test', the test will fail, if the 'output' remark, at the end of the function is not
// correct.
func main() {
	useThread()
	passed := counter <= 9 || counter <= 11
	fmt.Println(passed)
}
Output:
true

func New

func New(log logrus.FieldLogger, name string, interval time.Duration, exec func()) *Thread

func (*Thread) Start

func (t *Thread) Start()

Start thread

func (*Thread) Stop

func (t *Thread) Stop()

Stop thread

Jump to

Keyboard shortcuts

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