2007-10-16 17:12:48 +00:00
< ? php
2007-10-26 21:14:35 +00:00
require_once ( " ../inc/db_conn.inc " );
2007-10-30 22:31:13 +00:00
require_once ( " ../inc/util.inc " );
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 )) {
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
}
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
}
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 ();
2008-02-11 23:38:31 +00:00
$query = " insert into DBNAME.bossa_job (create_time, name, app_id, info, batch, time_estimate, time_limit, nneeded) values ( $now , ' $this->name ', $this->app_id , ' $this->info ', $this->batch , $this->time_estimate , $this->time_limit , $this->nneeded ) " ;
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 ();
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
//
2008-02-11 23:38:31 +00:00
// TODO: put the following in a transaction
2007-10-26 17:04:46 +00:00
$db = BossaDb :: get ();
2008-02-11 23:38:31 +00:00
$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.nneeded>0 and bossa_job_inst.user_id is null 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 ;
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
$job -> update ( " nneeded=nneeded-1 " );
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 ' " );
$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
}
?>