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

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

Open
CarlosEduardoAD opened this issue Dec 26, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@CarlosEduardoAD
Copy link

CarlosEduardoAD commented Dec 26, 2024

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.

@CarlosEduardoAD CarlosEduardoAD added the bug Something isn't working label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants