Code Monkey home page Code Monkey logo

Comments (11)

cielu avatar cielu commented on July 25, 2024

Hello, hibiken , how to use this lib in go-gin ?

from asynq.

cielu avatar cielu commented on July 25, 2024

Why I can't kill the process ?

from asynq.

hibiken avatar hibiken commented on July 25, 2024

@cielu Which OS are you using?
If you are on Windows, maybe something related to https://golang.org/pkg/os/signal/#hdr-Windows.

Please take a look at the Signal Wiki page on how to gracefully shutdown the background workers.

from asynq.

cielu avatar cielu commented on July 25, 2024

@hibiken macOS, But I start the worker in go-gin . Do you have any good idea ? thanks ..

I'm new at go

from asynq.

hibiken avatar hibiken commented on July 25, 2024

So you are using github.com/gin-gonic/gin for your web framework, correct?
And are you starting the workers (bg.Run(handler)) from your web server's main?

from asynq.

cielu avatar cielu commented on July 25, 2024

@hibiken Yes , that's right . Here is my main func code

func main() {
	// Set Mode
	gin.SetMode(setting.AppCfg.AppMode)
	// Init router
	router := routes.InitRouter()
	// run
	router.Run(":" + setting.AppCfg.AppPost)
	// init asynq worker
	asynqWorker.InitAsynqWorker()
	// init asynq client
	asynqClient.InitAsynqClient()
}

from asynq.

hibiken avatar hibiken commented on July 25, 2024

I see.
The intended use case of asynq is that you use Client to schedule tasks to be processed in the background outside of a user request, and have another process(main.go) to process those tasks in the background.
So you will create another main.go apart from your web server.

So for example, you will have something like this in your web server's main file:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
        client := asynq.NewClient(/* redis conn opt */)
	r.POST("/signup", func(c *gin.Context) {
                t := asynq.NewTask("send_email", map[string]interface{}{"user_id": 42})
		client.Schedule(t, time.Now())
                // render html or json
	})
	r.Run() 
}

and create a separate main.go file for workers:

func main() {
    r := &asynq.RedisClientOpt{
        Addr: "localhost:6379",
    }

    bg := asynq.NewBackground(r, &asynq.Config{
        Concurrency: 10,
    })

    bg.Run(handler)
}

from asynq.

cielu avatar cielu commented on July 25, 2024

Yeah . enhh, I got you . But , can i start the worker in one project ? I don't want separate it .That is a lot of trouble

thanks 😊

from asynq.

hibiken avatar hibiken commented on July 25, 2024

I see.

Background.Run method will block and wait for a termination signal (OS signal TERM or INTERRUPT).

I think if you want to start the workers from your web server's main then try this:

func main() {
	// Set Mode
	gin.SetMode(setting.AppCfg.AppMode)
	// Init router
	router := routes.InitRouter()

        bgDone := make(chan, struct{})

       go func() {
           bg := asynq.NewBackground(r, &asynq.Config{/* options */})
           bg.Run(handler)
           bgDone <- struct{}{}
       }()
	// run
	router.Run(":" + setting.AppCfg.AppPost)
        <-bgDone
}

If that doesn't work, then another option is to fork this repo and export Background.Start and Background.Stop methods (currently they are unexported) and call those to start and stop the background workers, those methods are non-blocking.

from asynq.

cielu avatar cielu commented on July 25, 2024

Ohh. Thank you very much !

from asynq.

hibiken avatar hibiken commented on July 25, 2024

Closing this issue for now

from asynq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.