phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] CVS: phpgwapi/inc class.asyncservice.inc.php,1.1.2.2


From: Ralf Becker <address@hidden>
Subject: [Phpgroupware-cvs] CVS: phpgwapi/inc class.asyncservice.inc.php,1.1.2.2,1.1.2.3
Date: Sat, 31 May 2003 19:47:46 -0400

Update of /cvsroot/phpgroupware/phpgwapi/inc
In directory subversions:/tmp/cvs-serv15950

Modified Files:
      Tag: Version-0_9_16-branch
        class.asyncservice.inc.php 
Log Message:
added administration interface to the new async-services


Index: class.asyncservice.inc.php
===================================================================
RCS file: /cvsroot/phpgroupware/phpgwapi/inc/class.asyncservice.inc.php,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -r1.1.2.2 -r1.1.2.3
*** class.asyncservice.inc.php  26 Apr 2003 16:39:02 -0000      1.1.2.2
--- class.asyncservice.inc.php  31 May 2003 23:47:44 -0000      1.1.2.3
***************
*** 32,36 ****
                        'install'   => True,
                        'installed' => True,
!                       'test'      => True
                );
                var $php = '';
--- 32,36 ----
                        'install'   => True,
                        'installed' => True,
!                       'last_check_run' => True
                );
                var $php = '';
***************
*** 38,45 ****
--- 38,50 ----
                var $db;
                var $db_table = 'phpgw_async';
+               var $debug = 0;
                
                function asyncservice()
                {
                        $this->db = $GLOBALS['phpgw']->db;
+                               
+                       $this->cronline = PHPGW_SERVER_ROOT . 
'/phpgwapi/cron/asyncservices.php '.$GLOBALS['phpgw_info']['user']['domain'];
+                       
+                       $this->only_fallback = substr(php_uname(), 0, 7) == 
"Windows";  // atm cron-jobs dont work on win
                }
  
***************
*** 88,95 ****
--- 93,106 ----
                        for every 5mins in the time from 9am to 5pm. All not 
set units before the smallest one set, \
                        are taken into account as every possible value, all 
after as the smallest possible value. 
+               @param $debug if True some debug-messages about syntax-errors 
in $times are echoed
                @returns a unix timestamp of the next execution time or False 
if no more executions
                */
                function next_run($times,$debug=False)
                {
+                       if ($this->debug)
+                       {
+                               echo "<p>next_run("; print_r($times); 
",'$debug')</p>\n";
+                               $debug = True;  // enable syntax-error messages 
too
+                       }
                        $now = time();
                        
***************
*** 155,159 ****
                        {
                                ++$n;
!                               //echo "<p>$u: isset(times[$u]="; 
print_r($times[$u]); echo ")=".(isset($times[$u])?'True':'False')."</p>\n";
                                if (isset($times[$u]))
                                {
--- 166,170 ----
                        {
                                ++$n;
!                               if ($this->debug) { echo "<p>n=$n, $u: 
isset(times[$u]="; print_r($times[$u]); echo 
")=".(isset($times[$u])?'True':'False')."</p>\n"; }
                                if (isset($times[$u]))
                                {
***************
*** 181,184 ****
--- 192,197 ----
                                                else
                                                {
+                                                       if ($t == '*') $t = 
'*/1';
+ 
                                                        list($one,$inc) = $arr 
= explode('/',$t);
                                                        
***************
*** 215,219 ****
                                        }
                                }
!                               elseif ($n < $last_set)         // => empty 
gets enumerated
                                {
                                        for ($i = $min_unit[$u]; $i <= 
$max_unit[$u]; ++$i)
--- 228,232 ----
                                        }
                                }
!                               elseif ($n < $last_set || $u == 'dow')  // 
before last value set (or dow) => empty gets enumerated
                                {
                                        for ($i = $min_unit[$u]; $i <= 
$max_unit[$u]; ++$i)
***************
*** 222,231 ****
                                        }
                                }
!                               else                                            
// => empty is min-value
                                {
                                        $times[$u][$min_unit[$u]] = True;
                                }
                        }
!                       //echo "enumerated times=<pre>"; print_r($times); echo 
"</pre>\n";
                        
                        // now we have the times enumerated, lets find the 
first not expired one
--- 235,244 ----
                                        }
                                }
!                               else    // => after last value set => empty is 
min-value
                                {
                                        $times[$u][$min_unit[$u]] = True;
                                }
                        }
!                       if ($this->debug) { echo "enumerated times=<pre>"; 
print_r($times); echo "</pre>\n"; }
                        
                        // now we have the times enumerated, lets find the 
