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

graceful shutdown is invalid #174

Closed
nilnoun opened this issue Jul 1, 2024 · 3 comments
Closed

graceful shutdown is invalid #174

nilnoun opened this issue Jul 1, 2024 · 3 comments

Comments

@nilnoun
Copy link

nilnoun commented Jul 1, 2024

The expectation was to exit after completing the task, but it exited immediately upon receiving the exit signal.


example:

comsumer.go

package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/wagslane/go-rabbitmq"
)

func main() {
	conn, err := rabbitmq.NewConn(
		"amqp://guest:guest@localhost",
		rabbitmq.WithConnectionOptionsLogging,
	)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	consumer, err := rabbitmq.NewConsumer(
		conn,
		"my_queue",
		rabbitmq.WithConsumerOptionsQueueDurable,
		rabbitmq.WithConsumerOptionsConcurrency(1),
		rabbitmq.WithConsumerOptionsQOSPrefetch(1),
	)
	if err != nil {
		log.Fatal(err)
	}

	sigs := make(chan os.Signal, 1)

	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		fmt.Println("awaiting signal")
		sig := <-sigs

		fmt.Println()
		fmt.Println(sig)
		fmt.Println("stopping consumer")

		consumer.Close()
	}()

	// block main thread - wait for shutdown signal
	err = consumer.Run(func(d rabbitmq.Delivery) rabbitmq.Action {
		for i := 0; i < 10; i++ {
			log.Printf("consumed: %v, %d", string(d.Body), i)
			time.Sleep(1 * time.Second)
		}
		// rabbitmq.Ack, rabbitmq.NackDiscard, rabbitmq.NackRequeue
		return rabbitmq.Ack
	})
	if err != nil {
		log.Fatal(err)
	}
}

output

awaiting signal
2024/07/01 11:15:54 gorabbit INFO: Processing messages on 1 goroutines
2024/07/01 11:15:54 consumed: 1f7e305c-8361-462d-8bfc-60f3ee6c36b4, 0
2024/07/01 11:15:55 consumed: 1f7e305c-8361-462d-8bfc-60f3ee6c36b4, 1
^C
interrupt
stopping consumer
2024/07/01 11:15:55 gorabbit INFO: waiting for handler to finish...
2024/07/01 11:15:55 gorabbit INFO: closing channel manager...
2024/07/01 11:15:55 gorabbit INFO: closing consumer...
2024/07/01 11:15:55 gorabbit INFO: closing connection manager...
2024/07/01 11:15:55 gorabbit INFO: amqp channel closed gracefully

@thibleroy
Copy link

thibleroy commented Jul 1, 2024

Hello, I have tried to fix this: #175

@nilnoun
Copy link
Author

nilnoun commented Jul 1, 2024

Thank you.

@wagslane
Copy link
Owner

fix is merged, please open a new issue if 0.14.2 doesn't fix

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

3 participants