Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch error on startup #75

Closed
randomandy opened this issue Apr 20, 2016 · 4 comments
Closed

Catch error on startup #75

randomandy opened this issue Apr 20, 2016 · 4 comments

Comments

@randomandy
Copy link
Contributor

So I have something like this:

    func init() {
    ...
        server, err := machinery.NewServer(&cnf)
        if err != nil {
            // some error handling
        }

        server.RegisterTasks(tasks)

        worker = server.NewWorker("machinery_worker")
    ...

    func main() {
        err := worker.Launch()
        if err != nil {
            // some error handling
        }
    }

which leads to this if the worker cannot connect to the broker on startup:

    2016/04/20 16:01:59 Launching a worker with the following settings:
    2016/04/20 16:01:59 - Broker: amqp://some_url
    2016/04/20 16:01:59 - ResultBackend: amqp://some_url
    2016/04/20 16:01:59 - Exchange: machinery_exchange
    2016/04/20 16:01:59 - ExchangeType: direct
    2016/04/20 16:01:59 - DefaultQueue: machinery_tasks
    2016/04/20 16:01:59 - BindingKey: machinery_task
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xb code=0x1 addr=0x20 pc=0x3495e9]

    goroutine 5 [running]:
    panic(0x4d3680, 0xc82000a0d0)
        /usr/local/go/src/runtime/panic.go:464 +0x3e6
    github.com/RichardKnop/machinery/vendor/github.com/streadway/amqp.(*Channel).Close(0x0, 0x0, 0x0)
        /Users/andy/checkouts/golang/src/github.com/RichardKnop/machinery/vendor/github.com/streadway/amqp/channel.go:402 +0x39
    github.com/RichardKnop/machinery/v1/brokers.(*AMQPBroker).StartConsuming(0xc8200165c0, 0x5d7220, 0x10, 0xc44828, 0xc8200bd500, 0x1, 0xc40028, 0xc8200e2050)
        /Users/andy/checkouts/golang/src/github.com/RichardKnop/machinery/v1/brokers/amqp.go:57 +0x156
    github.com/RichardKnop/machinery/v1.(*Worker).Launch.func1(0xc44740, 0xc8200165c0, 0xc8200bd500, 0xc820010540)
        /Users/andy/checkouts/golang/src/github.com/RichardKnop/machinery/v1/worker.go:38 +0xa2
    created by github.com/RichardKnop/machinery/v1.(*Worker).Launch
        /Users/andy/checkouts/golang/src/github.com/RichardKnop/machinery/v1/worker.go:47 +0x67d

I'm trying to figure out where the process dies. server.RegisterTasks(tasks) or in worker = server.NewWorker("machinery_worker") (I'm catching all other errors).

Although the output would suggest that the error actually occurs during worker.Launch(), which leads me to believe that there's a bug in the error handling (because err comes back as nil)?

@randomandy
Copy link
Contributor Author

Ok so I'm pretty sure it happens here:
broker.StartConsuming()
inside Launch()

@randomandy
Copy link
Contributor Author

This fixes it:

func (worker *Worker) Launch() error {
    cnf := worker.server.GetConfig()
    broker := worker.server.GetBroker()

    log.Printf("Launching a worker with the following settings:")
    log.Printf("- Broker: %s", cnf.Broker)
    log.Printf("- ResultBackend: %s", cnf.ResultBackend)
    log.Printf("- Exchange: %s", cnf.Exchange)
    log.Printf("- ExchangeType: %s", cnf.ExchangeType)
    log.Printf("- DefaultQueue: %s", cnf.DefaultQueue)
    log.Printf("- BindingKey: %s", cnf.BindingKey)

    errorsChan := make(chan error)

    go func() {
        for {
            retry, err := broker.StartConsuming(worker.ConsumerTag, worker)
            if err != nil {
                errorsChan <- err
            }

            if retry {
                log.Printf("Going to retry launching the worker. Error: %v", err)
            }
        }
    }()

    return <-errorsChan
}

@RichardKnop
Copy link
Owner

@randomandy HI Andy. Thanks for catching this bug!

I'm quite busy at the moment so it might take me few days to take a look. If you want, create a PR with a fix and I will try to look at it and merge it this weekend if time allows.

@randomandy
Copy link
Contributor Author

@RichardKnop Hey Richard. Sure thing, happy to help. Fix is in #76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants