Connect a Golang application to SchedulerX by using SDKs for Golang

更新时间:
复制 MD 格式

This topic describes how to connect a Golang application to SchedulerX by using SDKs for Golang.

Configuration in the console

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

    image.png

    image.png

  2. Create a Golang job. For more information, see Task management.

    In the Basic Settings step, set the Job Name to hellogo, enter HelloWorld for the display name, select standalone for the execution mode, and select Medium for the Priority.

Client access

  1. Run the following command to pull the SchedulerX Go SDK with the latest tag.

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

    You can also run the following command to download and install a SDK for Golang branch package with its dependencies:

    go get github.com/alibaba/schedulerx-worker-go@{branch_name}
  2. Write the business code to implement 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 Golang job with the client. The job name is the same as that you specify in the console.

    package main
    import (
    	"github.com/alibaba/schedulerx-worker-go"
    )
    func main() {
    	// This is an example. Obtain the actual configuration from the platform.
    	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{}
    	// Assign a name to your job and register it with the client. This name must match the name configured in the console.
    	client.RegisterTask("HelloWorld", task)
    	select {}
    }
    

Client parameters

Parameter

API

Description

Custom port

config.WithGrpcPort

For non-standalone jobs, workers must be able to connect to each other. You can specify a port. If you do not specify a port, SchedulerX randomly selects an available port.

Custom network interface

config.WithIface

If your machine has multiple network interfaces, specify a network interface to use its IP address.

Custom label

config.WithLabel

You can add labels to the client and configure jobs to run only on workers with specific labels. This is commonly used for canary releases and testing.

Example:

func main() {
    // This is just an example, the real configuration needs to be obtained from the platform
    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 name TestMapReduceJob registered here must match the name configured on the platform
    task := &TestMapReduceJob{
        mapjob.NewMapReduceJobProcessor(), // FIXME how define user behavior
    }
    client.RegisterTask("TestMapReduceJob", task)
    select {}
}

Verify the results

  1. Deploy the application to Alibaba Cloud.

  2. Log on to the 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 the total number of instances is 0, the connection failed. Check and fix your application's code and configuration.

    • If the total number of instances is greater than 0, the connection is successful. This number represents the count of connected instances.

What to do next

After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see the "Create a job" section of the Job management topic.

References

Golang