From b381e54d992e8ffa5800a4b3c3fc9728a42bda3a Mon Sep 17 00:00:00 2001 From: Karsten Heiken Date: Fri, 22 Apr 2011 02:49:53 +0200 Subject: [PATCH] Implement a simple rpc system Servers can transmit their workload and jobstatus updates. --- application/controllers/statusrpc.php | 49 +++++++++++++++++++++++++++ application/models/job.php | 23 +++++++++++++ application/models/server.php | 40 ++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 application/controllers/statusrpc.php create mode 100644 application/models/job.php create mode 100644 application/models/server.php diff --git a/application/controllers/statusrpc.php b/application/controllers/statusrpc.php new file mode 100644 index 0000000..3696f0b --- /dev/null +++ b/application/controllers/statusrpc.php @@ -0,0 +1,49 @@ +load->model('job'); + + $query = $this->db->get_where('servers', array('secret' => $secret)); + + // if we are in production mode, we do not want to tell the evil hacker + // that he used a wrong secret. That just encourages him. + if($query->num_rows() < 1 && ENVIRONMENT != 'production') { + die("Unauthorized access."); + } + + $this->job->update($job_id, $progress); + } + + /** + * Update the workload of the server. + */ + public function update_workload($secret, $workload) { + $this->load->model('server'); + $query = $this->db->get_where('servers', array('secret' => $secret)); + + // if we are in production mode, we do not want to tell the evil hacker + // that he used a wrong secret. That just encourages him. + if($query->num_rows() < 1 && ENVIRONMENT != 'production') { + die("Unauthorized access."); + } + + $this->server->update_workload($secret, $workload); + } +} \ No newline at end of file diff --git a/application/models/job.php b/application/models/job.php new file mode 100644 index 0000000..de6d45e --- /dev/null +++ b/application/models/job.php @@ -0,0 +1,23 @@ +db->query("UPDATE `jobs` SET `progress`=". + $this->db->escape($progress).$finished_at. " WHERE `id`=". + $this->db->escape($job_id)); + } +} diff --git a/application/models/server.php b/application/models/server.php new file mode 100644 index 0000000..b4d8570 --- /dev/null +++ b/application/models/server.php @@ -0,0 +1,40 @@ +db->get('servers')->result_array(); + } + + /** + * Get a list of servers that could handle another job. + * + * @return array List of servers that could handle another job. + */ + public function get_idle() { + return $this->db->get_where('servers', 'workload <= 2')->result_array(); + } + + /** + * Set a server's workload. + * + * In order to check if a server can handle another job we need to know + * the workload of every server. + * + * @param type $secret The server's secret for basic authentication. + * @param type $workload The server's workload. + */ + public function update_workload($secret, $workload) { + $this->db->query("UPDATE `servers` SET `workload`=".$this->db->escape($workload) + . ", `last_update`=NOW()" + . " WHERE `secret`=".$this->db->escape($secret)); + } +}