HTTP jobs

Updated at:
Copy as MD

SchedulerX 2.0 schedules HTTP jobs that call your HTTP endpoints on a defined schedule and validate the responses automatically. You run HTTP jobs in one of two execution modes -- serverless or agent -- depending on whether your endpoints are publicly accessible and whether you need second-level scheduling.

Choose an execution mode

Serverless modeAgent mode
Best forInternet-facing API calls and lightweight jobsInternal service calls and complex processing scenarios
Client setupNot required. SchedulerX initiates the requests.Required. Deploy the SchedulerX agent first.
Request methodsGET, POSTGET, POST
Second-level schedulingNot supported. Minimum interval is one minute.Supported
Internal URLsNot supported. URLs must be publicly accessible. If the address uses IP:Port format, the machine must have Internet access.Supported
Execution timeoutBasic Edition: 30 seconds. Professional Edition: 120 seconds.No limit
Note

Every HTTP response must be in JSON format. SchedulerX validates each response by checking whether the parsed value of a specified key matches the value you configured.

Prerequisites

Before you begin, make sure that you have:

  • A deployed HTTP service with a URL accessible from the Internet (serverless mode) or from the agent host (agent mode)

  • (Agent mode only) A deployed SchedulerX agent (version later than 1.8.2). See Deploy the SchedulerX agent.

Create an HTTP job

Creating an HTTP job involves three steps: configure basic settings, set up scheduling, and configure notifications.

Prepare an HTTP service

If you already have an HTTP service, skip to Create the job in the console. Have the following ready:

  • The full URL of the HTTP service

  • The request method (GET or POST)

  • Any headers, cookies, or POST parameters

If you need to build an HTTP service, use the following Java examples as a starting point.

GET endpoint example:

@GET
@Path("hi")
@Produces(MediaType.APPLICATION_JSON)
public RestResult hi(@QueryParam("user") String user) {
    TestVo vo = new TestVo();
    vo.setName(user);
    RestResult result = new RestResult();
    result.setCode(200);
    result.setData(vo);
    return result;
}

POST endpoint example:

import com.alibaba.schedulerx.common.constants.CommonConstants;

@POST
@Path("createUser")
@Produces(MediaType.APPLICATION_JSON)
public RestResult createUser(@FormParam("userId") String userId,
        @FormParam("userName") String userName) {
    TestVo vo = new TestVo();
    System.out.println("userId=" + userId + ", userName=" + userName);
    vo.setName(userName);
    RestResult result = new RestResult();
    result.setCode(200);
    result.setData(vo);
    return result;
}

Create the job in the console

  1. Log on to the MSE console.

  2. Create an HTTP job. For general job creation steps, see Create a job.

  3. In the Basic Configuration step, configure the following parameters.

The following figures show the basic configuration for a GET request and a POST request:

  • GET request configuration: HTTP Serverles 任务

  • POST request configuration: Serverless HTTP POST

Note

The basic configuration parameters are the same for both serverless and agent mode.

Basic configuration parameters

ParameterDescription
Task nameA custom name for the job.
DescriptionA brief description for easy identification.
Application IDThe application group to which the job belongs. Select from the drop-down list.
Task typeSelect Http from the drop-down list.
Full urlThe complete URL, starting with http or https.
Request methodGET or POST.
Response analysis modeThe method SchedulerX uses to validate the HTTP response. Options: HTTP response code (validates by status code), Custom JSON (validates by a JSON key-value pair), Custom string (validates by string matching).
HTTP response codeThe expected HTTP status code. Default: 200. Available when Response analysis mode is set to HTTP response code.
Return check keyThe JSON key to check. Available when Response analysis mode is set to Custom JSON.
Return check valueThe expected JSON value. Available when Response analysis mode is set to Custom JSON.
Custom stringThe expected string to match against the response body. Available when Response analysis mode is set to Custom string.
Execution timeoutThe maximum execution time. Serverless mode: 30 seconds (Basic Edition), 120 seconds (Professional Edition). Agent mode: no limit.
ContentTypeThe request body format for POST requests. Options: application/x-www-form-urlencoded, application/json.
POST parametersThe request body for POST requests. Format depends on ContentType: key1=value&key2=value2 for form-urlencoded, {"key1":"val1","key2":"val2"} for JSON.
cookieCookie values in the format key1=val1;key2=val2. Multiple values are separated by semicolons (;). Maximum length: 300 bytes.
Execution wayThe execution mode: serverless (requests initiated by the server; URLs must be Internet-accessible) or agent (requests initiated by the client; supports internal URLs). Agent mode requires deploying the SchedulerX agent beforehand. See Deploy the SchedulerX agent for script or HTTP jobs.

Custom JSON validation example:

The following sample response shows how key-value validation works:

