|
1 | 1 | //! Turn maintenance mode on/off. |
| 2 | +//! |
| 3 | +//! Maintenance mode is special: it's completely independent from the config |
| 4 | +//! and will hold true during config changes, e.g. when some databases disappear |
| 5 | +//! and new ones are added. |
| 6 | +//! |
| 7 | +//! This is useful when changing the sharding config online, for example. |
| 8 | +//! |
2 | 9 |
|
3 | 10 | use crate::backend::maintenance_mode; |
4 | 11 |
|
5 | 12 | use super::prelude::*; |
6 | 13 |
|
7 | | -/// Turn maintenance mode on/off. |
| 14 | +/// Turn maintenance mode on/off, optionally for a single database. |
8 | 15 | #[derive(Default)] |
9 | 16 | pub struct MaintenanceMode { |
10 | 17 | enable: bool, |
| 18 | + database: Option<String>, |
11 | 19 | } |
12 | 20 |
|
13 | 21 | #[async_trait] |
14 | 22 | impl Command for MaintenanceMode { |
15 | 23 | fn parse(sql: &str) -> Result<Self, Error> { |
16 | 24 | let parts = sql.split(" ").collect::<Vec<_>>(); |
17 | 25 |
|
18 | | - match parts[..] { |
19 | | - ["maintenance", "on"] => Ok(Self { enable: true }), |
20 | | - ["maintenance", "off"] => Ok(Self { enable: false }), |
21 | | - _ => Err(Error::Syntax), |
22 | | - } |
| 26 | + let (enable, database) = match parts[..] { |
| 27 | + ["maintenance", "on"] => (true, None), |
| 28 | + ["maintenance", "off"] => (false, None), |
| 29 | + ["maintenance", "on", database] => (true, Some(database.to_string())), |
| 30 | + ["maintenance", "off", database] => (false, Some(database.to_string())), |
| 31 | + _ => return Err(Error::Syntax), |
| 32 | + }; |
| 33 | + |
| 34 | + Ok(Self { enable, database }) |
23 | 35 | } |
24 | 36 |
|
25 | 37 | async fn execute(&self) -> Result<Vec<Message>, Error> { |
| 38 | + let database = self.database.as_deref(); |
26 | 39 | if self.enable { |
27 | | - maintenance_mode::start(); |
| 40 | + maintenance_mode::start(database); |
28 | 41 | } else { |
29 | | - maintenance_mode::stop(); |
| 42 | + maintenance_mode::stop(database); |
30 | 43 | } |
31 | 44 |
|
32 | 45 | Ok(vec![]) |
33 | 46 | } |
34 | 47 |
|
35 | 48 | fn name(&self) -> String { |
36 | | - format!("MAINTENANCE {}", if self.enable { "ON" } else { "OFF" }) |
| 49 | + let state = if self.enable { "ON" } else { "OFF" }; |
| 50 | + match &self.database { |
| 51 | + Some(database) => format!("MAINTENANCE {} {}", state, database), |
| 52 | + None => format!("MAINTENANCE {}", state), |
| 53 | + } |
37 | 54 | } |
38 | 55 | } |
0 commit comments