Add jobs table with ID, status, and created_at fields. POST /jobs endpoint creates pending jobs in SQLite. Worker polls every 5s for pending jobs, processes them with 2s delay, and marks as done.
35 lines
634 B
Go
35 lines
634 B
Go
package worker
|
|
|
|
import (
|
|
"alpenqueue/pkg/db"
|
|
"database/sql"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
func Start(database *sql.DB) {
|
|
go func() {
|
|
for {
|
|
jobs, err := db.GetPendingJobs(database)
|
|
if err != nil {
|
|
log.Printf("Error fetching jobs: %v", err)
|
|
time.Sleep(5 * time.Second)
|
|
continue
|
|
}
|
|
|
|
for _, job := range jobs {
|
|
log.Printf("Processing job %d", job.ID)
|
|
time.Sleep(2 * time.Second)
|
|
|
|
if err := db.MarkJobDone(database, job.ID); err != nil {
|
|
log.Printf("Error marking job %d done: %v", job.ID, err)
|
|
} else {
|
|
log.Printf("Job %d completed", job.ID)
|
|
}
|
|
}
|
|
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
}()
|
|
}
|