worker

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 14 Imported by: 0

README ¶

âš¡ Core Ping & Synthetic Worker Engine (internal/worker)

internal/worker executes synthetic HTTP/HTTPS response probes, inspects TLS SSL certificate expiration dates, and processes probe telemetries.


📦 Key Components

1. ExecuteHTTPProbe(targetURL string, timeout time.Duration) *HTTPProbeResult

Performs HTTP/HTTPS GET requests against target URLs, measuring status code, response latency in milliseconds, and network diagnostic messages.

2. ExecuteSSLProbe(targetURL string, timeout time.Duration) *SSLProbeResult

Establishes a TLS connection to extract server peer certificates, calculating remaining validity days and issuer authority.

3. WorkerEngine

Connects task payload handlers to database updates:

  • Records execution data to ping_logs.
  • Updates monitors.status (UP / DOWN).
  • Automatically triggers Incident workflows:
    • Creates an OPEN incident when a target transitions from UP -> DOWN.
    • Marks active incidents as RESOLVED when a target recovers from DOWN -> UP.

🧪 Testing & Verification

Automated Unit Test Suite (probe_test.go)

We use Go's standard net/http/httptest package to launch isolated in-memory HTTP mock servers and test probe handlers without external network dependencies:

Test Method Test Focus & Verification
TestExecuteHTTPProbeSuccess - Launches a mock HTTP server returning 200 OK.
- Verifies ExecuteHTTPProbe measures non-zero latency and marks IsUp = true.
TestExecuteHTTPProbeFailure - Launches a mock HTTP server returning 500 Internal Server Error.
- Verifies ExecuteHTTPProbe records status 500 and marks IsUp = false with cause diagnostics.
TestWorkerEngineProcessHTTPCheck - End-to-end integration test connecting WorkerEngine to a mock HTTP server and isolated test database (test_worker.db).
- Verifies JSON CheckPayload parsing, probe execution, PingLog record creation, and Monitor.Status state transition (PAUSED -> UP).
Running Worker Unit Tests
go test -v ./internal/worker

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var (
	ErrInvalidScheme   = errors.New("invalid URL scheme: only http and https are permitted")
	ErrEmptyHost       = errors.New("invalid URL: missing host")
	ErrSSRFForbiddenIP = errors.New("prohibited target IP address: private, loopback, or cloud metadata endpoints are not allowed")
)

Functions ¶

func ValidateSafeURL ¶

func ValidateSafeURL(targetURL string) error

ValidateSafeURL checks if a target URL is safe for synthetic probing and protects against SSRF attacks.

Types ¶

type CheckPayload ¶

type CheckPayload struct {
	MonitorID string `json:"monitor_id"`
	TargetURL string `json:"target_url"`
}

CheckPayload represents task parameter data passed into probe workers.

type HTTPProbeResult ¶

type HTTPProbeResult struct {
	StatusCode     int
	ResponseTimeMS int
	ErrorMessage   string
	IsUp           bool
}

HTTPProbeResult contains the metrics collected from an HTTP/HTTPS request probe.

func ExecuteHTTPProbe ¶

func ExecuteHTTPProbe(targetURL string, timeout time.Duration) *HTTPProbeResult

ExecuteHTTPProbe performs a synthetic HTTP/HTTPS probe against targetURL with timeout and SSRF protection.

type SSLProbeResult ¶

type SSLProbeResult struct {
	ExpirationDate *time.Time
	DaysRemaining  int
	Issuer         string
	ErrorMessage   string
	IsValid        bool
}

SSLProbeResult contains SSL/TLS certificate metadata collected from a target.

func ExecuteSSLProbe ¶

func ExecuteSSLProbe(targetURL string, timeout time.Duration) *SSLProbeResult

ExecuteSSLProbe connects via TLS to targetURL to inspect server certificate validity and expiry.

type WorkerEngine ¶

type WorkerEngine struct {
	DB       *gorm.DB
	Notifier *notifier.NotificationEngine
}

WorkerEngine processes synthetic probes and updates database telemetry.

func NewWorkerEngine ¶

func NewWorkerEngine(database *gorm.DB, notifierEngine *notifier.NotificationEngine) *WorkerEngine

NewWorkerEngine creates a new WorkerEngine instance with alerting capabilities.

func (*WorkerEngine) ProcessHTTPCheck ¶

func (w *WorkerEngine) ProcessHTTPCheck(payloadRaw []byte) error

ProcessHTTPCheck executes an HTTP uptime check for a monitor and handles incident state transitions.

func (*WorkerEngine) ProcessSSLCheck ¶

func (w *WorkerEngine) ProcessSSLCheck(payloadRaw []byte) error

ProcessSSLCheck executes an SSL certificate inspection probe for a monitor.

Jump to

Keyboard shortcuts

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