2007-10-16 17:12:48 +00:00
< ? php
2007-10-26 21:14:35 +00:00
require_once ( " ../inc/db_conn.inc " );
2008-06-17 17:38:02 +00:00
require_once ( " ../inc/util_basic.inc " );
2007-10-21 20:54:24 +00:00
2008-02-13 04:25:52 +00:00
define ( 'VALIDATE_STATE_INIT' , 0 );
define ( 'VALIDATE_STATE_VALID' , 1 );
define ( 'VALIDATE_STATE_INVALID' , 2 );
2007-10-26 17:04:46 +00:00
class BossaDb extends DbConn {
public static $instance ;
static function get () {
if ( ! isset ( $instance )) {
2007-10-30 22:31:13 +00:00
$config = get_config ();
$user = parse_config ( $config , '<bossa_db_user>' );
$passwd = parse_config ( $config , '<bossa_db_passwd>' );
$host = parse_config ( $config , '<bossa_db_host>' );
$name = parse_config ( $config , '<bossa_db_name>' );
if ( $host == null ) {
$host = " localhost " ;
}
2007-10-26 17:04:46 +00:00
$instance = new DbConn ();
2007-10-30 22:31:13 +00:00
$retval = $instance -> init_conn ( $user , $passwd , $host , $name );
2007-10-26 21:14:35 +00:00
if ( ! $retval ) return null ;
2007-10-26 17:04:46 +00:00
}
return $instance ;
}
2008-02-11 23:38:31 +00:00
static function escape_string ( $string ) {
$db = self :: get ();
return $db -> base_escape_string ( $string );
}
2007-10-26 17:04:46 +00:00
}
2008-02-13 04:25:52 +00:00
class BossaUser {
static $cache ;
static function lookup_userid ( $id ) {
$db = BossaDb :: get ();
return $db -> lookup ( 'bossa_user' , 'BossaUser' , " user_id= $id " );
}
static function insert ( $clause ) {
$db = BossaDb :: get ();
return $db -> insert ( 'bossa_user' , $clause );
}
static function lookup ( & $user ) {
if ( ! $user ) return ;
if ( isset ( $user -> bossa )) return ;
if ( isset ( self :: $cache [ $user -> id ])) {
$bossa = self :: $cache [ $user -> id ];
} else {
$bossa = self :: lookup_userid ( $user -> id );
if ( ! $bossa ) {
self :: insert ( " (user_id) values ( $user->id ) " );
$bossa = self :: lookup_userid ( $user -> id );
}
self :: $cache [ $user -> id ] = $bossa ;
}
$user -> bossa = $bossa ;
}
function update ( $clause ) {
$db = BossaDb :: get ();
$clause = " $clause where user_id= $this->user_id " ;
return $db -> update_aux ( 'bossa_user' , $clause );
}
}
2007-10-26 17:04:46 +00:00
class BossaApp {
2008-02-11 23:38:31 +00:00
function insert ( $clause ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-02-11 23:38:31 +00:00
return $db -> insert ( 'bossa_app' , $clause );
2007-10-16 17:12:48 +00:00
}
2008-02-13 04:25:52 +00:00
static function lookup_short_name ( $name ) {
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-02-13 04:25:52 +00:00
return $db -> lookup ( 'bossa_app' , 'BossaApp' , " short_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
}
2008-02-15 15:25:44 +00:00
function update ( $clause ) {
$db = BossaDb :: get ();
return $db -> update ( $this , 'bossa_app' , $clause );
}
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 ();
2008-02-15 15:25:44 +00:00
$int_max = 2147483647 ;
$query = " insert into DBNAME.bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, conf_needed, transition_time) values ( $now , ' $this->name ', $this->app_id , ' $this->info ', $this->batch , $this->time_estimate , $this->time_limit , $this->conf_needed , $int_max ) " ;
2007-10-26 17:04:46 +00:00
$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 ();
2008-02-12 15:04:27 +00:00
return $db -> update ( $this , '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 ();
2008-05-30 03:29:31 +00:00
$query = " insert into DBNAME.bossa_job_inst (create_time, app_id, job_id, user_id, confidence) values ( $now , $this->app_id , $this->job_id , $this->user_id , $this->confidence ) " ;
2007-10-26 17:04:46 +00:00
$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 ) {
2008-05-30 03:29:31 +00:00
$db = BossaDb :: get ();
// first look for unfinished jobs
//
$query = " select * from DBNAME.bossa_job_inst where app_id= $app->id and user_id= $user->id and finish_time=0 limit 1 " ;
$result = $db -> do_query ( $query );
if ( $result ) {
$ji = mysql_fetch_object ( $result , 'BossaJobInst' );
mysql_free_result ( $result );
2008-06-01 03:43:47 +00:00
if ( $ji ) return $ji ;
2008-05-30 03:29:31 +00:00
}
2007-10-16 17:12:48 +00:00
// this query skips jobs for which this user
// has already been assigned an instance
//
2008-02-11 23:38:31 +00:00
// TODO: put the following in a transaction
2008-05-29 20:11:43 +00:00
$query = " select * from DBNAME.bossa_job where app_id= $app->id and (select count(*) from DBNAME.bossa_job_inst where job_id=bossa_job.id and user_id= $user->id ) = 0 limit 1 " ;
2007-10-26 17:04:46 +00:00
$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 ;
2008-02-15 15:25:44 +00:00
$ji -> confidence = $user -> conf ;
2008-05-30 03:29:31 +00:00
$ji -> app_id = $app -> id ;
2007-10-16 17:12:48 +00:00
2007-10-16 23:02:13 +00:00
if ( ! $ji -> insert ()) {
2007-10-16 17:12:48 +00:00
echo mysql_error ();
return null ;
}
2008-02-11 23:38:31 +00:00
2008-02-13 19:02:44 +00:00
$job -> update ( " conf_needed=conf_needed- $user->conf " );
2007-10-16 17:12:48 +00:00
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 ' " );
2008-02-12 15:04:27 +00:00
$now = time ();
return $job -> update ( " transition_time= $now " );
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
}
?>