Go SDK integration

更新时间:
复制 MD 格式

This topic shows how to connect a Go application to SchedulerX by using the Go SDK.

Console configuration

  1. Refer to Create an application to create a standard application, and then follow Step 2 to find its configuration information.

    In the application list, find the target application and click Access Configuration in the Operation column.

    Select Golang to obtain the following connection parameters.

    Endpoint:   acm.aliyun.com,
    Namespace:  433d8b23-xxx-a-90d4d1b9a4af,
    GroupId:    xxl-job-test,
    AppKey:     b0iXMY3Bxxx+Gew==,

  2. To create a Go job, refer to Job management.

    In the Basic Configuration step, set Job Name to hellogo, select the target Application, set Job Type to golang, set Task Name to HelloWorld, set Execution mode to Standalone, set Priority to Medium, and specify Job parameters as needed.

Client integration

  1. Run the following command to pull the latest version of the SchedulerX Go SDK by specifying a tag.

    go get github.com/alibaba/schedulerx-worker-go@{LATEST_TAG}

    Alternatively, run the following command to pull a specific branch.

    go get github.com/alibaba/schedulerx-worker-go@{BRANCH_NAME}
  2. Write your business logic by implementing the Processor interface.

    type Processor interface {
        Process(ctx *processor.JobContext) (*ProcessResult, error)
    }

    Example:

    package main
    import (
    	"fmt"
    	"github.com/alibaba/schedulerx-worker-go/processor"
    	"github.com/alibaba/schedulerx-worker-go/processor/jobcontext"
    	"time"
    )
    var _ processor.Processor = &HelloWorld{}
    type HelloWorld struct{}
    func (h *HelloWorld) Process(ctx *jobcontext.JobContext) (*processor.ProcessResult, error) {
    	fmt.Println("[Process] Start process my task: Hello world!")
    	// mock execute task
    	time.Sleep(3 * time.Second)
    	ret := new(processor.ProcessResult)
    	ret.SetStatus(processor.InstanceStatusSucceed)
    	fmt.Println("[Process] End process my task: Hello world!")
    	return ret, nil
    }
    
  3. Register the client and job. The task name must match the name that you configured in the console.

    package main
    import (
    	"github.com/alibaba/schedulerx-worker-go"
    )
    func main() {
    	// This is just an example, obtain the actual configuration from the console
    	cfg := &schedulerx.Config{
    		Endpoint:  "acm.aliyun.com",
    		Namespace: "433d8b23-xxx-xxx-xxx-90d4d1b9a4af",
    		GroupId:   "xueren_sub",
    		AppKey:    "xxxxxx",
    	}
    	client, err := schedulerx.GetClient(cfg)
    	if err != nil {
    		panic(err)
    	}
    	task := &HelloWorld{}
    	// Register the task using a name that matches the one configured in the console.
    	client.RegisterTask("HelloWorld", task)
    	select {}
    }
    

Client configuration parameters

Parameter

API

Description

Custom port

config.WithGrpcPort

For non-standalone jobs, workers communicate with each other. You can specify a port for this communication. If a port is not specified, a random idle port is used.

Custom NIC

config.WithIface

If a machine has multiple Network Interface Controllers (NICs), specify a NIC by name to use its IP address.

Custom tag

config.WithLabel

You can attach a tag to a client and configure jobs to run only on clients with that tag. This is commonly used for canary release and testing.

Example:

func main() {
    // This is just an example, obtain the actual configuration from the console
    cfg := &schedulerx.Config{
        Endpoint:  "acm.aliyun.com",
        Namespace: "fa6ed99e-xxxxxx-a2bf1659d039",
        GroupId:   "xueren_test_sub",
        AppKey:    "myV5K5Xaf1kxxxxxxxx",
    }
    client, err := schedulerx.GetClient(cfg, schedulerx.WithWorkerConfig(config.NewWorkerConfig(
        config.WithGrpcPort(8001),
        config.WithIface("eth0")))),
        config.WithLabel("test")
    if err != nil {
        panic(err)
    }
    // The task name, TestMapReduceJob, must match the name configured in the console.
    task := &TestMapReduceJob{
        mapjob.NewMapReduceJobProcessor(), // FIXME how define user behavior
    }
    client.RegisterTask("TestMapReduceJob", task)
    select {}
}

Result verification

  1. Deploy the application to Alibaba Cloud. The client connects automatically when the application starts.

  2. Log on to the MSE SchedulerX console.

  3. In the top navigation bar, select a region.

  4. In the left-side navigation pane, click Application Management.

  5. On the Application Management page, check the Total number of instances.

    • If Total number of instances is 0, the application failed to connect. Review your application's configuration and logs for errors.

    • If Total number of instances is greater than 0, the application is connected. This value represents the number of connected instances. In the Actions column, click View instances to view the list of instances in the Connect to an instance dialog box.

What to do next

After the application is connected to SchedulerX, you can create scheduling tasks in the Distributed Task Scheduling Platform. For more information, see Create a scheduling task.

Related documentation

Go jobs.