first not expired one
***************
*** 234,241 ****
                        while (!isset($found['min']))
                        {
-                               $found = array();
                                $future = False;
  
-                               $n = 0;
                                foreach($units as $u => $date_pattern)
                                {
--- 247,252 ----
***************
*** 243,246 ****
--- 254,263 ----
                                                
intval(date($date_pattern,mktime(12,0,0,$found['month'],$found['day'],$found['year'])));
  
+                                       if (isset($found[$u]))
+                                       {
+                                               $future = $future || $found[$u] 
> $unit_now;
+                                               if ($this->debug) echo "--> 
already have a $u = ".$found[$u].", future='$future'<br>\n";
+                                               continue;       // already set
+                                       }
                                        foreach($times[$u] as $unit_value => 
$nul)
                                        {
***************
*** 268,286 ****
                                        {
                                                $next = array_keys($units);
!                                               
!                                               if (!isset($next[$n-1]))
                                                {
!                                                       //echo "<p>Nothing 
found, exiting !!!</p>\n";
                                                        return False;           
                                        
                                                }
!                                               $next = $next[$n-1];
                                                $over = $found[$next];
!                                               //echo "<p>Have to try the next 
$next, $u's are over for $next=$over !!!</p>\n";
                                                break;
                                        }
-                                       $n++;
                                }
                        }
!                       //echo "<p>next="; print_r($found); echo "</p>\n";
                        return 
mktime($found['hour'],$found['min'],0,$found['month'],$found['day'],$found['year']);
                }
--- 285,303 ----
                                        {
                                                $next = array_keys($units);
!                                               if 
(!isset($next[count($found)-1]))
                                                {
!                                                       if ($this->debug) echo 
"<p>Nothing found, exiting !!!</p>\n";
                                                        return False;           
                                        
                                                }
!                                               $next = $next[count($found)-1];
                                                $over = $found[$next];
!                                               unset($found[$next]);
!                                               if ($this->debug) echo "<p>Have 
to try the next $next, $u's are over for $next=$over !!!</p>\n";
                                                break;
                                        }
                                }
                        }
!                       if ($this->debug) { echo "<p>next="; print_r($found); 
echo "</p>\n"; }
! 
                        return 
mktime($found['hour'],$found['min'],0,$found['month'],$found['day'],$found['year']);
                }
***************
*** 299,325 ****
  
                /*!
                @function check_run
                @abstract checks if there are any jobs ready to run (timer 
expired) and executes them
                */
!               function check_run()
                {
!                       if (!($jobs = $this->read()))
                        {
!                               return False;
                        }
!                       foreach($jobs as $id => $job)
                        {
!                               ExecMethod($job['method'],$job['data']);
!                               
!                               if ($job['next'] = 
$this->next_run($job['times']))
                                {
!                                       $this->write($job,True);
!                               }
!                               else    // no further runs
!                               {
!                                       $this->delete($job['id']);
                                }
                        }
!                       return count($jobs);
                }
  
--- 316,414 ----
  
                /*!
+               @function last_check_run
+               @abstract checks when the last check_run was run or set the 
run-semaphore if $semaphore == True 
+               @param $semaphore if False only check, if true try to 
set/release the semaphore
+               @param $release if $semaphore == True, tells if we should set 
or release the semaphore
+               @returns if !$set array('start' => $start,'end' => $end) with 
timestamps of last check_run start and end,  \
+                       !$end means check_run is just running. If $set returns 
True if it was able to get the semaphore, else False
+               */
+               function 
last_check_run($semaphore=False,$release=False,$run_by='')
+               {
+                       //echo 
"<p>last_check_run(semaphore=".($semaphore?'True':'False').",release=".($release?'True':'False').")</p>\n";
+                       if ($semaphore)
+                       {
+                               $this->db->lock($this->db_table,'write');       
// this will block til we get exclusive access to the table
+ 
+                               set_time_limit(0);              // dont stop 
for an execution-time-limit
+                               ignore_user_abort(True);
+                       }
+                       if ($exists = $this->read('##last-check-run##'))
+                       {
+                               list(,$last_run) = each($exists);
+                       }
+                       //echo "last_run (from db)=<pre>"; print_r($last_run); 
echo "</pre>\n";
+ 
+                       if (!$semaphore)
+                       {
+                               return $last_run['data'];
+                       }
+                       elseif (!$release && !$last_run['data']['end'] && 
$last_run['data']['start'] > time()-600)
+                       {
+                               // already one instance running (started not 
more then 10min ago, else we ignore it)
+ 
+                               $this->db->unlock();    // unlock the table 
again
+ 
+                               //echo "<p>An other instance is running 
!!!</p>\n";
+                               return False;
+                       }
+                       // no other instance runs ==> we should run
+                       //
+                       if ($release)
+                       {
+                               $last_run['data']['end'] = time();
+                       }
+                       else
+                       {
+                               $last_run = array(
+                                       'id'     => '##last-check-run##',
+                                       'next'   => 0,
+                                       'times'  => array(),
+                                       'method' => 'none',
+                                       'data'   => array(
+                                               'run_by'=> $run_by,
+                                               'start' => time(),
+                                               'end'   => 0
+                                       )
+                               );
+                       }
+                       //echo "last_run=<pre>"; print_r($last_run); echo 
"</pre>\n";
+                       $this->write($last_run,!!$exits);
+                       
+                       $this->db->unlock();
+ 
+                       return True;
+               }
+ 
+               /*!
                @function check_run
                @abstract checks if there are any jobs ready to run (timer 
expired) and executes them
                */
