[GH-ISSUE #997] [BUG] Receiving "context deadline exceeded" every time I try to deploy it #2504

Open
opened 2026-03-15 20:43:53 +03:00 by kerem · 0 comments
Owner

Originally created by @CarlosEduardoAD on GitHub (Dec 26, 2024).
Original GitHub issue: https://github.com/hibiken/asynq/issues/997

Originally assigned to: @hibiken, @kamikazechaser on GitHub.

Describe the bug
Basically I can't use the Asynq lib to process tasks because every time I deploy it in a Railway project it gives me this error, I've tried multiple things, like increasing write and read timeout, adding a new queue and even removing the timeout from the Redis db.

Environment (please complete the following information):

  • OS: Alpine Linux:latest (I build it from a Dockerfile to deploy on railway)
  • asynq package version: v0.25.1
  • Redis/Valkey version: 7.2.5

To Reproduce
Steps to reproduce the behavior (Code snippets if applicable):
Basically

redisOpt := asynq.RedisClientOpt{Addr: fmt.Sprintf("%s:6379", host), Password: password, TLSConfig: &tls.Config{}, DialTimeout: 10 * time.Second, // Aumente este valor se necessário
		ReadTimeout:  20 * time.Second,
		WriteTimeout: 20 * time.Second, PoolSize: 20}

	srv := asynq.NewServer(redisOpt, asynq.Config{
		Concurrency: 20,
		Queues: map[string]int{
			"default":  1,
			"critical": 1,
		},
		GroupMaxDelay: 5 * time.Second,
	})

I create a server to to handle my jobs

payload := map[string]interface{}{"id": sej.Id, "email": sej.Email, "ttd": sej.TTD}
	payloadBytes, err := json.Marshal(payload)

	if err != nil {
		return err
	}

	task := asynq.NewTask(sej.Type, payloadBytes)

	_, err = task_manager.Enqueue(task, asynq.ProcessIn(1*time.Minute), asynq.MaxRetry(0), asynq.Timeout(30*time.Second), asynq.Queue("critical"))

Inside my models, I have a function to add a job (task etc...) to be executed

Finally, my function that executes that job

func (jc *JobController) ExecuteTask(ctx context.Context, t *asynq.Task) error {
	var emailJobPayload jobmodel.SendEmailJob
	var email_model emailmodel.EmailModel

	if err := json.Unmarshal(t.Payload(), &emailJobPayload); err != nil {
		return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
	}

	session := db.GenereateDB()

	newsApiClient := newsapi_client.GenerateNewsApi()

	articleResponse, err := newsApiClient.GetEverything(ctx, &newsapi.EverythingParameters{Keywords: "golang"})

	if err != nil {
		return err
	}

	email, err := email_model.SelectOneByEmail(session, emailJobPayload.Email)

	if err != nil {
		return err
	}

	if !email.Authorized {

		return errors.New("unauthorized")
	}

	articleOfTheDay := articleResponse.Articles[0]

	host := env.GetEnv("MAILTRAP_HOST", "my-host")
	port, err := strconv.Atoi(env.GetEnv("MAILTRAP_PORT", "my-port"))

	if err != nil {
		panic(err)
	}

	username := env.GetEnv("MAILTRAP_USERNAME", "my-user")
	password := env.GetEnv("MAILTRAP_PASSWORD", "my-password")

	email_sender := shared.GenerateEmailSender(host, port, username, password)

	unsubscribeToken, err := shared.GenerateToken(jwt.MapClaims{"email": emailJobPayload.Email})

	if err != nil {
		return err
	}

	unsubscribeLink := fmt.Sprintf("http://localhost:3000/api/v1/emails/dismiss?token=%s", unsubscribeToken)

	template, err := utils.LoadTemplate("internal/views/templates/news.html", utils.EmailData{NewsTitle: articleOfTheDay.Title, NewsDescription: articleOfTheDay.Description, NewsLink: articleOfTheDay.URL, UnsubscribeLink: unsubscribeLink})

	if err != nil {
		return err
	}

	err = email_sender.SendEmail(emailJobPayload.Email, "Sua newsletter de go de hoje chegou!", template)

	if err != nil {
		return err
	}

	email_job := jobmodel.NewSendEmailJob(emailJobPayload.Id, emailJobPayload.Email, utils.ReturnNextMonday(), "send_email")
	err = email_job.AddAndEnqueueTask(jc.Client)

	if err != nil {
		return err
	}
	return nil
}

It's not that much, but I can't really provide anything other than my code and there isn't really to reproduce

Expected behavior
My email sending job to be executed

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
As you can see, I'm building a newsletter, the thing is, is the e-mail protocol slowing down my app and messing up the asynq execution? I don't really know...

Hope you guys can help me with this one, I've been stuck in this for like 3-4 days.

Originally created by @CarlosEduardoAD on GitHub (Dec 26, 2024). Original GitHub issue: https://github.com/hibiken/asynq/issues/997 Originally assigned to: @hibiken, @kamikazechaser on GitHub. **Describe the bug** Basically I can't use the Asynq lib to process tasks because every time I deploy it in a Railway project it gives me this error, I've tried multiple things, like increasing write and read timeout, adding a new queue and even removing the timeout from the Redis db. **Environment (please complete the following information):** - OS: Alpine Linux:latest (I build it from a Dockerfile to deploy on railway) - `asynq` package version: v0.25.1 - Redis/Valkey version: 7.2.5 **To Reproduce** Steps to reproduce the behavior (Code snippets if applicable): Basically ``` redisOpt := asynq.RedisClientOpt{Addr: fmt.Sprintf("%s:6379", host), Password: password, TLSConfig: &tls.Config{}, DialTimeout: 10 * time.Second, // Aumente este valor se necessário ReadTimeout: 20 * time.Second, WriteTimeout: 20 * time.Second, PoolSize: 20} srv := asynq.NewServer(redisOpt, asynq.Config{ Concurrency: 20, Queues: map[string]int{ "default": 1, "critical": 1, }, GroupMaxDelay: 5 * time.Second, }) ``` I create a server to to handle my jobs ``` payload := map[string]interface{}{"id": sej.Id, "email": sej.Email, "ttd": sej.TTD} payloadBytes, err := json.Marshal(payload) if err != nil { return err } task := asynq.NewTask(sej.Type, payloadBytes) _, err = task_manager.Enqueue(task, asynq.ProcessIn(1*time.Minute), asynq.MaxRetry(0), asynq.Timeout(30*time.Second), asynq.Queue("critical")) ``` Inside my models, I have a function to add a job (task etc...) to be executed Finally, my function that executes that job ``` func (jc *JobController) ExecuteTask(ctx context.Context, t *asynq.Task) error { var emailJobPayload jobmodel.SendEmailJob var email_model emailmodel.EmailModel if err := json.Unmarshal(t.Payload(), &emailJobPayload); err != nil { return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry) } session := db.GenereateDB() newsApiClient := newsapi_client.GenerateNewsApi() articleResponse, err := newsApiClient.GetEverything(ctx, &newsapi.EverythingParameters{Keywords: "golang"}) if err != nil { return err } email, err := email_model.SelectOneByEmail(session, emailJobPayload.Email) if err != nil { return err } if !email.Authorized { return errors.New("unauthorized") } articleOfTheDay := articleResponse.Articles[0] host := env.GetEnv("MAILTRAP_HOST", "my-host") port, err := strconv.Atoi(env.GetEnv("MAILTRAP_PORT", "my-port")) if err != nil { panic(err) } username := env.GetEnv("MAILTRAP_USERNAME", "my-user") password := env.GetEnv("MAILTRAP_PASSWORD", "my-password") email_sender := shared.GenerateEmailSender(host, port, username, password) unsubscribeToken, err := shared.GenerateToken(jwt.MapClaims{"email": emailJobPayload.Email}) if err != nil { return err } unsubscribeLink := fmt.Sprintf("http://localhost:3000/api/v1/emails/dismiss?token=%s", unsubscribeToken) template, err := utils.LoadTemplate("internal/views/templates/news.html", utils.EmailData{NewsTitle: articleOfTheDay.Title, NewsDescription: articleOfTheDay.Description, NewsLink: articleOfTheDay.URL, UnsubscribeLink: unsubscribeLink}) if err != nil { return err } err = email_sender.SendEmail(emailJobPayload.Email, "Sua newsletter de go de hoje chegou!", template) if err != nil { return err } email_job := jobmodel.NewSendEmailJob(emailJobPayload.Id, emailJobPayload.Email, utils.ReturnNextMonday(), "send_email") err = email_job.AddAndEnqueueTask(jc.Client) if err != nil { return err } return nil } ``` It's not that much, but I can't really provide anything other than my code and there isn't really to reproduce **Expected behavior** My email sending job to be executed **Screenshots** If applicable, add screenshots to help explain your problem. **Additional context** As you can see, I'm building a newsletter, the thing is, is the e-mail protocol slowing down my app and messing up the asynq execution? I don't really know... Hope you guys can help me with this one, I've been stuck in this for like 3-4 days.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#2504
No description provided.