Giter Club home page Giter Club logo

Comments (3)

TheOrioli avatar TheOrioli commented on July 4, 2024

You could always queue the jobs up like this:

redis-cli -r 100 RPUSH resque:queue:myqueue '{"class":"MyClass","args":[{"product_file":"whatever.csv","category_file":"something_else.csv"}]}'

Your code would look like this:

func(queue string, args ...interface{}) error{
    files, ok := args[0].(map[string]string)
    if !ok {
      // error
    }
    productFile := os.Open(files["product_file"])
    // more things done here
    return nil
}

The thing is with the interface{} array all kinds of arguments can be queued up, the same way they are in Resque.

from goworker.

benmanns avatar benmanns commented on July 4, 2024

@Aorioli has it. We maintain compatibility with Resque (1.x), which uses the array-for-args format. You also have to be careful expecting a map[string]string because that can run into issues with formats like nested objects {"a":{"b":"c"}}, numeric types {"a":123}, and null {"a":null}.

You can define a separate handler to help with the repetitive args checking:

func newMapFunc(f func(queue string, args map[string]interface{}) error) (func(queue string, args ...interface{}) error) {
    return func(queue string, args ...interface{}) error {
        if len(args) != 1 {
            // return some error
        }
        mapArgs, ok := args[0].(map[string]interface{})
        if !ok {
            // return some error
        }
        return f(mapArgs)
    }
}

// ...

func someWorker(queue string, args map[string]interface) error{
    path, ok := args["product_file"].(string)
    if !ok {
        // return some error
    }
    productFile := os.Open(path)
    // more things done here
    return nil
}

// ...

goworker.Register("MyClass", newMapFunc(someWorker))
goworker.Register("MyOtherClass", newMapFunc(someOtherWorker))

from goworker.

verygoodsoftwarenotvirus avatar verygoodsoftwarenotvirus commented on July 4, 2024

Oh awesome! Thanks @Aorioli & @benmanns.

from goworker.

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.