!               function check_run($run_by='')
                {
!                       flush();
! 
!                       if (!$this->last_check_run(True,False,$run_by))
                        {
!                               return False;   // cant obtain semaphore
                        }
!                       if ($jobs = $this->read())
                        {
!                               foreach($jobs as $id => $job)
                                {
!                                       ExecMethod($job['method'],$job['data']);
! 
!                                       if ($job['next'] = 
$this->next_run($job['times']))
!                                       {
!                                               $this->write($job,True);
!                                       }
!                                       else    // no further runs
!                                       {
!                                               $this->delete($job['id']);
!                                       }
                                }
                        }
!                       $this->last_check_run(True,True,$run_by);       // 
release semaphore
! 
!                       return $jobs ? count($jobs) : False;
                }
  
***************
*** 337,345 ****
                        if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
                        {
!                               $where = "id LIKE '$id'";
                        }
                        elseif (!$id)
                        {
!                               $where = 'next<='.time();
                        }
                        else
--- 426,434 ----
                        if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
                        {
!                               $where = "id LIKE '$id' AND 
id!='##last-check-run##'";
                        }
                        elseif (!$id)
                        {
!                               $where = 'next<='.time()." AND 
id!='##last-check-run##'";
                        }
                        else
***************
*** 361,364 ****
--- 450,454 ----
                                        'data'   => 
unserialize($this->db->f('data'))
                                );
+                               //echo "job id='$id'<pre>"; 
print_r($jobs[$id]); echo "</pre>\n";
                        }
                        if (!count($jobs))
***************
*** 380,393 ****
                        $job['times'] = 
$this->db->db_addslashes(serialize($job['times']));
                        $job['data']  = 
$this->db->db_addslashes(serialize($job['data']));
  
                        if ($exists || $this->read($job['id']))
                        {
                                $this->db->query("UPDATE $this->db_table SET 
next=$job[next],times='$job[times]',".
!                                       
"method='$job[method]',data='$job[data]' WHERE 
id='$job[id]'",__FILE__,__LINE__);
                        }
                        else
                        {
                                $this->db->query("INSERT INTO $this->db_table 
(id,next,times,method,data) VALUES ".
!                                       
"('$job[id]',$job[next],'$job[times]','$job[method]','$job[data]')",__FILE__,__LINE__);
                        }
                }
--- 470,484 ----
                        $job['times'] = 
$this->db->db_addslashes(serialize($job['times']));
                        $job['data']  = 
$this->db->db_addslashes(serialize($job['data']));
+                       $job['next']  = intval($job['next']);
  
                        if ($exists || $this->read($job['id']))
                        {
                                $this->db->query("UPDATE $this->db_table SET 
next=$job[next],times='$job[times]',".
!                                       
"method='$job[method]',data='$job[data]' WHERE 
id='$job[id]'",__LINE__,__FILE__);
                        }
                        else
                        {
                                $this->db->query("INSERT INTO $this->db_table 
(id,next,times,method,data) VALUES ".
!                                       
"('$job[id]',$job[next],'$job[times]','$job[method]','$job[data]')",__LINE__,__FILE__);
                        }
                }
***************
*** 436,439 ****
--- 527,534 ----
                                                }
                                        }
+                                       if (!is_executable($this->$name))
+                                       {
+                                               $this->$name = $name;   // 
hopefully its in the path
+                                       }
                                        //echo "<p>$name = 
'".$this->$name."'</p>\n";
                                }
