risedev/task/
redis_service.rs1use std::env;
16use std::io::Write;
17use std::path::{Path, PathBuf};
18use std::process::Command;
19
20use anyhow::{Result, anyhow};
21
22use crate::util::stylized_risedev_subcmd;
23use crate::{ExecuteContext, RedisConfig, Task};
24
25pub struct RedisService {
26 pub config: RedisConfig,
27}
28
29impl RedisService {
30 pub fn new(config: RedisConfig) -> Result<Self> {
31 Ok(Self { config })
32 }
33
34 fn redis_path(&self) -> Result<PathBuf> {
35 let prefix_bin = env::var("PREFIX_BIN")?;
36 Ok(Path::new(&prefix_bin)
37 .join("redis")
38 .join("src")
39 .join("redis-server"))
40 }
41
42 fn redis(&self) -> Result<Command> {
43 Ok(Command::new(self.redis_path()?))
44 }
45}
46
47impl Task for RedisService {
48 fn execute(&mut self, ctx: &mut ExecuteContext<impl Write>) -> Result<()> {
49 ctx.service(self);
50 ctx.pb.set_message("starting");
51 let path = self.redis_path()?;
52 if !path.exists() {
53 return Err(anyhow!(
54 "Redis binary not found in {:?}\nDid you enable redis feature in `{}`?",
55 path,
56 stylized_risedev_subcmd("configure")
57 ));
58 }
59
60 let mut cmd = self.redis()?;
61 cmd.arg("--bind")
62 .arg(&self.config.address)
63 .arg("--port")
64 .arg(self.config.port.to_string())
65 .arg("--shutdown-on-sigint")
66 .arg("nosave");
67
68 ctx.run_command(ctx.tmux_run(cmd)?)?;
69 ctx.pb.set_message("started");
70
71 Ok(())
72 }
73
74 fn id(&self) -> String {
75 self.config.id.clone()
76 }
77}