Skip to content

Commit 724fd8e

Browse files
authored
Merge pull request #10 from dimacurrentai/redis
Redis.
2 parents 9103660 + 7e08d96 commit 724fd8e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

step06_redis/code/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "redis_example"
3+
edition = "2024"
4+
5+
[dependencies]
6+
clap = { version = "4.0", features = ["derive"] }
7+
redis = { version = "0.25", features = ["tokio-comp"] }
8+
tokio = { version = "1", features = ["full"] }

step06_redis/code/src/main.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use clap::Parser;
2+
use redis::AsyncCommands;
3+
use std::future::Future;
4+
use std::process::ExitCode;
5+
6+
#[derive(Parser)]
7+
struct Args {
8+
#[arg(long, default_value = "redis://127.0.0.1")]
9+
redis: String,
10+
11+
#[arg(long)]
12+
mode: String,
13+
}
14+
15+
async fn run_check(args: &Args) -> Result<u8, redis::RedisError> {
16+
let _ = redis::Client::open(args.redis.to_string())?.get_multiplexed_async_connection().await?;
17+
println!("Redis is available.");
18+
Ok(0)
19+
}
20+
21+
async fn run_test(args: &Args) -> Result<u8, redis::RedisError> {
22+
let client = redis::Client::open(args.redis.to_string())?;
23+
let mut con = client.get_multiplexed_async_connection().await?;
24+
25+
let _: () = con.set("key", "value").await.expect("failed");
26+
let val: String = con.get("key").await?;
27+
28+
println!("Got value: {}", val);
29+
30+
if val == "value" { Ok(0) } else { Ok(1) }
31+
}
32+
33+
async fn run_arm<'a, F, RETVAL>(f: F, args: &'a Args) -> ExitCode
34+
where
35+
F: FnOnce(&'a Args) -> RETVAL,
36+
RETVAL: Future<Output = Result<u8, redis::RedisError>>,
37+
{
38+
let result = f(&args).await;
39+
ExitCode::from(match result {
40+
Ok(code) => code,
41+
_ => {
42+
println!("Something failed, or Redis is not available.");
43+
1
44+
}
45+
})
46+
}
47+
48+
#[tokio::main]
49+
async fn main() -> ExitCode {
50+
let args = Args::parse();
51+
match args.mode.as_str() {
52+
"check" => run_arm(&run_check, &args).await,
53+
"test" => run_arm(&run_test, &args).await,
54+
_ => {
55+
println!("This `--mode` value is not supported.");
56+
ExitCode::from(1)
57+
}
58+
}
59+
}

step06_redis/run.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
docker pull redis
6+
7+
docker build -f ../Dockerfile.template . -t demo
8+
9+
docker stop redis-for-rust >/dev/null 2>&1 || true
10+
docker rm redis-for-rust >/dev/null 2>&1 || true
11+
12+
docker run --rm --name redis-for-rust -p 6379:6379 -d redis
13+
14+
echo 'Waiting for Redis ...'
15+
16+
for i in $(seq 20); do
17+
if docker run --add-host=host.docker.internal:host-gateway --rm -t --network bridge demo --mode check --redis redis://host.docker.internal ; then
18+
echo 'Redis is up.'
19+
break
20+
else
21+
echo 'Need to wait more.'
22+
sleep 0.5
23+
fi
24+
done
25+
26+
echo 'Running the test.'
27+
docker run --add-host=host.docker.internal:host-gateway --rm -t demo --mode test --redis redis://host.docker.internal
28+
echo 'Test run successfully.'
29+
30+
docker stop redis-for-rust
31+
echo 'Redis stopped.'

0 commit comments

Comments
 (0)