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.1


From: Ralf Becker <address@hidden>
Subject: [Phpgroupware-cvs] CVS: phpgwapi/inc class.asyncservice.inc.php,1.1.2.1,1.1.2.2
Date: Sat, 26 Apr 2003 12:39:04 -0400

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

Modified Files:
      Tag: Version-0_9_16-branch
        class.asyncservice.inc.php 
Log Message:
added dow, install and installed functions and debug function in the class


Index: class.asyncservice.inc.php
===================================================================
RCS file: /cvsroot/phpgroupware/phpgwapi/inc/Attic/class.asyncservice.inc.php,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** class.asyncservice.inc.php  22 Apr 2003 00:40:20 -0000      1.1.2.1
--- class.asyncservice.inc.php  26 Apr 2003 16:39:02 -0000      1.1.2.2
***************
*** 29,34 ****
--- 29,39 ----
                        'check_run' => True,
                        'cancel_timer' => True,
+                       'read'      => True,
+                       'install'   => True,
+                       'installed' => True,
                        'test'      => True
                );
+               var $php = '';
+               var $crontab = '';
                var $db;
                var $db_table = 'phpgw_async';
***************
*** 43,47 ****
                @abstract calculates the next run of the timer and puts that 
with the rest of the data in the db
                @syntax set_timer($times,$id,$method,$data)
!               @param $times unix timestamp or 
array('min','hour','day','month','year') with execution time. \
                        Repeated events are possible to shedule by setting the 
array only partly, eg. \
                        array('day' => 1) for first day in each month 0am or 
array('min' => '* /5', 'hour' => '9-17') \
--- 48,52 ----
                @abstract calculates the next run of the timer and puts that 
with the rest of the data in the db
                @syntax set_timer($times,$id,$method,$data)
!               @param $times unix timestamp or 
array('min','hour','dow','day','month','year') with execution time. \
                        Repeated events are possible to shedule by setting the 
array only partly, eg. \
                        array('day' => 1) for first day in each month 0am or 
array('min' => '* /5', 'hour' => '9-17') \
***************
*** 78,82 ****
                @abstract calculates the next execution time for $time
                @syntax next_run($time)
!               @param $times unix timestamp or 
array('year'=>$year,'month'=>$month,'day'=>$day,'hour'=>$hour,'min'=>$min) \
                        with execution time. Repeated execution is possible to 
shedule by setting the array only partly, \
                        eg. array('day' => 1) for first day in each month 0am 
or array('min' => '/5', 'hour' => '9-17') \
--- 83,87 ----
                @abstract calculates the next execution time for $time
                @syntax next_run($time)
!               @param $times unix timestamp or 
array('year'=>$year,'month'=>$month,'dow'=>$dow,'day'=>$day,'hour'=>$hour,'min'=>$min)
 \
                        with execution time. Repeated execution is possible to 
shedule by setting the array only partly, \
                        eg. array('day' => 1) for first day in each month 0am 
or array('min' => '/5', 'hour' => '9-17') \
***************
*** 103,106 ****
--- 108,112 ----
                                'month' => 'm',
                                'day'   => 'd',
+                               'dow'   => 'w',
                                'hour'  => 'H',
                                'min'   => 'i'
***************
*** 109,112 ****
--- 115,119 ----
                                'min'   => 59,
                                'hour'  => 23,
+                               'dow'   => 6,
                                'day'   => 31,
                                'month' => 12,
***************
*** 116,119 ****
--- 123,127 ----
                                'min'   => 0,
                                'hour'  => 0,
+                               'dow'   => 0,
                                'day'   => 1,
                                'month' => 1,
***************
*** 156,160 ****
                                        foreach($time as $t)
                                        {
!                                               if (strstr($t,'-') !== False)
                                                {
                                                        list($min,$max) = $arr 
= explode('-',$t);
--- 164,168 ----
                                        foreach($time as $t)
                                        {
!                                               if (strstr($t,'-') !== False && 
strstr($t,'/') === False)
                                                {
                                                        list($min,$max) = $arr 
= explode('-',$t);
***************
*** 178,182 ****
                                                              count($arr) == 2 
&& is_numeric($inc)))
                                                        {
!                                                               if ($debug) 
echo "<p>Syntax error in $u='$t', allowed is a number or '*/inc', 
inc='$inc'</p>\n";
  
                                                                return False;
--- 186,190 ----
                                                              count($arr) == 2 
&& is_numeric($inc)))
                                                        {
!                                                               if ($debug) 
echo "<p>Syntax error in $u='$t', allowed is a number or '{*|range}/inc', 
inc='$inc'</p>\n";
  
                                                                return False;
***************
*** 188,192 ****
                                                        else
                                                        {
!                                                               for ($i = 
$min_unit[$u]; $i <= $max_unit[$u]; $i += $inc)
                                                                {
                                                                        
$times[$u][$i] = True;
--- 196,211 ----
                                                        else
                                                        {
!                                                               list($min,$max) 
= $arr = explode('-',$one);
!                                                               if (empty($one) 
|| $one == '*')
!                                                               {
!                                                                       $min = 
$min_unit[$u];
!                                                                       $max = 
$max_unit[$u];
!                                                               }
!                                                               elseif 
(count($arr) != 2 || $min > $max)
!                                                               {
!                                                                       if 
($debug) echo "<p>Syntax error in $u='$t', allowed is '{*|min-max}/inc', 
min='$min',max='$max', inc='$inc'</p>\n";
!                                                                       return 
False;
!                                                               }
!                                                               for ($i = $min; 
$i <= $max; $i += $inc)
                                                                {
                                                                        
$times[$u][$i] = True;
***************
*** 221,230 ****
                                foreach($units as $u => $date_pattern)
                                {
!                                       $unit_now = intval(date($date_pattern));
  
                                        foreach($times[$u] as $unit_value => 
$nul)
                                        {
!                                               if (($future || $unit_value >= 
$unit_now) && // not yet expired and
!                                                   ($u != $next || $unit_value 
> $over))        // not over
                                                {
                                                        $found[$u] = 
$unit_value;
--- 240,262 ----
                                foreach($units as $u => $date_pattern)
                                {
!                                       $unit_now = $u != 'dow' ? 
intval(date($date_pattern)) :
!                                               
intval(date($date_pattern,mktime(12,0,0,$found['month'],$found['day'],$found['year'])));
  
                                        foreach($times[$u] as $unit_value => 
$nul)
                                        {
!                                               switch($u)
!                                               {
!                                                       case 'dow':
!                                                               $valid = 
$unit_value == $unit_now;
!                                                               break;
!                                                       case 'min':
!                                                               $valid = 
$future || $unit_value > $unit_now;
!                                                               break;
!                                                       default:
!                                                               $valid = 
$future || $unit_value >= $unit_now;
!                                                               break;
!                                                       
!                                               }
!                                               if ($valid && ($u != $next || 
$unit_value > $over))      // valid and not over
                                                {
                                                        $found[$u] = 
$unit_value;
***************
*** 294,305 ****
                /*!
                @function read
!               @abstract reads db-row / job with $id or if $id=0 reads all 
expired rows / jobs ready to run
!               @returns db-rows / jobs as array or False if $id not found or 
no expired rows
                */
                function read($id=0)
                {
!                       $query = "SELECT * FROM $this->db_table WHERE ".($id ? 
"id='$id'" : 'next<='.time());
!                       
!                       $this->db->query($query,__LINE__,__FILE__);
  
                        $jobs = array();
--- 326,351 ----
                /*!
                @function read
!               @abstract reads all matching db-rows / jobs
!               @syntax reay($id=0)
!               @param $id =0 reads all expired rows / jobs ready to run\
!                       != 0 reads all rows/jobs matching $id (sql-wildcards 
'%' and '_' can be used)
!               @returns db-rows / jobs as array or False if no matches
                */
                function read($id=0)
                {
!                       $id = $this->db->db_addslashes($id);
!                       if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
!                       {
!                               $where = "id LIKE '$id'";
!                       }
!                       elseif (!$id)
!                       {
!                               $where = 'next<='.time();
!                       }
!                       else
!                       {
!                               $where = "id='$id'";
!                       }
!                       $this->db->query($sql="SELECT * FROM $this->db_table 
WHERE $where",__LINE__,__FILE__);
  
                        $jobs = array();
***************
*** 359,362 ****
--- 405,519 ----
                }
                
+               function find_binarys()
+               {
+                       static $run = False;
+                       if ($run)
+                       {
+                               return;
+                       }
+                       $run = True;
+ 
+                       if (substr(php_uname(), 0, 7) == "Windows") 
+                       {
+                               // ToDo: find php-cgi on windows
+                       }
+                       else
+                       {
+                               $binarys = array('php' => 
'/usr/bin/php','crontab' => '/usr/bin/crontab');
+                               foreach ($binarys as $name => $path)
+                               {
+                                       $this->$name = $path;   // a reasonable 
default for *nix
+                                       if (!is_executable($this->$name))
+                                       {
+                                               if ($fd = popen('/bin/sh -c 
"which '.$name.'"','r'))
+                                               {
+                                                       $this->$name = 
fgets($fd,256);
+                                                       @pclose($fd);
+                                               }
+                                               if ($pos = 
strpos($this->$name,"\n"))
+                                               {
+                                                       $this->$name = 
substr($this->$name,0,$pos);
+                                               }
+                                       }
+                                       //echo "<p>$name = 
'".$this->$name."'</p>\n";
+                               }
+                       }
+               
+               }
+               
+               /*!
+               @function installed
+               @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;
+                                       }
+                               }
+                               @pclose($crontab);
+                       }
+                       return $times;
+               }
+               
+               /*!
+               @function insall
+               @abstract installs /phpgwapi/cron/asyncservices.php as cron-job
+               @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)
+                       {
+                               $cron_units = 
array('min','hour','day','month','dow');
+                               foreach($cron_units as $cu)
+                               {
+                                       $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)
                {
***************
*** 375,384 ****
  
        $async = new asyncservice;
!       $units = array('year','month','day','hour','min');
        
!       if ($_POST['send'] || $_POST['test'] || $_POST['cancel'])
        {
                $times = array();
!               foreach($units as $u)
                {
                        if (!empty($_POST[$u]))
--- 532,548 ----
  
        $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]))
***************
*** 389,393 ****
                $next = $async->next_run($times,True);
                
!               echo "<p>async::next_run(";print_r($times);echo")=".($next === 
False ? 'False':"'$next'=".date('d.m.Y H:i',$next))."</p>\n";
                
                if ($_POST['test'])
--- 553,557 ----
                $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'])
***************
*** 405,417 ****
                        }
                }
        }
        echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'."\n";
!       foreach ($units as $u)
        {
!               echo "$u: <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 "</form>";
! }
\ No newline at end of file
--- 569,622 ----
                        }
                }
+               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";
! }





reply via email to

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