Skip to content

Latest commit

 

History

History
185 lines (139 loc) · 4.83 KB

README.md

File metadata and controls

185 lines (139 loc) · 4.83 KB

JobSync: Real-Time Job Status Monitoring

Table of Contents

  1. Spinning up the Server
  2. Client Library
  3. Configuration
  4. Running the Program
  5. Running Integration Tests
  6. Troubleshooting
  7. Algorithm Used

Spinning up the server

server is the package used to launch the Job Status server.

Fetch the package using:

git clone https://github.com/iamber12/job-status-monitoring.git

Run server

cd job-status-monitoring/server
go build -o ./bin/server ./cmd/main.go 
./bin/server serve

Client library

client is the Go package for interacting with a Job Status server. It provides methods to create jobs, monitor their statuses, and configure retry/backoff mechanisms for job execution workflows.

Installation

Fetch the package using:

git clone https://github.com/iamber12/job-status-monitoring.git

Usage

Importing the Package

Assuming the following project structure

├── your-project-name
    ├── go.mod                  
    ├── main.go                 
    ├── job-status-monitoring
        ├── client
            ├── client.go       

Import the package

import (
    "context"
    "fmt"
    "time"
    "your-project-name/job-status-monitoring/client"
)

Creating a Client

To create a new client instance, initialize it with the server's base URL:

jobClient := client.NewClient("http://localhost:8080")

Creating a Job

Use CreateJob to create a new job and retrieve its Job ID:

ctx := context.Background()
jobID, err := jobClient.CreateJob(ctx)
if err != nil {
    fmt.Printf("Error creating job: %v\n", err)
    return
}
fmt.Printf("Created job with ID: %s\n", jobID)

Waiting for Job Completion

Poll the API until the job completes, fails, or times out:

ctx := context.Background()
status, err := jobClient.WaitForJob(ctx, jobID)
if err != nil {
    fmt.Printf("Error waiting for job: %v\n", err)
    return
}
fmt.Printf("Job completed with status: %s\n", status.Result)

Real-Time Status Updates

Use WaitForJobWithUpdates to receive status updates in real-time during polling:

statusUpdate := make(chan string)
go func() {
    for update := range statusUpdate {
        fmt.Println(update)
    }
}()

ctx := context.Background()
status, err := jobClient.WaitForJobWithUpdates(ctx, jobID, statusUpdate)
if err != nil {
    fmt.Printf("Error waiting for job: %v\n", err)
    return
}
fmt.Printf("Job completed with status: %s\n", status.Result)

Configuration

Customize the client with the following methods:

  • SetMaxAttempts(maxAttempts int) - Default: 10
  • SetBaseDelay(baseDelay time.Duration) - Default: 100 ms
  • SetMaxDelay(maxDelay time.Duration) - Default: 10 s
  • SetTimeout(timeout time.Duration) - Default: 2 minutes

Example:

err := jobClient.SetMaxAttempts(5)
if err != nil {
    fmt.Printf("Error setting max attempts: %v\n", err)
}

Running the program

go run main.go

Running Integration Tests

To execute the integration test, use the following command:

go test -v -count=1 ./test/integration

Troubleshooting

Error: package your-project-name/job-status-monitoring/client is not in std (#GOROOT)

If you encounter this error, it means Go is trying to resolve the package from the standard library (#GOROOT) instead of your local module. To fix this issue, make sure your go.mod file includes the following entries:

require (
    your-project-name/job-status-monitoring v0.0.0
)

replace your-project-name/job-status-monitoring => ./job-status-monitoring

Algorithm Used

The client library uses a polling with exponential backoff approach to monitor job statuses.

Approach

  1. Polling: Repeatedly queries the server for job status until it completes, fails, or times out.
  2. Exponential Backoff: Wait times between requests increase exponentially with each retry, capped at a maximum delay. Random jitter is added to prevent server overload.
  3. Configurable Parameters: Users can adjust retry attempts, delays, and timeouts to fit their specific needs.

This algorithm ensures efficient resource usage and minimizes unnecessary API calls.