|
13 | 13 | sleep, |
14 | 14 | HttpRequest, |
15 | 15 | HttpResponse, |
16 | | - io::{http::{fetch, HttpBody}, blob::BlobGetError, kv::{self, KvSetNxPxError}, env}, |
| 16 | + io::{http::{fetch, HttpBody}, blob::BlobGetError, kv::{self, KvSetNxPxError}, env, tasks}, |
17 | 17 | StatusCode, |
18 | 18 | utils::{axum::handle_request, migrations::{Migrations, Migration, SqlMigrationError}}, |
19 | 19 | random, |
@@ -88,6 +88,8 @@ pub async fn http(mut req: HttpRequest) -> HttpResponse { |
88 | 88 | .route("/test/kv/distributed-lock", get(kv_distributed_lock)) |
89 | 89 | .route("/test/kv/pubsub/subscribe", get(kv_pubsub_subscribe)) |
90 | 90 | .route("/test/kv/pubsub/publish", post(kv_pubsub_publish)) |
| 91 | + .route("/test/tasks/background/start", post(kv_tasks_background_start)) |
| 92 | + .route("/test/tasks/background/status", get(kv_tasks_background_status)) |
91 | 93 | .route("/_fx/cron", get(handle_cron)) |
92 | 94 | .route("/", get(home)) |
93 | 95 | .layer(Extension(Metrics::new())), |
@@ -567,6 +569,22 @@ async fn kv_pubsub_publish(Json(req): Json<KvPubsubPublishRequest>) -> &'static |
567 | 569 | "ok.\n" |
568 | 570 | } |
569 | 571 |
|
| 572 | +async fn kv_tasks_background_start() -> &'static str { |
| 573 | + tasks::run_in_background(async { |
| 574 | + sleep(Duration::from_secs(1)).await; |
| 575 | + kv::Kv::new("test-namespace").set("background_task_status", "done").await; |
| 576 | + }); |
| 577 | + |
| 578 | + "ok.\n" |
| 579 | +} |
| 580 | + |
| 581 | +async fn kv_tasks_background_status() -> (StatusCode, &'static str) { |
| 582 | + match kv::Kv::new("test-namespace").get("background_task_status").await { |
| 583 | + Some(_) => (StatusCode::OK, "done."), |
| 584 | + None => (StatusCode::NOT_FOUND, "not done yet.") |
| 585 | + } |
| 586 | +} |
| 587 | + |
570 | 588 | #[derive(Clone)] |
571 | 589 | struct Metrics { |
572 | 590 | test_counter: Counter, |
|
0 commit comments