-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjobs.go
135 lines (113 loc) · 2.87 KB
/
jobs.go
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
package main
import (
"context"
"os"
"sort"
"sync"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
// Job represents an OCR job
type Job struct {
ID string
DocumentID int
Status string // "pending", "in_progress", "completed", "failed"
Result string // OCR result or error message
CreatedAt time.Time
UpdatedAt time.Time
PagesDone int // Number of pages processed
}
// JobStore manages jobs and their statuses
type JobStore struct {
sync.RWMutex
jobs map[string]*Job
}
var (
logger = logrus.New()
jobStore = &JobStore{
jobs: make(map[string]*Job),
}
jobQueue = make(chan *Job, 100) // Buffered channel with capacity of 100 jobs
)
func init() {
// Initialize logger
logger.SetOutput(os.Stdout)
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logger.SetLevel(logrus.InfoLevel)
logger.WithField("prefix", "OCR_JOB")
}
func generateJobID() string {
return uuid.New().String()
}
func (store *JobStore) addJob(job *Job) {
store.Lock()
defer store.Unlock()
job.PagesDone = 0 // Initialize PagesDone to 0
store.jobs[job.ID] = job
logger.Infof("Job added: %v", job)
}
func (store *JobStore) getJob(jobID string) (*Job, bool) {
store.RLock()
defer store.RUnlock()
job, exists := store.jobs[jobID]
return job, exists
}
func (store *JobStore) GetAllJobs() []*Job {
store.RLock()
defer store.RUnlock()
jobs := make([]*Job, 0, len(store.jobs))
for _, job := range store.jobs {
jobs = append(jobs, job)
}
sort.Slice(jobs, func(i, j int) bool {
return jobs[i].CreatedAt.After(jobs[j].CreatedAt)
})
return jobs
}
func (store *JobStore) updateJobStatus(jobID, status, result string) {
store.Lock()
defer store.Unlock()
if job, exists := store.jobs[jobID]; exists {
job.Status = status
if result != "" {
job.Result = result
}
job.UpdatedAt = time.Now()
logger.Infof("Job status updated: %v", job)
}
}
func (store *JobStore) updatePagesDone(jobID string, pagesDone int) {
store.Lock()
defer store.Unlock()
if job, exists := store.jobs[jobID]; exists {
job.PagesDone = pagesDone
job.UpdatedAt = time.Now()
logger.Infof("Job pages done updated: %v", job)
}
}
func startWorkerPool(app *App, numWorkers int) {
for i := 0; i < numWorkers; i++ {
go func(workerID int) {
logger.Infof("Worker %d started", workerID)
for job := range jobQueue {
logger.Infof("Worker %d processing job: %s", workerID, job.ID)
processJob(app, job)
}
}(i)
}
}
func processJob(app *App, job *Job) {
jobStore.updateJobStatus(job.ID, "in_progress", "")
ctx := context.Background()
fullOcrText, err := app.ProcessDocumentOCR(ctx, job.DocumentID)
if err != nil {
logger.Errorf("Error processing document OCR for job %s: %v", job.ID, err)
jobStore.updateJobStatus(job.ID, "failed", err.Error())
return
}
jobStore.updateJobStatus(job.ID, "completed", fullOcrText)
logger.Infof("Job completed: %s", job.ID)
}