1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use mlua::{FromLua, Lua, Result, Table, TableExt, Value};

/// The "Server" class provides a way for manipulating servers and retrieving information.
#[derive(Clone)]
pub struct Server<'lua> {
    class: Table<'lua>,
    pub name: String,
    pub puid: String,
}

impl<'lua> Server<'lua> {
    /// Returns true if the server is currently draining sticky connections.
    pub fn is_draining(&self) -> Result<bool> {
        self.class.call_method("is_draining", ())
    }

    /// Dynamically changes the maximum connections of the server.
    pub fn set_maxconn(&self, maxconn: u64) -> Result<()> {
        self.class.call_method("set_maxconn", maxconn)
    }

    /// Returns an integer representing the server maximum connections.
    pub fn get_maxconn(&self) -> Result<u64> {
        self.class.call_method("get_maxconn", ())
    }

    /// Dynamically changes the weight of the server.
    /// See the management socket documentation for more information about the format of the string.
    pub fn set_weight(&self, weight: &str) -> Result<()> {
        self.class.call_method("set_weight", weight)
    }

    /// Returns an integer representing the server weight.
    pub fn get_weight(&self) -> Result<u32> {
        self.class.call_method("get_weight", ())
    }

    /// Dynamically changes the address of the server.
    pub fn set_addr(&self, addr: String, port: Option<u16>) -> Result<()> {
        self.class.call_method("set_addr", (addr, port))
    }

    /// Returns a string describing the address of the server.
    pub fn get_addr(&self) -> Result<String> {
        self.class.call_method("get_addr", ())
    }

    /// Returns a table containing the server statistics.
    pub fn get_stats(&self) -> Result<Table<'lua>> {
        self.class.call_method("get_stats", ())
    }

    /// Shutdowns all the sessions attached to the server.
    pub fn shut_sess(&self) -> Result<()> {
        self.class.call_method("shut_sess", ())
    }

    /// Drains sticky sessions.
    pub fn set_drain(&self) -> Result<()> {
        self.class.call_method("set_drain", ())
    }

    /// Sets maintenance mode.
    pub fn set_maint(&self) -> Result<()> {
        self.class.call_method("set_maint", ())
    }

    /// Sets normal mode.
    pub fn set_ready(&self) -> Result<()> {
        self.class.call_method("set_ready", ())
    }

    /// Enables health checks.
    pub fn check_enable(&self) -> Result<()> {
        self.class.call_method("check_enable", ())
    }

    /// Disables health checks.
    pub fn check_disable(&self) -> Result<()> {
        self.class.call_method("check_disable", ())
    }

    /// Forces health-check up.
    pub fn check_force_up(&self) -> Result<()> {
        self.class.call_method("check_force_up", ())
    }

    /// Forces health-check nolb mode.
    pub fn check_force_nolb(&self) -> Result<()> {
        self.class.call_method("check_force_nolb", ())
    }

    /// Forces health-check down.
    pub fn check_force_down(&self) -> Result<()> {
        self.class.call_method("check_force_down", ())
    }

    /// Enables agent check.
    pub fn agent_enable(&self) -> Result<()> {
        self.class.call_method("agent_enable", ())
    }

    /// Disables agent check.
    pub fn agent_disable(&self) -> Result<()> {
        self.class.call_method("agent_disable", ())
    }

    /// Forces agent check up.
    pub fn agent_force_up(&self) -> Result<()> {
        self.class.call_method("agent_force_up", ())
    }

    /// Forces agent check down.
    pub fn agent_force_down(&self) -> Result<()> {
        self.class.call_method("agent_force_down", ())
    }
}

impl<'lua> FromLua<'lua> for Server<'lua> {
    fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self> {
        let class = Table::from_lua(value, lua)?;
        Ok(Server {
            name: class.get("name")?,
            puid: class.get("puid")?,
            class,
        })
    }
}