44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/kb-search/kb/internal/api"
|
|
)
|
|
|
|
type jobStatus struct {
|
|
ID int `json:"id"`
|
|
Status string `json:"status"`
|
|
DocumentID int `json:"document_id"`
|
|
ChunkCount int `json:"chunk_count"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func waitForJob(client *api.Client, jobID int, timeout time.Duration) (*jobStatus, error) {
|
|
deadline := time.Now().Add(timeout)
|
|
for {
|
|
resp, err := client.Get(fmt.Sprintf("/api/v1/jobs/%d", jobID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := api.CheckError(resp); err != nil {
|
|
return nil, err
|
|
}
|
|
var job jobStatus
|
|
if err := api.DecodeJSON(resp, &job); err != nil {
|
|
return nil, fmt.Errorf("failed to decode job status: %w", err)
|
|
}
|
|
switch job.Status {
|
|
case "done", "skipped":
|
|
return &job, nil
|
|
case "failed":
|
|
return nil, fmt.Errorf("ingestion job %d failed: %s", jobID, job.Error)
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return nil, fmt.Errorf("timed out waiting for ingestion job %d", jobID)
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|