AlpenQueue/cmd/alpenqueue/main.go
Soldier c395a57b38 Add recurring job scheduling with frequency
Add frequency_minutes field to schedule recurring jobs. Jobs with frequency > 0 run repeatedly at specified intervals, automatically rescheduling after each execution. One-time jobs (frequency = 0) remain unchanged. Status transitions from pending to active for recurring jobs.
2025-11-16 09:17:30 +00:00

55 lines
1.2 KiB
Go

package main
import (
"alpenqueue/pkg/db"
"alpenqueue/pkg/worker"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
func main() {
database, err := db.Init("./alpenqueue.db")
if err != nil {
log.Fatal(err)
}
defer database.Close()
worker.Start(database)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("AlpenQueue running!"))
})
http.HandleFunc("/jobs", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
WebhookURL string `json:"webhook_url"`
URL string `json:"url"`
Selector string `json:"selector"`
FrequencyMinutes int `json:"frequency_minutes"`
}
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &req)
id, err := db.CreateJob(database, req.WebhookURL, req.URL, req.Selector, req.FrequencyMinutes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "Job %d created\n", id)
})
log.Println("Server starting on :8080")
http.ListenAndServe(":8080", nil)
}