2007-10-16 17:12:48 +00:00
< ? php
2007-10-26 17:04:46 +00:00
ini_set ( 'display_errors' , 'stdout' );
error_reporting ( E_ALL );
2007-10-25 21:34:13 +00:00
require_once ( " ../inc/util.inc " );
2007-10-26 17:04:46 +00:00
class DbConn {
private $db_conn ;
private $db_name ;
2007-10-16 23:02:13 +00:00
2007-10-26 17:04:46 +00:00
function init_conn ( $user_tag , $passwd_tag , $host_tag , $name_tag ) {
2007-10-25 21:34:13 +00:00
$config = get_config ();
2007-10-26 17:04:46 +00:00
$user = parse_config ( $config , $user_tag );
$pass = parse_config ( $config , $passwd_tag );
$host = parse_config ( $config , $host_tag );
2007-10-25 21:34:13 +00:00
if ( $host == null ) {
$host = " localhost " ;
}
2007-10-26 17:04:46 +00:00
$this -> db_conn = mysql_pconnect ( $host , $user , $pass );
if ( ! $this -> db_conn ) {
2007-10-25 21:34:13 +00:00
error_page ( " can't connect to database " );
}
2007-10-26 17:04:46 +00:00
$this -> db_name = parse_config ( $config , $name_tag );
}
function do_query ( $query ) {
$q = str_replace ( 'DBNAME' , $this -> db_name , $query );
//echo $q;
return mysql_query ( $q , $this -> db_conn );
2007-10-25 21:34:13 +00:00
}
2007-10-26 17:04:46 +00:00
function lookup ( $table , $classname , $clause ) {
$query = " select * from DBNAME. $table where $clause " ;
$result = $this -> do_query ( $query );
2007-10-25 21:34:13 +00:00
if ( ! $result ) {
echo $query ;
echo mysql_error ();
return null ;
}
$obj = mysql_fetch_object ( $result , $classname );
mysql_free_result ( $result );
return $obj ;
}
2007-10-26 17:04:46 +00:00
function lookup_id ( $id , $table , $classname ) {
return $this -> lookup ( $table , $classname , " id= $id " );
}
function enum ( $table , $classname , $clause = null ) {
2007-10-25 21:34:13 +00:00
$x = array ();
if ( $clause ) {
2007-10-26 17:04:46 +00:00
$query = " select * from DBNAME. $table where $clause " ;
2007-10-25 21:34:13 +00:00
} else {
2007-10-26 17:04:46 +00:00
$query = " select * from DBNAME. $table " ;
2007-10-25 21:34:13 +00:00
}
2007-10-26 17:04:46 +00:00
$result = $this -> do_query ( $query );
2007-10-25 21:34:13 +00:00
if ( ! $result ) return null ;
while ( $obj = mysql_fetch_object ( $result , $classname )) {
$x [] = $obj ;
}
mysql_free_result ( $result );
return $x ;
}
2007-10-26 17:04:46 +00:00
function update ( $obj , $table , $clause ) {
$query = " update DBNAME. $table set $clause where id= $obj->id " ;
return $this -> do_query ( $query );
}
function insert_id () {
return mysql_insert_id ( $this -> db_conn );
2007-10-25 21:34:13 +00:00
}
2007-10-21 20:54:24 +00:00
}
2007-10-26 17:04:46 +00:00
class BossaDb extends DbConn {
public static $instance ;
static function get () {
if ( ! isset ( $instance )) {
$instance = new DbConn ();
$instance -> init_conn (
" <bossa_db_user> " , " <bossa_db_passwd> " ,
" <bossa_db_host> " , " <bossa_db_name> "
);
}
return $instance ;
}
}
class BossaApp {
2007-10-16 23:02:13 +00:00
function insert () {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
if ( ! isset ( $this -> long_jobs )) $this -> long_jobs = 0 ;
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-26 17:04:46 +00:00
$query = " insert into DBNAME.bossa_app (create_time, name, user_friendly_name, long_jobs, start_url) values ( $now , ' $this->name ', ' $this->user_friendly_name ', $this->long_jobs , ' $this->start_url ') " ;
$result = $db -> do_query ( $query );
2007-10-16 17:12:48 +00:00
if ( ! $result ) return false ;
2007-10-26 17:04:46 +00:00
$this -> id = $db -> insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
static function lookup_name ( $name ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup ( 'bossa_app' , 'BossaApp' , " name=' $name ' " );
2007-10-16 17:12:48 +00:00
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_app' , 'BossaApp' );
2007-10-16 17:12:48 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum () {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_app' , 'BossaApp' );
2007-10-21 20:54:24 +00:00
}
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
2007-10-26 17:04:46 +00:00
class BossaJob {
2007-10-16 23:02:13 +00:00
function insert () {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-26 17:04:46 +00:00
$query = " insert into DBNAME.bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, more_needed, npending, nsuccess, nsuccess_needed) values ( $now , ' $this->name ', $this->app_id , ' $this->info ', $this->batch , $this->time_estimate , $this->time_limit , 1, 0, 0, $this->nsuccess_needed ) " ;
$result = $db -> do_query ( $query );
2007-10-16 17:12:48 +00:00
if ( ! $result ) {
echo " $query\n " ;
return false ;
}
2007-10-26 17:04:46 +00:00
$this -> id = $db -> insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
function update ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> update ( 'bossa_job' , $clause );
2007-10-16 23:02:13 +00:00
}
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_job' , 'BossaJob' );
2007-10-16 23:02:13 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_job' , 'BossaJob' , $clause );
2007-10-21 20:54:24 +00:00
}
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
2007-10-26 17:04:46 +00:00
class BossaJobInst {
2007-10-16 23:02:13 +00:00
function insert () {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2007-10-16 17:12:48 +00:00
$now = time ();
2007-10-26 17:04:46 +00:00
$query = " insert into DBNAME.bossa_job_inst (create_time, job_id, user_id) values ( $now , $this->job_id , $this->user_id ) " ;
$result = $db -> do_query ( $query );
2007-10-16 17:12:48 +00:00
if ( ! $result ) {
echo " $query\n " ;
return false ;
}
2007-10-26 17:04:46 +00:00
$this -> id = $db -> insert_id ();
2007-10-16 17:12:48 +00:00
return true ;
}
2007-10-16 23:02:13 +00:00
static function lookup_id ( $id ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> lookup_id ( $id , 'bossa_job_inst' , 'BossaJobInst' );
2007-10-16 23:02:13 +00:00
}
2007-10-21 20:54:24 +00:00
static function enum ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> enum ( 'bossa_job_inst' , 'BossaJobInst' , $clause );
2007-10-21 20:54:24 +00:00
}
2007-10-16 23:02:13 +00:00
function update ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
return $db -> update ( $this , 'bossa_job_inst' , $clause );
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
// Assign a job from the given app to the given user.
// Returns the job instance or NULL.
//
2007-10-16 23:02:13 +00:00
static function assign ( $app , $user ) {
2007-10-16 17:12:48 +00:00
// this query skips jobs for which this user
// has already been assigned an instance
//
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
$query = " select bossa_job.* from DBNAME.bossa_job left join DBNAME.bossa_job_inst on bossa_job_inst.job_id = bossa_job.id where bossa_job.more_needed<>0 and bossa_job_inst.user_id is null limit 1 " ;
$result = $db -> do_query ( $query );
2007-10-16 17:12:48 +00:00
if ( ! $result ) return null ;
2007-10-18 21:43:25 +00:00
$job = mysql_fetch_object ( $result , 'BossaJob' );
if ( ! $job ) return null ;
2007-10-16 17:12:48 +00:00
mysql_free_result ( $result );
2007-10-16 23:02:13 +00:00
$ji = new BossaJobInst ();
2007-10-16 17:12:48 +00:00
$ji -> user_id = $user -> id ;
$ji -> job_id = $job -> id ;
2007-10-16 23:02:13 +00:00
if ( ! $ji -> insert ()) {
2007-10-16 17:12:48 +00:00
echo mysql_error ();
return null ;
}
return $ji ;
}
2007-10-16 23:02:13 +00:00
// The given job instance has completed
//
function completed ( $job ) {
$this -> finish_time = time ();
$this -> update ( " finish_time= $this->finish_time , info=' $this->info ' " );
$job -> npending -- ;
$job -> nsuccess ++ ;
$job -> more_needed = ( $job -> npending + $job -> nsuccess < $job -> nsuccess_needed );
2007-10-26 17:04:46 +00:00
return $job -> update ( " npending= $job->npending , nsuccess= $job->nsuccess , more_needed= $job->more_needed " );
2007-10-16 23:02:13 +00:00
}
// The given job instance has timed out.
//
function timed_out ( $job ) {
$job -> npending -- ;
$job -> more_needed = ( $job -> npending + $job -> nsuccess < $job -> nsuccess_needed );
2007-10-26 17:04:46 +00:00
return $job -> update ( " npending= $job->npending , more_needed= $job->more_needed " );
2007-10-16 23:02:13 +00:00
}
2007-10-16 17:12:48 +00:00
}
?>