{
  "code": 200,
  "data": "true",
  "message": "",
  "requestId": "446655068791923614103381232971",
  "success": true
}

To validate this response, set Return check key to success and Return check value to true, or set the key to code and the value to 200.

Advanced configuration parameters

ParameterDescription
Task failure retry countThe number of retries after a failure. Default: 0.
Task failure retry intervalThe interval between retries, in seconds. Default: 30.
Task concurrencyThe maximum number of concurrent instances for this job. A value of 1 prevents overlapping runs. If the concurrency limit is reached, the current trigger is skipped.
Cleaning strategyThe policy for clearing job history records. Default: Keep last N entries. Options: Keep last N entries, Keep the last N entries by status.
Retained NumberThe number of history records to retain. Default: 300.

Set up scheduling

In the Timing configuration step, set the scheduling parameters and click Next Step.

创建任务-定时配置

Scheduling parameters

ParameterDescription
Time typeThe scheduling method. Options: none (no scheduling; typically used with workflows), cron (cron expression), api (API-triggered), fixed_rate (fixed interval), second_delay (second-level delay), one_time (single execution).
cron expressionA cron expression defining the schedule. Available when Time type is cron. Use the built-in tool to generate a valid expression.
Fixed frequencyThe interval in seconds between runs. Must be greater than 60. Available when Time type is fixed_rate.
Fixed delayThe delay in seconds between runs. Valid values: 1 to 60. Available when Time type is second_delay.
Scheduling timeThe date and time for a one-time execution. Available when Time type is one_time.

Advanced scheduling parameters

ParameterDescription
Time offsetThe offset between the data processing time and the job scheduling time. Retrieve this value from the scheduling context at runtime.
Time zoneThe time zone for scheduling. Select a country/region time zone or a GMT offset.
CalendarThe calendar that determines when the job runs. Options: Daily scheduling, Specify calendar (choose Financial day or Workday).
Effective timeWhen the schedule takes effect. Options: Effective immediately, Start Time (select a specific date and time).

Configure notifications

HTTP jobs support error alerts for issues such as timeouts or unexpected return values.

  1. In the Notification configuration step, configure alert conditions and contacts, then click Complete.

    创建任务-报警配置

  2. To trigger a manual test run, click Run Once in the Operation column of the job.

Retrieve job metadata from HTTP headers

SchedulerX passes job metadata in HTTP request headers. To read this metadata in your HTTP service, add the following Maven dependency:

<dependency>
    <groupId>com.aliyun.schedulerx</groupId>
    <artifactId>schedulerx2-common</artifactId>
    <version>1.6.0</version>
</dependency>

The following GET endpoint example reads the job ID and job name from the request headers:

import com.alibaba.schedulerx.common.constants.CommonConstants;

@GET
@Path("hi")
@Produces(MediaType.APPLICATION_JSON)
public RestResult hi(@QueryParam("user") String user,
        @HeaderParam(CommonConstants.JOB_ID_HEADER) String jobId,
        @HeaderParam(CommonConstants.JOB_NAME_HEADER) String jobName) {
    TestVo vo = new TestVo();
    vo.setName("armon");
    // Decode URL-encoded job name (required for non-ASCII characters)
    String decodedJobName = URLDecoder.decode(jobName, "utf-8");
    System.out.println("user=" + user + ", jobId=" + jobId + ", jobName=" + decodedJobName);
    RestResult result = new RestResult();
    result.setCode(200);
    result.setData(vo);
    return result;
}

Available header constants

All constants are defined in CommonConstants:

ConstantHeader keyDescription
JOB_ID_HEADERschedulerx-jobIdThe job ID
JOB_NAME_HEADERschedulerx-jobNameThe job name (English names supported)
SCHEDULE_TIMESTAMP_HEADERschedulerx-scheduleTimestampThe scheduling timestamp
DATA_TIMESTAMP_HEADERschedulerx-dataTimestampThe data processing timestamp
GROUP_ID_HEADERschedulerx-groupIdThe application group ID
USER_HEADERschedulerx-userThe username
MAX_ATTEMPT_HEADERschedulerx-maxAttemptThe maximum retry count for the instance
ATTEMPT_HEADERschedulerx-attemptThe current retry count for the instance
JOB_PARAMETERS_HEADERschedulerx-jobParametersThe job parameters
INSTANCE_PARAMETERS_HEADERschedulerx-instanceParametersThe instance parameters (set through API calls)

Verify the result

After a job runs, check the execution list for the result.

If a job fails, click Details to view the failure reason. Common failure scenarios include:

  • Return value mismatch: The actual response does not match the expected value configured in the response analysis mode. 返回值和期望不相同

  • Execution timeout: The job exceeded the configured timeout period. 执行超时