Skip to content

Instantly share code, notes, and snippets.

@dontlaugh
Created May 17, 2021 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dontlaugh/9da17c47c16b42a54da52f8f9982d707 to your computer and use it in GitHub Desktop.
Save dontlaugh/9da17c47c16b42a54da52f8f9982d707 to your computer and use it in GitHub Desktop.
Faktory worker
package main
import (
"context"
"log"
"os"
worker "github.com/contribsys/faktory_worker_go"
)
func main() {
// Expects FAKTORY_URL to be set in the environment
// example: FAKTORY_URL=tcp://:password@localhost:7419
if os.Getenv("FAKTORY_URL") == "" {
log.Fatal("FAKTORY_URL must be set in the environment")
}
mgr := worker.NewManager()
// Pick a name for the jobtype you will listen to, and provide a
// callback function that does the work.
mgr.Register("ExampleJobFunction", jobFunction)
// Carefully consider the concurrency your worker process should support
mgr.Concurrency = 1
// One of the Process*Queues methods should be called before Run()
mgr.ProcessStrictPriorityQueues("critical", "default", "bulk")
mgr.Run()
}
func jobFunction(ctx context.Context, args ...interface{}) error {
// Your job is called with a flat list of arguments. It is up to you to
// parse these, or ignore them. If you want key-value pairs, the
// caller process could do something like this:
//
// foo=baz, foo2=baz2 ...
//
// ... and your worker could split the args and use them, or hand them
// to a subprocess, which acceps environment variables of this form.
help := worker.HelperFor(ctx)
log.Printf("Working on job %s\n", help.Jid())
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment