*/ class Server extends CI_Model { /** * Creates a new server. * * @param array $data The server informations * @return boolean Returns TRUE if the insert was successful. */ public function create($data) { if ($this->db->insert('servers', $data)) { return $data['id']; } else { return false; } } /** * Deletes a server. * * @param string $serverId * @return boolean Returns TRUE if the deletion was successful. */ public function delete($serverId) { $this->db->delete('servers', array('id' => $serverId)); return $this->db->affected_rows() > 0; } /** * Gets a list of all available servers. * * @return array The list of all available servers */ public function getAll() { $servers = $this->db->get('servers')->result_array(); return array_map(function($var) { $var['available'] = time_diff($var['last_update'], mysql_now()) < 120 ? true : false; return $var; }, $servers); } /** * Gets a list of servers that could handle another job. * * @return array The list of servers that could handle another job */ public function getIdle() { return $this->db->get_where('servers', 'workload <= 2')->result_array(); } /** * Updates a server. * * @param type $secret The server's secret for basic authentication * @param type $workload The server's workload * @return boolean Returns TRUE if the update was successful. */ public function update($serverId, $data) { $this->db->where('id', $serverId)->update('servers', $data); return $this->db->affected_rows() > 0; } /** * Get the best suiting server for a new job. * * @todo not yet verified. */ public function getBestServer() { return $this->db->limit(1)->order_by('last_update', 'desc')-> get_where('servers', 'workload <= 2')->row_array(); } /** * * @param string $secret */ public function getBySecret($secret) { return $this->db->get_where('servers', array('secret' => $secret))->row(); } /** * * @param string $serverId */ public function getById($serverId) { $server = $this->db->get_where('servers', array('id' => $serverId))->row(); if (is_object($server)) { $server->uptimestring = prettyTime($server->uptime); $server->lastheartbeat = prettyTime(time_diff($server->last_update, mysql_now())); } return $server; } } /* End of file server.php */ /* Location: ./application/models/server.php */