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: 0

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