Repository navigation
Expand file tree
/
Copy pathworker.go
More file actions
361 lines (326 loc) · 10.8 KB
/
Copy pathworker.go
File metadata and controls
361 lines (326 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
// Bounds on the external tools the worker shells out to. Without these, a
// single unresponsive remote or a pathological repository stalls ingestion
// permanently, because the queue has one consumer.
const (
lsRemoteTimeout = 15 * time.Second
cloneTimeout = 15 * time.Minute
extractTimeout = 30 * time.Minute
)
// cliConfigMutex serializes CLI invocations. The Kotlin CLI keeps its
// configuration in a single directory beside the jar and calls resetAndSave()
// on every headless run (cli/src/main/kotlin/app/Main.kt), so two concurrent
// runs would clobber each other's credentials and repo list.
var cliConfigMutex sync.Mutex
type UserSyncStatus struct {
IsSyncing bool `json:"is_syncing"`
CurrentRepo string `json:"current_repo,omitempty"`
CompletedCount int `json:"completed_count"`
TotalCount int `json:"total_count"`
LastSyncedAt int64 `json:"last_synced_at,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}
type SyncTracker struct {
mu sync.RWMutex
status map[string]*UserSyncStatus
}
var globalSyncTracker = &SyncTracker{
status: make(map[string]*UserSyncStatus),
}
var (
inFlightMu sync.Mutex
inFlightRepos = make(map[string]bool)
)
func (st *SyncTracker) StartBatch(email string, count int) {
st.mu.Lock()
defer st.mu.Unlock()
s, ok := st.status[email]
if !ok || !s.IsSyncing {
s = &UserSyncStatus{
IsSyncing: true,
TotalCount: count,
CompletedCount: 0,
}
st.status[email] = s
} else {
s.TotalCount = count
if s.CompletedCount > s.TotalCount {
s.CompletedCount = 0
}
}
}
func (st *SyncTracker) StartJob(email, repoName string) {
st.mu.Lock()
defer st.mu.Unlock()
s, ok := st.status[email]
if !ok {
s = &UserSyncStatus{
IsSyncing: true,
TotalCount: 1,
}
st.status[email] = s
}
s.IsSyncing = true
s.CurrentRepo = repoName
}
func (st *SyncTracker) FinishJob(email string, err error) {
st.mu.Lock()
defer st.mu.Unlock()
s, ok := st.status[email]
if !ok {
return
}
s.CompletedCount++
s.LastSyncedAt = time.Now().Unix()
if err != nil {
s.ErrorMessage = err.Error()
}
if s.CompletedCount >= s.TotalCount {
s.IsSyncing = false
s.CurrentRepo = ""
}
}
func (st *SyncTracker) GetStatus(email string) UserSyncStatus {
st.mu.RLock()
defer st.mu.RUnlock()
if s, ok := st.status[email]; ok {
return *s
}
var lastSync int64
if db != nil && email != "" {
_ = db.QueryRow(`
SELECT COALESCE(MAX(last_synced_at), 0)
FROM repos r
JOIN commits c ON c.repo_rehash = r.rehash
WHERE c.author_email = $1`, email).Scan(&lastSync)
}
return UserSyncStatus{
IsSyncing: false,
LastSyncedAt: lastSync,
}
}
type IngestionJob struct {
RepoURL string
RepoName string
UserEmail string
GitHubPushedAt string
}
var JobQueue = make(chan IngestionJob, 500)
func EnqueueJob(job IngestionJob) bool {
inFlightMu.Lock()
defer inFlightMu.Unlock()
if inFlightRepos[job.RepoURL] {
slog.Debug("Job already queued or in progress, skipping duplicate", "repo_url", job.RepoURL)
return false
}
select {
case JobQueue <- job:
inFlightRepos[job.RepoURL] = true
slog.Info("Enqueued repository ingestion job", "repo_url", job.RepoURL, "repo_name", job.RepoName, "user_email", job.UserEmail)
return true
default:
slog.Warn("Job queue full, dropping repository ingestion job", "repo_url", job.RepoURL, "repo_name", job.RepoName, "user_email", job.UserEmail)
return false
}
}
func StartWorker(ctx context.Context) {
slog.Info("Starting background ingestion worker")
for {
select {
case <-ctx.Done():
slog.Info("Background ingestion worker shutting down")
return
case job := <-JobQueue:
// Deliberately detached from ctx: on shutdown the loop stops
// accepting new jobs, but the job already in flight is allowed to
// finish (bounded by its own timeouts) rather than leaving a
// half-written clone and a partial ingest. main() waits for it.
processJob(context.Background(), job)
}
}
}
// resolveJarPath locates the Kotlin extractor jar and rejects placeholder
// files. A zero-length jar passes a naive existence check but makes every
// `java -jar` invocation fail at runtime, which is how a missing build step
// used to surface — one failed job at a time instead of at startup.
func resolveJarPath() (string, error) {
candidates := []string{
"/app/sourcerer-app.jar",
filepath.Join("..", "cli", "build", "libs", "sourcerer-app.jar"),
}
if env := os.Getenv("SOURCERER_JAR_PATH"); env != "" {
candidates = append([]string{env}, candidates...)
}
var problems []string
for _, path := range candidates {
stat, err := os.Stat(path)
switch {
case err != nil:
problems = append(problems, err.Error())
case stat.IsDir():
problems = append(problems, path+": is a directory, not a jar (Docker creates one when a bind-mount source is missing)")
case stat.Size() == 0:
problems = append(problems, path+": empty file; build the CLI jar first")
default:
return path, nil
}
}
// Report every candidate: the first one failing is the interesting case in
// Docker, and reporting only the last hides it behind the dev-mode path.
return "", fmt.Errorf("no usable sourcerer-app.jar: %s", strings.Join(problems, "; "))
}
func computeCommitRehash(gitSHA string) string {
hash := sha256.Sum256([]byte(strings.TrimSpace(gitSHA)))
return hex.EncodeToString(hash[:])
}
func getRemoteHeadSHA(parent context.Context, repoURL string) string {
ctx, cancel := context.WithTimeout(parent, lsRemoteTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "git", "ls-remote", repoURL, "HEAD")
out, err := cmd.Output()
if err != nil {
slog.Warn("git ls-remote failed", "repo_url", repoURL, "error", err)
return ""
}
parts := strings.Fields(string(out))
if len(parts) > 0 {
return parts[0]
}
return ""
}
func processJob(ctx context.Context, job IngestionJob) {
slog.Info("Processing repository ingestion job", "repo_url", job.RepoURL, "repo_name", job.RepoName, "user_email", job.UserEmail)
globalSyncTracker.StartJob(job.UserEmail, job.RepoName)
var jobErr error
defer func() {
inFlightMu.Lock()
delete(inFlightRepos, job.RepoURL)
inFlightMu.Unlock()
globalSyncTracker.FinishJob(job.UserEmail, jobErr)
}()
// Step 1: Lightweight remote HEAD check (git ls-remote takes ~100ms and transfers a few bytes)
remoteHeadSHA := getRemoteHeadSHA(ctx, job.RepoURL)
var headRehash string
if remoteHeadSHA != "" {
headRehash = computeCommitRehash(remoteHeadSHA)
if db != nil {
// Check if this latest commit is already indexed in commits or recorded as last_commit_rehash
var exists bool
err := db.QueryRow(`
SELECT EXISTS (
SELECT 1 FROM commits WHERE rehash = $1
) OR EXISTS (
SELECT 1 FROM repos WHERE (repo_url = $2 OR repo_name = $3) AND last_commit_rehash = $1
)`, headRehash, job.RepoURL, job.RepoName).Scan(&exists)
if err == nil && exists {
slog.Info("Skipping repository ingestion: latest remote HEAD commit already indexed",
"repo", job.RepoName,
"head_sha", remoteHeadSHA,
"head_rehash", headRehash)
// Update sync timestamps without cloning or running JVM extractor
_, _ = db.Exec(`
UPDATE repos
SET last_commit_rehash = $1,
last_synced_at = $2,
github_pushed_at = COALESCE(NULLIF($3, ''), github_pushed_at),
repo_url = COALESCE(NULLIF($4, ''), repo_url),
repo_name = COALESCE(NULLIF($5, ''), repo_name)
WHERE repo_url = $4 OR repo_name = $5`,
headRehash, time.Now().Unix(), job.GitHubPushedAt, job.RepoURL, job.RepoName)
return
}
}
}
// Step 2: Full ingestion needed — Create temporary directory for cloning
tmpDir, err := os.MkdirTemp("", "sourcerer-*")
if err != nil {
slog.Error("Failed to create temporary directory for clone", "error", err)
return
}
defer os.RemoveAll(tmpDir)
// Clone repository (full clone to preserve complete commit telemetry).
// git is told never to prompt: a repository that has become private would
// otherwise block on a credential prompt until the timeout expires.
slog.Info("Cloning repository", "repo_url", job.RepoURL, "dir", tmpDir)
cloneCtx, cancelClone := context.WithTimeout(ctx, cloneTimeout)
defer cancelClone()
cmdClone := exec.CommandContext(cloneCtx, "git", "clone", job.RepoURL, tmpDir)
cmdClone.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "GIT_ASKPASS=", "GCM_INTERACTIVE=never")
cmdClone.Stdout = os.Stdout
cmdClone.Stderr = os.Stderr
if err := cmdClone.Run(); err != nil {
if cloneCtx.Err() == context.DeadlineExceeded {
slog.Error("Clone timed out", "repo_url", job.RepoURL, "timeout", cloneTimeout)
} else {
slog.Error("Failed to clone repository", "repo_url", job.RepoURL, "error", err)
}
return
}
jarPath, err := resolveJarPath()
if err != nil {
slog.Error("Cannot run ingestion CLI", "error", err)
return
}
internalToken := os.Getenv("API_INTERNAL_TOKEN")
if internalToken == "" {
internalToken = "dummy_token"
}
slog.Info("Running headless ingestion CLI", "path", tmpDir, "user_email", job.UserEmail)
extractCtx, cancelExtract := context.WithTimeout(ctx, extractTimeout)
defer cancelExtract()
// The CLI rewrites its shared on-disk config on every run, so only one may
// execute at a time.
cliConfigMutex.Lock()
cmdJava := exec.CommandContext(extractCtx, "java", "-jar", jarPath,
"--headless",
"--path", tmpDir,
"--username", job.UserEmail,
"--password", internalToken)
cmdJava.Stdout = os.Stdout
cmdJava.Stderr = os.Stderr
runErr := cmdJava.Run()
cliConfigMutex.Unlock()
if runErr != nil {
if extractCtx.Err() == context.DeadlineExceeded {
slog.Error("Ingestion CLI timed out", "repo_url", job.RepoURL, "timeout", extractTimeout)
} else {
slog.Error("Failed to run Java CLI ingestion", "repo_url", job.RepoURL, "error", runErr)
}
return
}
// Step 3: Record sync completion in PostgreSQL
if db != nil && headRehash != "" {
var repoRehash string
err := db.QueryRow("SELECT repo_rehash FROM commits WHERE rehash = $1 LIMIT 1", headRehash).Scan(&repoRehash)
if err == nil && repoRehash != "" {
_, err = db.Exec(`
UPDATE repos
SET last_commit_rehash = $1,
last_synced_at = $2,
github_pushed_at = COALESCE(NULLIF($3, ''), github_pushed_at),
repo_url = COALESCE(NULLIF($4, ''), repo_url),
repo_name = COALESCE(NULLIF($5, ''), repo_name)
WHERE rehash = $6`,
headRehash, time.Now().Unix(), job.GitHubPushedAt, job.RepoURL, job.RepoName, repoRehash)
if err != nil {
slog.Warn("Failed to update repo sync metadata", "repo_url", job.RepoURL, "error", err)
}
} else {
slog.Warn("Could not find repo_rehash for commit", "head_rehash", headRehash, "repo_url", job.RepoURL, "error", err)
}
}
slog.Info("Successfully ingested repository", "repo_url", job.RepoURL, "user_email", job.UserEmail)
}