contribsys / faktory_worker_go

Faktory workers for Go
Mozilla Public License 2.0
242 stars 43 forks source link

Improve Readme #16

Closed thapakazi closed 5 years ago

thapakazi commented 6 years ago

Motivation Today I started playing with faktory, and its super awesome with slick UI ;)

But sadly i can't find any documentation/wiki regarding retry.

Can we and more case where we have:

Ignore if, you might have more important priorities to deal with.

cayter commented 6 years ago

After digging into a few repos, I believe this is the way to customise the retry options.

How To Create A New Job Using faktory_worker_go Repo

job := faktory.NewJob("SomeJob", 1, 2, "hello")

NewJob Implementation In faktory Repo

func NewJob(jobtype string, args ...interface{}) *Job {
    return &Job{
        Type:      jobtype,
        Queue:     "default",
        Args:      args,
        Jid:       randomJid(),
        CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
        Retry:     25,
        Priority:  5,
    }
}

To answer your question on how to use retry, basically it has a default value of 25 if you decide to use NewJob. However, if you wish to customise the default value, you can do this(INSTEAD OF using NewJob):

import (
  ...
  faktory "github.com/contribsys/faktory/client"
  ...
)

...

job := &faktory.Job{
  Type:      jobtype,
  Queue:     "default",
  Args:      args,
  Jid:       randomJid(),
  CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
  Retry:     <YOUR_OWN_RETRY_VALUE>,
  Priority:  5,
}

...

As for throwing exception, a similar mechanism in golang would be using panic/recover. However, you should only use panic/recover if you're not sure how to handle the error(check the discussion here) or you assume that caller doesn't need to handle it. In any case, you should try to handle the error or return the error to the caller so that it is handled properly. Hope this helps!

Would be great if you can help to document this down and send out a PR to improve the README since you have already created an issue. Thanks!

mperham commented 6 years ago

@cayter Correct. The idea is that you call NewJob to get a correctly initialized "default" job, set additional properties, like Retry, and then Push() it to Faktory. Don't copy the internals of NewJob.

job := faktory.NewJob("type", args...)
job.Retry = 4
client.Push(job)

As for README updates, PRs welcome.

alebian commented 5 years ago

Hi,

Where I'm working we have a microservice architecture and I want to create a service in go using faktory for processing background jobs. A cool thing about faktory is that I can use any client library (in any language) to send jobs to this service. Currently I want to send jobs from a Ruby microservice and I'm forced to send a "retry" param in order to make faktory retry the worker if it fails, and I don't think that this should be the responsibility of the client. Wouldn't it be nice to at least have a default value in the workers for this? Or maybe a new type of error that makes faktory retry the job?

Thanks

mperham commented 5 years ago

@alebian Nevermind, I see why you put this here (not a great issue title so I was misled).

mperham commented 5 years ago

Retry is an Integer; when it is converted from JSON into a Go struct, it will automatically be populated but if it was not included, Go will set it to 0. Thus the problem: I can't tell if it was set to 0 by the client or not included (and should be set to a default value). Your client should set some sensible defaults and document how to change them. The Go and Ruby libraries both set a default Retry value of 25; if your client of choice does not, it should be flagged as a bug.

alebian commented 5 years ago

I am using faktory_worker_ruby. When I do:

Faktory::Client.new.push('queue' =>'default', 'args' => [], 'jid' => SecureRandom.hex(12), 'jobtype' => 'SomeJob')

I receive a 0 in the retry value in the Go worker. I have to:

Faktory::Client.new.push('queue' =>'default', 'args' => [], 'jid' => SecureRandom.hex(12), 'jobtype' => 'SomeJob', 'retry' => 25)

in order to get a retry value different from 0.

mperham commented 5 years ago

Right, the Faktory::Client#push API is low-level and sends the passed hash directly to Faktory. SomeJob.perform_async will include a default retry value.