All Products
Search
Document Center

SchedulerX:Connect a Golang application to SchedulerX by using SDKs for Golang

Last Updated:Nov 17, 2023

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

Configuration in the console

  1. Create an application. For more information, see Create an application in the Application management topic. Generate the configuration information about the application. For more information, see Step 2 in the Connect to SchedulerX over the Internet from an on-premises environment topic.

    image.pngimage.png

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

    image.png

Client access

  1. Run the following command to download and install the SDK for Golang package with its dependencies by using 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 service 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 just an example, the real configuration needs to be obtained 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{}
    
    	// Specify a name for your job and register it with the client. The job name must be consistent with that you specify in the console.
    	client.RegisterTask("HelloWorld", task)
    	select {}
    }
    

Verify the result

  1. Publish the application to Alibaba Cloud after the application is connected to SchedulerX.

  2. Log on to Distributed Task scheduling platform.

  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, you can view Total number of instances.

    • If the Total number of instances column displays 0, the application is not connected to SchedulerX. In this case, check and modify the on-premises application.

    • If the Total number of instances column displays a number other than 0, the application is connected to SchedulerX. Click View instances in the Operation column to view the instances in the Connect to an instance panel.

What to do next

After the application is connected to SchedulerX, you can create jobs in the SchedulerX console. For more information, see Create a task.

References

Golang