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-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 ();
2007-10-26 21:14:35 +00:00
$retval = $instance -> init_conn (
2007-10-26 17:04:46 +00:00
" <bossa_db_user> " , " <bossa_db_passwd> " ,
" <bossa_db_host> " , " <bossa_db_name> "
);
2007-10-26 21:14:35 +00:00
if ( ! $retval ) return null ;
2007-10-26 17:04:46 +00:00
}
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
}
?>