Add Tasks
This commit is contained in:
parent
5e15311d49
commit
a0f3d5e439
3 changed files with 151 additions and 0 deletions
|
@ -74,6 +74,7 @@ func Start(_conf config.Config, tmplFS embed.FS, staticFS embed.FS, migrationsFS
|
|||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/worker", handleWorkerWebsocket)
|
||||
mux.Handle("/static/", fs)
|
||||
mux.HandleFunc("/tasks", handleTask)
|
||||
mux.HandleFunc("/scan/{id}", handleScan)
|
||||
mux.HandleFunc("/libraries/{id}", handleLibrary)
|
||||
mux.HandleFunc("/libraries", handleLibraries)
|
||||
|
|
110
server/task.go
Normal file
110
server/task.go
Normal file
|
@ -0,0 +1,110 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type TaskData struct {
|
||||
Libraries []Library
|
||||
Tasks []Task
|
||||
}
|
||||
|
||||
func handleTask(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
err := createTask(r.Context(), r)
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Create Task", "err", err)
|
||||
http.Error(w, "Error Create Library: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
data := TaskData{}
|
||||
|
||||
rows, err := db.Query(r.Context(), "SELECT id, name, path, enable FROM libraries WHERE enable = $1", true)
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Query Libraries", "err", err)
|
||||
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
libraries, err := pgx.CollectRows[Library](rows, pgx.RowToStructByName[Library])
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Collect Rows", "err", err)
|
||||
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Libraries = libraries
|
||||
|
||||
rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, t.status AS status, f.path AS file FROM tasks t INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id")
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Query Tasks", "err", err)
|
||||
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tasks, err := pgx.CollectRows[Task](rows, pgx.RowToStructByName[Task])
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Collect Tasks", "err", err)
|
||||
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data.Tasks = tasks
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
err = templates.ExecuteTemplate(&buf, constants.TASK_TEMPLATE_NAME, data)
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Executing Task Template", "err", err)
|
||||
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = w.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createTask(ctx context.Context, r *http.Request) error {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Parseing Form: %w", err)
|
||||
}
|
||||
library := r.FormValue("library")
|
||||
typ := r.FormValue("type")
|
||||
slog.Info("Got Task Create", "library", library, "type", typ)
|
||||
|
||||
rows, err := db.Query(r.Context(), "SELECT id, path, size, missing FROM files where library_id = $1 AND missing = $2", library, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Query Files: %w", err)
|
||||
}
|
||||
files, err := pgx.CollectRows[File](rows, pgx.RowToStructByName[File])
|
||||
if err != nil {
|
||||
return fmt.Errorf("Collect Files: %w", err)
|
||||
}
|
||||
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Begin Transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
for _, file := range files {
|
||||
_, err = db.Exec(r.Context(), "INSERT INTO tasks (file_id, type, status) VALUES ($1,$2,$3)", file.ID, typ, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Inserting Task: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Committing Transcation: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
40
tmpl/tasks.tmpl
Normal file
40
tmpl/tasks.tmpl
Normal file
|
@ -0,0 +1,40 @@
|
|||
{{template "head"}}
|
||||
<h2>New Tasks</h2>
|
||||
<form method="POST">
|
||||
<label for="library">Library:</label><br />
|
||||
<select id="library" name="library">
|
||||
{{range $l := .Libraries}}
|
||||
<option value="{{$l.ID}}">{{$l.Name}}</option>
|
||||
{{end}}
|
||||
</select><br />
|
||||
<label for="type">Type</label><br />
|
||||
<select id="type" name="type">
|
||||
<option value="0">Health Check</option>
|
||||
</select> <br />
|
||||
<input type="submit">
|
||||
</form>
|
||||
|
||||
<h2>Tasks</h2>
|
||||
<div class="task-list">
|
||||
<table>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Type</th>
|
||||
<th>File</th>
|
||||
</tr>
|
||||
{{range $t := .Tasks}}
|
||||
<tr onclick="window.location='/tasks/{{ $t.ID }}';">
|
||||
<td>
|
||||
{{ $t.ID }}
|
||||
</td>
|
||||
<td>
|
||||
{{ $t.Type }}
|
||||
</td>
|
||||
<td>
|
||||
{{ $t.File }}
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</div>
|
||||
{{template "tail"}}
|
Loading…
Add table
Reference in a new issue