func init() { // Parse the flags flag.Parse() cnf = config.Config{ Broker: *broker, ResultBackend: *resultBackend, Exchange: *exchange, ExchangeType: *exchangeType, DefaultQueue: *defaultQueue, BindingKey: *bindingKey, } // Parse the config // NOTE: If a config file is present, it has priority over flags data, err := config.ReadFromFile(*configPath) if err == nil { err = config.ParseYAMLConfig(&data, &cnf) errors.Fail(err, "Could not parse config file") } server, err := machinery.NewServer(&cnf) errors.Fail(err, "Could not initialize server") // Register tasks tasks := map[string]interface{}{ "add": exampletasks.Add, "multiply": exampletasks.Multiply, } server.RegisterTasks(tasks) // The second argument is a consumer tag // Ideally, each worker should have a unique tag (worker1, worker2 etc) worker = server.NewWorker("machinery_worker") }
func _setup(brokerURL, backend string) *machinery.Server { cnf := config.Config{ Broker: brokerURL, ResultBackend: backend, Exchange: "test_exchange", ExchangeType: "direct", DefaultQueue: "test_queue", BindingKey: "test_task", } server, err := machinery.NewServer(&cnf) errors.Fail(err, "Could not initialize server") tasks := map[string]interface{}{ "add": func(args ...int64) (int64, error) { sum := int64(0) for _, arg := range args { sum += arg } return sum, nil }, "multiply": func(args ...int64) (int64, error) { sum := int64(1) for _, arg := range args { sum *= arg } return sum, nil }, } server.RegisterTasks(tasks) return server }
func (s *EagerIntegrationTestSuite) SetupSuite() { var err error // init server cnf := config.Config{ Broker: "eager", ResultBackend: "eager", } s.srv, err = machinery.NewServer(&cnf) s.Nil(err) s.NotNil(s.srv) // register task s.called = 0 s.srv.RegisterTask("float_called", func(i float64) (float64, error) { s.called = i return s.called, nil }) s.srv.RegisterTask("float_result", func(i float64) (float64, error) { return i + 100.0, nil }) s.srv.RegisterTask("int_result", func(i int64) (int64, error) { return i + 100, nil }) }
func getTestServer(t *testing.T) *machinery.Server { server, err := machinery.NewServer(&config.Config{ Broker: "amqp://*****:*****@localhost:5672/", ResultBackend: "redis://127.0.0.1:6379", Exchange: "machinery_exchange", ExchangeType: "direct", DefaultQueue: "machinery_tasks", BindingKey: "machinery_task", }) if err != nil { t.Error(err) } return server }
func init() { // Parse the flags flag.Parse() cnf = config.Config{ Broker: *broker, ResultBackend: *resultBackend, Exchange: *exchange, ExchangeType: *exchangeType, DefaultQueue: *defaultQueue, BindingKey: *bindingKey, } // Parse the config // NOTE: If a config file is present, it has priority over flags data, err := config.ReadFromFile(*configPath) if err == nil { err = config.ParseYAMLConfig(&data, &cnf) errors.Fail(err, "Could not parse config file") } server, err = machinery.NewServer(&cnf) errors.Fail(err, "Could not initialize server") }
func TestWorkerOnlyConsumesRegisteredTaskAMQP(t *testing.T) { amqpURL := os.Getenv("AMQP_URL") if amqpURL != "" { cnf := config.Config{ Broker: amqpURL, ResultBackend: amqpURL, Exchange: "test_exchange", ExchangeType: "direct", DefaultQueue: "test_queue", BindingKey: "test_task", } server1, err := machinery.NewServer(&cnf) errors.Fail(err, "Could not initialize server") server1.RegisterTask("add", func(args ...int64) (int64, error) { sum := int64(0) for _, arg := range args { sum += arg } return sum, nil }) server2, err := machinery.NewServer(&cnf) errors.Fail(err, "Could not initialize server") server2.RegisterTask("multiply", func(args ...int64) (int64, error) { sum := int64(1) for _, arg := range args { sum *= arg } return sum, nil }) task1 := signatures.TaskSignature{ Name: "add", Args: []signatures.TaskArg{ { Type: "int64", Value: 2, }, { Type: "int64", Value: 3, }, }, } task2 := signatures.TaskSignature{ Name: "multiply", Args: []signatures.TaskArg{ { Type: "int64", Value: 4, }, { Type: "int64", Value: 5, }, }, } worker1 := server1.NewWorker("test_worker") worker2 := server2.NewWorker("test_worker2") go worker1.Launch() go worker2.Launch() group := machinery.NewGroup(&task2, &task1) asyncResults, err := server1.SendGroup(group) if err != nil { t.Error(err) } expectedResults := []int64{5, 20} actualResults := make([]int64, 2) for i, asyncResult := range asyncResults { result, err := asyncResult.Get() if err != nil { t.Error(err) } intResult, ok := result.Interface().(int64) if !ok { t.Errorf("Could not convert %v to int64", result.Interface()) } actualResults[i] = intResult } worker1.Quit() worker2.Quit() sort.Sort(ascendingInt64s(actualResults)) if !reflect.DeepEqual(expectedResults, actualResults) { t.Errorf( "expected results = %v, actual results = %v", expectedResults, actualResults, ) } } }