Skip to content

Latest commit

 

History

History
81 lines (55 loc) · 2.04 KB

README.md

File metadata and controls

81 lines (55 loc) · 2.04 KB

gophile-worker

GoDoc Go Report Card License

A simple task runner compatible with graphile-worker — if you prefer to run your background tasks with Go instead of Nodejs.

Quickstart

Create a test database

createdb worker_test

Install graphile-worker schema

npx graphile-worker -c "worker_test" --schema-only

Implement a simple task runner

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))
}

Schedule a job via SQL

SELECT graphile_worker.add_job('hello', json_build_object('name', 'Bobby Tables'));

You should see the worker output Hello, Bobby Tables!

What's next?

Please take a look at Documentation and a more elaborate example, which shows how to use middleware and handle graceful shutdown.

License

This library is distributed under the terms of the MIT License. See the included file for more detail.

Contributing

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.