任务生命周期
Scheduler 维护一个内存延迟队列。任务使用 id、future 或 delay、payload 以及字符串数组 labels 入队。工作者通过 lease 预留任务,并通过删除任务来确认完成。
入队
curl -X POST "https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks" \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"id": "task-001",
"payload": { "action": "generate_report" },
"delay": "1h",
"labels": ["team:analytics", "env:production"]
}'POST /schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks HTTP/1.1
Host: api.hola.cloud
X-API-Key: YOUR_API_KEY
Content-Type: application/json
{
"id": "task-001",
"payload": { "action": "generate_report" },
"delay": "1h",
"labels": ["team:analytics", "env:production"]
}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"delay": "1h", "id": "task-001", "labels": []any{"team:analytics", "env:production"}, "payload": map[string]any{"action": "generate_report"}}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("X-API-Key", "YOUR_API_KEY")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['delay' => '1h', 'id' => 'task-001', 'labels' => ['team:analytics', 'env:production'], 'payload' => ['action' => 'generate_report']];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'X-API-Key: YOUR_API_KEY',
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"X-API-Key": "YOUR_API_KEY",
"Content-Type": "application/json",
}
payload = {"delay": "1h", "id": "task-001", "labels": ["team:analytics", "env:production"], "payload": {"action": "generate_report"}}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks",
headers=headers,
data=body
)
print(response.text)
const payload = {"delay": "1h", "id": "task-001", "labels": ["team:analytics", "env:production"], "payload": {"action": "generate_report"}};
const response = await fetch("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks", {
method: "POST",
headers: {
"X-API-Key": "YOUR_API_KEY",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"delay": "1h", "id": "task-001", "labels": ["team:analytics", "env:production"], "payload": {"action": "generate_report"}};
const response = await fetch("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks", {
method: "POST",
headers: {
"X-API-Key": "YOUR_API_KEY",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("delay", "1h", "id", "task-001", "labels", List.of("team:analytics", "env:production"), "payload", Map.of("action", "generate_report"));
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("X-API-Key", "YOUR_API_KEY")
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}响应:HTTP 202 Accepted,响应体为空。
列表
1{
2 "scheduled": [
3 {
4 "id": "task-001",
5 "future": "2025-06-22T00:00:00Z",
6 "labels": ["team:analytics"]
7 }
8 ],
9 "inflight": [],
10 "scheduled_meta": { "page": 1, "per_page": 25, "total": 1, "total_pages": 1 },
11 "inflight_meta": { "page": 1, "per_page": 25, "total": 0, "total_pages": 0 }
12}
预留
1{ "worktime": "60s" }
1{
2 "id": "task-001",
3 "payload": { "action": "generate_report" },
4 "lease_expires_at": "2025-06-21T12:01:00Z",
5 "labels": ["team:analytics"]
6}
没有可用任务时,API 返回 204 No Content。
延长 Lease
1{ "extension": "30s" }
1{ "lease_expires_at": "2025-06-21T12:01:30Z" }
确认
工作者通过删除任务确认完成:
curl -X DELETE "https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001" \
-H "X-API-Key: YOUR_API_KEY"DELETE /schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001 HTTP/1.1
Host: api.hola.cloud
X-API-Key: YOUR_API_KEY
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("DELETE", "https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001", nil)
if err != nil {
panic(err)
}
req.Header.Set("X-API-Key", "YOUR_API_KEY")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => [
'X-API-Key: YOUR_API_KEY',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
headers = {
"X-API-Key": "YOUR_API_KEY",
}
response = requests.request(
"DELETE",
"https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001",
headers=headers
)
print(response.text)
const response = await fetch("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001", {
method: "DELETE",
headers: {
"X-API-Key": "YOUR_API_KEY"
}
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001", {
method: "DELETE",
headers: {
"X-API-Key": "YOUR_API_KEY"
}
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/schedulers/sched-a1b2c3d4-e5f6-7890-abcd-ef1234567890/tasks/task-001"))
.method("DELETE", HttpRequest.BodyPublishers.noBody())
.header("X-API-Key", "YOUR_API_KEY")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}响应:HTTP 204 No Content。
重新调度
1{ "delay": "5m" }
1{
2 "id": "task-001",
3 "future": "2025-06-21T12:06:00Z"
4}
SSE
SSE stream 只发送 snapshot 事件,包含 scheduled、inflight、分页 meta 和 health。
评论