background

package
v0.15.7 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2024 License: ISC Imports: 1 Imported by: 3

Documentation

Overview

Package background - Simple skeleton for background processes

Example
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"time"

	"github.com/bitmark-inc/bitmarkd/background"
)

type theState struct {
	count int
}

func main() {

	proc := &theState{
		count: 10,
	}

	// list of background processes to start
	processes := background.Processes{
		proc,
	}

	p := background.Start(processes, nil)
	time.Sleep(time.Second)
	p.Stop()
}

func (state *theState) Run(args interface{}, shutdown <-chan struct{}) {

	fmt.Printf("initialise\n")

loop:
	for {
		select {
		case <-shutdown:
			break loop
		default:
		}

		state.count += 1
		time.Sleep(time.Millisecond)
	}

	fmt.Printf("finalise\n")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Process

type Process interface {
	Run(args interface{}, shutdown <-chan struct{})
}

Process - type signature for background process and type that implements this Run is a process

type Processes

type Processes []Process

Processes - list of processes to start

type T

type T struct {
	sync.WaitGroup
	// contains filtered or unexported fields
}

T - handle type for the stop

func Start

func Start(processes Processes, args interface{}) *T

Start - start up a set of background processes all with the same arg value

func (*T) Stop added in v0.2.0

func (t *T) Stop()

Stop - stop a set of background processes

func (*T) StopAndWait added in v0.12.0

func (t *T) StopAndWait()

StopAndWait will notify all processes to shutdown by closing shutdown channel and wait until all processes be stopped.

Jump to

Keyboard shortcuts

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