A simple task runner compatible with graphile-worker — if you prefer to run your background tasks with Go instead of Nodejs.
createdb worker_test
npx graphile-worker -c "worker_test" --schema-only
package main
import (
"context"
"fmt"
"log"
worker "github.com/ioj/gophile-worker"
)
type HelloPayload struct {
Name string `json:"name"`
}
func main() {
w := worker.New(nil)
w.Handle("hello", func(ctx context.Context, job *worker.Job) error {
p := HelloPayload{}
if err := job.UnmarshalPayload(&p); err != nil {
return err
}
if p.Name == "" {
p.Name = "stranger"
}
fmt.Printf("Hello, %v!\n", p.Name)
return nil
})
conninfo := "dbname=worker_test sslmode=disable"
log.Fatal(w.ListenAndServe(conninfo))
}
SELECT graphile_worker.add_job('hello', json_build_object('name', 'Bobby Tables'));
You should see the worker output Hello, Bobby Tables!
Please take a look at Documentation and a more elaborate example, which shows how to use middleware and handle graceful shutdown.
This library is distributed under the terms of the MIT License. See the included file for more detail.
All suggestions and patches welcome, preferably via a git repository I can pull from. If this library proves useful to you, please let me know.