***************
*** 446,477 ****
                @abstract checks if phpgwapi/cron/asyncservices.php is 
installed as cron-job
                @syntax installed()
!               @returns the times asyncservices are run (normaly 'min'=>'* 
/5') or False if not installed
!               @note Not implemented for Windows at the moment, always returns 
False
                */
                function installed()
                {
!                       if (substr(php_uname(), 0, 7) == "Windows") {
!                               False;
                        }
                        $this->find_binarys();
! 
                        $times = False;
                        if (($crontab = popen('/bin/sh -c "'.$this->crontab.' 
-l" 2>&1','r')) !== False)
                        {
                                while ($line = fgets($crontab,256))
                                {
!                                       if ($line[0] != '#' && 
strstr($line,'asyncservices.php'))
                                        {
-                                               $time = explode(' ',$line);
                                                $cron_units = 
array('min','hour','day','month','dow');
                                                foreach($cron_units as $n => $u)
                                                {
!                                                       if ($time[$n] != '*')
!                                                       {
!                                                               $times[$u] = 
$time[$n];
!                                                       }
                                                }
                                                $times['cronline'] = $line;
!                                               break;
                                        }
                                }
--- 541,588 ----
                @abstract checks if phpgwapi/cron/asyncservices.php is 
installed as cron-job
                @syntax installed()
!               @returns the times asyncservices are run (normaly 'min'=>'* 
/5') or False if not installed or 0 if crontab not found
!               @note Not implemented for Windows at the moment, always returns 0
                */
                function installed()
                {
!                       if ($this->only_fallback) {
!                               return 0;
                        }
                        $this->find_binarys();
!                       
!                       if (!is_executable($this->crontab))
!                       {
!                               //echo "<p>Error: $this->crontab not found 
!!!</p>";
!                               return 0;
!                       }
                        $times = False;
+                       $this->other_cronlines = array();
                        if (($crontab = popen('/bin/sh -c "'.$this->crontab.' 
-l" 2>&1','r')) !== False)
                        {
                                while ($line = fgets($crontab,256))
                                {
!                                       if ($this->debug) echo 'line '.++$n.": 
$line<br>\n";
!                                       $parts = split(' ',$line,6);
! 
!                                       if ($line[0] == '#' || count($parts) < 
6 || $parts[5][0] != '/')
!                                       {
!                                               // ignore comments
!                                               if ($line[0] != '#')
!                                               {
!                                                       $times['error'] .= 
$line;
!                                               }
!                                       } 
!                                       elseif (strstr($line,$this->cronline) 
!== False)
                                        {
                                                $cron_units = 
array('min','hour','day','month','dow');
                                                foreach($cron_units as $n => $u)
                                                {
!                                                       $times[$u] = $parts[$n];
                                                }
                                                $times['cronline'] = $line;
!                                       }
!                                       else
!                                       {
!                                               $this->other_cronlines[] = 
$line;
                                        }
                                }
***************
*** 486,498 ****
                @syntax install($times)
                @param $times array with keys 'min','hour','day','month','dow', 
not set is equal to '*'
!               @returns the times asyncservices are run or False if they are 
not installed
!               @note Not implemented for Windows at the moment, always dies 
with an error-message
                */
                function install($times)
                {
!                       if (substr(php_uname(), 0, 7) == "Windows") {
!                               die ("Sorry, no automatic on Windows at the 
moment !!!\n");
                        }
!                       $this->find_binarys();
  
                        if (($crontab = popen('/bin/sh -c "'.$this->crontab.' 
-" 2>&1','w')) !== False)
--- 597,609 ----
                @syntax install($times)
                @param $times array with keys 'min','hour','day','month','dow', 
not set is equal to '*'
!               @returns the times asyncservices are run or False if they are 
not installed or 0 if crontab not found
!               @note Not implemented for Windows at the moment, always returns 0
                */
                function install($times)
                {
!                       if ($this->only_fallback) {
!                               return 0;
                        }
!                       $this->installed();     // find other installed 
cronlines
  
                        if (($crontab = popen('/bin/sh -c "'.$this->crontab.' 
-" 2>&1','w')) !== False)
***************
*** 503,622 ****
                                        $cronline .= (isset($times[$cu]) ? 
$times[$cu] : '*') . ' ';
                                }
!                               $cronline .= $this->php.' -q 
'.PHPGW_SERVER_ROOT . '/phpgwapi/cron/asyncservices.php'."\n";
                                //echo "<p>Installing: '$cronline'</p>\n";
                                fwrite($crontab,$cronline);
                                @pclose($crontab);
                        }
-                       else
-                       {
-                               //echo "<p>Error: /usr/bin/crontab not found 
!!!</p>";
-                               return False;
-                       }
                        return $this->installed();
                }
-               
-               function test($data)
-               {
-                       echo "asyncservice::test: data =\n";
- 
-                       print_r($data);
-               }
-       }
- 
- if (!isset($GLOBALS['phpgw_info']))
- {     
-       $GLOBALS['phpgw_info']['flags'] = array(
-               'currentapp' => 'login'
-       );
-       include('../../header.inc.php');
- 
-       $async = new asyncservice;
-       $units = array(
-               'year'  => 'Year',
-               'month' => 'Month',
-               'day'   => 'Day',
-               'dow'   => 'Day of week',
-               'hour'  => 'Hour',
-               'min'   => 'Minute'
-       );
-       
-       if ($_POST['send'] || $_POST['test'] || $_POST['cancel'] || 
$_POST['install'])
-       {
-               $times = array();
-               foreach($units as $u => $ulabel)
-               {
-                       if (!empty($_POST[$u]))
-                       {
-                               $times[$u] = $_POST[$u];
-                       }
-               }
-               $next = $async->next_run($times,True);
-               
-               echo "<p>async::next_run(";print_r($times);echo")=".($next === 
False ? 'False':"'$next'=".date('D(w) d.m.Y H:i',$next))."</p>\n";
-               
-               if ($_POST['test'])
-               {
-                       if 
(!$async->set_timer($times,'test','phpgwapi.asyncservice.test','Hello 
World!!!'))
-                       {
-                               echo "<p>Error setting timer, maybe there's one 
already running !!!</p>\n";
-                       }
-               }
-               if ($_POST['cancel'])
-               {
-                       if (!$async->cancel_timer('test'))
-                       {
-                               echo "<p>Error canceling timer, maybe there's 
none set !!!</p>\n";
-                       }
-               }
-               if ($_POST['install'])
-               {
-                       if ($install = $async->install($times))
-                       {
-                               echo "<p>Installing: 
'$install[cronline]'</p>\n";
-                       }
-                       else
-                       {
-                               echo "<p>Error: $async->crontab not found or 
other error !!!</p>";
-                       }
-               }
-       }
-       echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
-       foreach ($units as $u => $ulabel)
-       {
-               echo "$ulabel: <input name=\"$u\" value=\"$times[$u]\" size=5> 
&nbsp;\n";
-       }
-       echo "<input type=\"submit\" name=\"send\" value=\"Calculate next 
run\">\n";
-       echo "<input type=\"submit\" name=\"test\" value=\"Start TestJob!\">\n";
-       echo "<input type=\"submit\" name=\"cancel\" value=\"Cancel 
TestJob!\">\n";
-       echo "<p><b>crontab:</b> \n";
- 
-       if ($installed = $async->installed())
-       {
-               echo "$installed[cronline]</p>";
-       }
-       else
-       {
-               echo "$async->crontab not found or asyncservices not installed 
!!!</p>";
-       }
-       echo "<input type=\"submit\" name=\"install\" value=\"Install 
crontab\">\n";
-       
-       echo "<p><b>jobs:</b></p>\n";
-       if ($jobs = $async->read('%'))
-       {
-               echo "<table border=1>\n<tr>\n<th>Id</th><th>Next 
run</th><th>Times</th><th>Method</th><th>Data</th></tr>\n";
-               foreach($jobs as $job)
-               {
-                       echo "<tr>\n<td>$job[id]</td><td>".date('Y/m/d 
H:i',$job['next'])."</td><td>";
-                       print_r($job['times']); 
-                       echo "</td><td>$job[method]</td><td>"; 
-                       print_r($job['data']); 
-                       echo "</td></tr>\n"; 
-               }
-               echo "</table>\n";
-       }
-       else
-       {
-               echo "<p>No jobs in the database !!!</p>\n";
        }
-       echo "</form>\n";
- }
--- 614,628 ----
                                        $cronline .= (isset($times[$cu]) ? 
$times[$cu] : '*') . ' ';
                                }
!                               $cronline .= $this->php.' -q 
'.$this->cronline."\n";
                                //echo "<p>Installing: '$cronline'</p>\n";
                                fwrite($crontab,$cronline);
+                               
+                               foreach ($this->other_cronlines as $cronline)
+                               {
+                                       fwrite($crontab,$cronline);             
// preserv the other lines
+                               }
                                @pclose($crontab);
                        }
                        return $this->installed();
                }
        }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]