Skip to content

A simple task runner compatible with graphile-worker

License

Notifications You must be signed in to change notification settings

ioj/gophile-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A simple task runner compatible with graphile-worker

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages