Skip to main content

risedev/task/
task_clickhouse_ready_check.rs

1// Copyright 2026 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::io::Write;
16use std::time::Duration;
17
18use anyhow::{Context, Result, ensure};
19use reqwest::blocking::Client;
20
21use crate::{ClickHouseConfig, ExecuteContext, Task};
22
23pub struct ClickHouseReadyCheckTask {
24    config: ClickHouseConfig,
25}
26
27impl ClickHouseReadyCheckTask {
28    pub fn new(config: ClickHouseConfig) -> Self {
29        Self { config }
30    }
31}
32
33impl Task for ClickHouseReadyCheckTask {
34    fn execute(&mut self, ctx: &mut ExecuteContext<impl Write>) -> Result<()> {
35        ctx.pb.set_message("waiting for online...");
36        let url = format!(
37            "http://{}:{}/?query=SELECT%201",
38            self.config.address, self.config.http_port
39        );
40        let client = Client::builder().timeout(Duration::from_secs(1)).build()?;
41
42        ctx.wait(|| {
43            let response = client
44                .get(&url)
45                .basic_auth(&self.config.user, Some(&self.config.password))
46                .send()
47                .context("failed to query ClickHouse")?;
48            ensure!(
49                response.status().is_success(),
50                "ClickHouse returned HTTP status {}",
51                response.status()
52            );
53            ensure!(
54                response.text()?.trim() == "1",
55                "unexpected ClickHouse response"
56            );
57            Ok(())
58        })?;
59
60        ctx.complete_spin();
61        Ok(())
62    }
63}