This topic describes how to connect a Golang application to SchedulerX by using SDKs for Golang.
Configuration in the console
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.
Create a Golang job. For more information, see Task management.
Client access
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}
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 }
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
Publish the application to Alibaba Cloud after the application is connected to SchedulerX.
Log on to Distributed Task scheduling platform.
In the top navigation bar, select a region.
In the left-side navigation pane, click Application Management.
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.