healthcheck

package
v0.5.14 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthCheckCleanup

func HealthCheckCleanup(ctx context.Context, db *gorm.DB)

HealthCheckCleanup is a function that cleans up stale workers and work files in the database.

It first removes all workers that haven't sent a heartbeat for a certain threshold (staleThreshold). If there's an error removing the workers, it logs the error and continues.

Then, it resets the state of any jobs that are marked as being processed by a worker that no longer exists. If there's an error updating the sources, it logs the error and continues.

All database operations are retried on failure using the DoRetry function.

Parameters:

  • db: The Gorm DBNoContext connection to use for database queries.

func Register added in v0.2.43

func Register(ctx context.Context, db *gorm.DB, workerID uuid.UUID, workerType model.WorkerType, allowDuplicate bool) (alreadyRunning bool, err error)

Register registers a new worker in the database. It uses the provided context and database connection. The workerID is used to uniquely identify the worker. The workerType is the type of the worker. If allowDuplicate is set to true, it allows the registration of duplicate workers.

The function returns two values:

  • alreadyRunning: A boolean indicating if the worker is already running.
  • err: An error that will be nil if no errors occurred.

The function first gets the hostname of the machine where it's running. If it fails to get the hostname, it returns an error. Then it gets the current state of the worker using the getState function. It then creates a new worker model with the provided workerID, the current time as the last heartbeat, the hostname, and the work type and working on values from the state.

If allowDuplicate is set to false, the function checks if there are any active workers with the same work type and whose last heartbeat is not stale. If there are such workers, it sets alreadyRunning to true and returns.

Finally, it tries to create the worker in the database. If it fails, it returns an error.

func ReportHealth added in v0.2.43

func ReportHealth(ctx context.Context, db *gorm.DB, workerID uuid.UUID, workerType model.WorkerType)

ReportHealth reports the health of a worker to the database. It uses the provided context and database connection. The workerID is used to uniquely identify the worker.

The function first gets the hostname of the machine where it's running. If it fails to get the hostname, it logs an error and returns. Then it gets the current state of the worker using the getState function. It then creates a new worker model with the provided workerID, the current time as the last heartbeat, the hostname, and the work type and working on values from the state.

The function then tries to create the worker in the database or update the existing worker if one with the same ID already exists. The update will set the last heartbeat, work type, working on, and hostname fields to the values from the worker model. If the database operation fails, it logs an error.

func StartHealthCheckCleanup

func StartHealthCheckCleanup(ctx context.Context, db *gorm.DB)

StartHealthCheckCleanup continuously runs the HealthCheckCleanup function at intervals specified by the cleanupInterval. The function cleans up resources or updates the health status of various components in your application, as implemented by HealthCheckCleanup.

It is designed to be run as a background task and will continue to run until the passed context is cancelled.

Parameters:

  • ctx context.Context: The context that controls cancellations and timeouts. When the context is cancelled (e.g. during shutdown), the function returns, stopping its background cleaning task.
  • db *gorm.DB: The database connection object used by HealthCheckCleanup to interact with the database.

func StartReportHealth added in v0.2.43

func StartReportHealth(ctx context.Context, db *gorm.DB, workerID uuid.UUID, workerType model.WorkerType)

StartReportHealth continuously runs the ReportHealth function at intervals specified by the reportInterval. This function is responsible for reporting the health status of a worker to a centralized store (e.g., a database).

The health status of the worker is determined by calling the provided getState function, which should return the current state of the worker.

This function is designed to be run as a background task and will continue to run until the passed context is cancelled.

Parameters:

  • ctx context.Context: The context that controls cancellations and timeouts. When the context is cancelled (e.g. during shutdown), the function returns, stopping its background health reporting task.
  • db *gorm.DB: The database connection object used by ReportHealth to interact with the database.
  • workerID uuid.UUID: The unique identifier for the worker whose health is being reported.

Types

type State

type State struct {
	JobType   model.JobType
	WorkingOn string
}

Jump to

Keyboard shortcuts

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