Run Jobs
Enqueue a single job
run-job.go
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"github.com/labstack/echo/v4"
"github.com/go-arrower/arrower"
)
type MyJob struct {
Payload string
}
func main() {
a, _, err := arrower.New()
if err != nil {
panic(err)
}
err = a.DefaultQueue.RegisterJobFunc(func(ctx context.Context, job MyJob) error {
a.Logger.InfoContext(ctx, "process job with data", slog.String("payload", job.Payload))
return nil
})
if err != nil {
panic(err)
}
a.WebRouter.POST("/add-job", func(c echo.Context) error {
err := a.DefaultQueue.Enqueue(c.Request().Context(), MyJob{Payload: c.FormValue("payload")})
if err != nil {
return err
}
return c.NoContent(http.StatusAccepted)
})
a.WebRouter.Logger.Fatal(a.WebRouter.Start(fmt.Sprintf(":%d", a.Config.Web.Port)))
}
Enqueue the job with a HTTP POST request. This request could be triggered from a HTML form as well.
curl -X POST localhost:8080/add-job -d 'payload=custom-data'
Output
time=2024-06-15T18:47:16.518+02:00 level=INFO msg="process job with data" payload=custom-data traceID=2d0313c84d33f545a635a91e23393252 spanID=c89d79308de416e6 jobID=01J0ECQG0FXQ474E2RJS9TJTTW