The tracker recorded applications but could not find jobs. A new pipeline (services/job_leads.go, job_sources.go) polls a watch list of sources on a timer or on request, files unseen postings as leads, marks which pass a keyword and location pre-filter, and has Claude score those against a single search profile through a fourth internal Python route, /internal/jobs/score. Sources are Greenhouse, Lever and Ashby company boards (public JSON) and Adzuna and Reed keyword searches (API keys). The two search fetchers are written from the providers' documentation and tested only against stubs. Leads are their own table, not a job status, and are not soft deleted: the (source_kind, external_id) index is the dedup, so a dismissed lead has to remain a row. A lead reaches the tracker only by promotion, which goes through UpsertJobApplication. That upsert moves from the capture handler into services so promotion and the fetcher can share it; the capture endpoint's behaviour is unchanged. /cv/leads is the page: inbox, watch list and profile form. The timer is off unless JOB_LEADS_ENABLED=true and never runs at start-up, so a deploy spends no Claude calls. Co-Authored-By: Claude Opus 5.5 <[email protected]>
99 lines
4.0 KiB
Go
99 lines
4.0 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Every Claude call the site makes lives in the Python service, behind an
|
|
// /internal/ route. This file is the one way the Go backend reaches those
|
|
// routes: the rowing photo reader and the job posting reader (handlers), the
|
|
// email classifier (email_sync.go) and the job lead scorer (job_leads.go). It sits in services rather than
|
|
// handlers because both packages need it and handlers already imports
|
|
// services, not the other way round.
|
|
//
|
|
// The routes are internal in a specific sense: nginx answers 404 for
|
|
// /py/internal/, and each route itself refuses a request carrying a proxy
|
|
// header, so the only way in is this direct container-to-container call. That
|
|
// is why the request below sets nothing but Content-Type.
|
|
|
|
// pythonClient bounds the wait on the Python service. Two minutes sits above
|
|
// that side's own worst case (a 30s Claude timeout, retried twice with
|
|
// backoff), so a slow Claude surfaces as Python's error rather than as a
|
|
// timeout here. One client for every route, so they share a connection pool.
|
|
var pythonClient = &http.Client{Timeout: 2 * time.Minute}
|
|
|
|
// PythonError is a failure the Python service reported in words that are safe
|
|
// and useful to show a person: the string `detail` of a 502, which is always
|
|
// one of that route's own fixed messages ("failed to parse image data", ...).
|
|
//
|
|
// Everything else — a refused connection, a 422, a 503 for a missing API key,
|
|
// a reply that will not decode — comes back as an ordinary error instead. The
|
|
// distinction is the point: a caller relays a PythonError and only logs the
|
|
// rest, because the rest describe plumbing the user can do nothing about and
|
|
// may quote internals.
|
|
type PythonError struct{ Detail string }
|
|
|
|
func (e *PythonError) Error() string { return e.Detail }
|
|
|
|
// CallPython POSTs body as JSON to baseURL+path and decodes a 200 reply into
|
|
// T. baseURL is the service's address on the Docker network with no trailing
|
|
// slash (handlers.Store.PythonURL); path is the route, starting with a slash.
|
|
//
|
|
// Any non-200 is an error. See PythonError for which ones are relayable. A
|
|
// cancelled ctx is wrapped, not replaced, so errors.Is(err, context.Canceled)
|
|
// still holds — the email pipeline relies on that to tell an abandoned sync
|
|
// from a failed email.
|
|
func CallPython[T any](ctx context.Context, baseURL, path string, body any) (T, error) {
|
|
var out T
|
|
|
|
// Note for callers sending bytes: encoding/json writes a []byte as
|
|
// standard base64, which is the form the Anthropic API wants and so what
|
|
// the Python side passes straight through.
|
|
payload, err := json.Marshal(body)
|
|
if err != nil {
|
|
return out, fmt.Errorf("encoding request for %s: %w", path, err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+path, bytes.NewReader(payload))
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := pythonClient.Do(req)
|
|
if err != nil {
|
|
return out, fmt.Errorf("calling %s: %w", path, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
// FastAPI errors are {"detail": ...}. Only a 502 carries one of the
|
|
// route's own fixed messages; a 422's detail is a list, which fails
|
|
// to decode into the string and falls through to the generic error,
|
|
// and a 503's detail names an environment variable, which is for the
|
|
// log rather than the page. The body is capped because it is only
|
|
// ever quoted in a log line.
|
|
var failure struct {
|
|
Detail string `json:"detail"`
|
|
}
|
|
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
|
|
if resp.StatusCode == http.StatusBadGateway && json.Unmarshal(raw, &failure) == nil && failure.Detail != "" {
|
|
return out, &PythonError{Detail: failure.Detail}
|
|
}
|
|
return out, fmt.Errorf("%s returned %d: %s", path, resp.StatusCode, raw)
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
// A fresh zero value, so a half-decoded T never escapes with an error.
|
|
var zero T
|
|
return zero, fmt.Errorf("decoding %s response: %w", path, err)
|
|
}
|
|
return out, nil
|
|
}
|