Skip to content

Latest commit

 

History

History
138 lines (95 loc) · 5.04 KB

File metadata and controls

138 lines (95 loc) · 5.04 KB

Greenbone Logo

jobQueue Package Documentation

This package provides a job queue that can be used to execute a function in a thread-safe manner. The job queue is designed to be used in situations where multiple requests of the same type need to be processed, but only one request can be processed at a time and only the most recent request needs to be processed.

If a request is added to the queue while another request is being processed, the new request will be added to the queue and processed after the current request has finished. If there is already a request in the queue, the old request will be considered obsolete and replaced by the new request. jobQueue_test.go illustrates this behaviour.

Example Usage

Here is an example of how to use the job queue:

package main

import (
"context"
"fmt"
"github.com/greenbone/opensight-golang-libraries/pkg/jobQueue"
"time"
)

func main() {
	// Define the function that will be executed for each request
	execFunc := func() error {
		time.Sleep(50 * time.Millisecond)
		return nil
	}

	// Create a new job queue with the defined function
	q := jobQueue.NewJobQueue(execFunc, context.Background())

	// Add several requests to the queue
	for i := 0; i < 10; i++ {
		req := jobQueue.Request{ID: fmt.Sprintf("Request %d", i)}
		time.Sleep(1 * time.Millisecond)
		q.AddQueueRequest(req)
	}

	// wait some time for requests to finish, just for the sake to see the output
	time.Sleep(1000 * time.Millisecond)
}

Output:

{"level":"debug","time":"2023-12-13T15:15:36+01:00","message":"Executing queue request ID: Request 0\n"}
{"level":"debug","time":"2023-12-13T15:15:36+01:00","message":"Finished queue request ID: Request 0\n"}
{"level":"debug","time":"2023-12-13T15:15:36+01:00","message":"Executing queue request ID: Request 9\n"}
{"level":"debug","time":"2023-12-13T15:15:36+01:00","message":"Finished queue request ID: Request 9\n"}

In this example, 10 requests are added. The first request is processed immediately. The second request is added to the queue. All subsequent requests will replace their predecessors in the queue resulting in only the last request being processed after the first request.


jobQueue

import "github.com/greenbone/opensight-golang-libraries/pkg/jobQueue"

Package jobQueue provides a thread-safe queue of requests to execute a predefined function. When a request is added to an empty queue, it is processed immediately. If there is already a request running, the new request will be executed after the current one. If several requests are waiting, only the last one is processed

Index

JobQueue is a thread-safe queue of requests to execute a predefined function.

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

func NewJobQueue(execFunc func() error, context context.Context) *JobQueue

NewJobQueue creates a new job queue execFunc is the function to be executed for each request that is processed context is the context of the caller

func (*JobQueue) AddQueueRequest

func (q *JobQueue) AddQueueRequest(req Request)

AddQueueRequest adds a request to the queue

The job queue is designed to be used in situations where multiple requests of the same type need to be processed, but only one request can be processed at a time and only the most recent request needs to be processed.

If a request is added to the queue while another request is being processed, the new request will be added to the queue and processed after the current request has finished. If there is already a request in the queue, the old request will be considered obsolete and replaced by the new request. [jobQueue_test.go](jobQueue_test.go) illustrates this behaviour.

type Request

Request is a request to be processed by the queue and allows to provide an ID for identification

type Request struct {
    ID string
}

Generated by gomarkdoc

License

Copyright (C) 2022-2023 [Greenbone AG][Greenbone AG]

Licensed under the GNU General Public License v3.0 or later.