- Spinning up the Server
- Client Library
- Configuration
- Running the Program
- Running Integration Tests
- Troubleshooting
- Algorithm Used
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
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.
Fetch the package using:
git clone https://github.com/iamber12/job-status-monitoring.git
├── your-project-name
├── go.mod
├── main.go
├── job-status-monitoring
├── client
├── client.go
import (
"context"
"fmt"
"time"
"your-project-name/job-status-monitoring/client"
)
To create a new client instance, initialize it with the server's base URL:
jobClient := client.NewClient("http://localhost:8080")
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)
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)
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)
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)
}
go run main.go
To execute the integration test, use the following command:
go test -v -count=1 ./test/integration
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
The client library uses a polling with exponential backoff approach to monitor job statuses.
- Polling: Repeatedly queries the server for job status until it completes, fails, or times out.
- Exponential Backoff: Wait times between requests increase exponentially with each retry, capped at a maximum delay. Random jitter is added to prevent server overload.
- 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.