FRED™  3.0
FRED™: Framework for Rapid and Easy Development
External.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 /**
6  * External call wrapper.
7  */
8 class External extends Component{
9 
10  public $countPeriod = 3600; //!< Default count period (seconds).
11  public $countMax = null; //!< Default maximum number of open calls in a period (empty = no maximum).
12  public $errorPeriod = 3600; //!< Default error period (seconds).
13  public $errorMax = 10; //!< Default maximum number of errors in a period.
14  public $functions = []; //!< Function (key) specific configurations (array).
15  public $logPrio = Log::INFO; //!< Prio for call results.
16 
17  protected $_path = null;
18  protected $_ext = '.cnt';
19  protected $_garbageChance = 100;
20 
21  protected function filename($func_name,$type,$period){
22  return $this->_path . "/$func_name-$type-" . round(time() / $period) . $this->_ext;
23  }
24 
25  protected function inc($func_name,$type,$period){
26  file_put_contents($this->filename($func_name,$type,$period),chr(65 + time() % 26),FILE_APPEND);
27  }
28 
29  protected function count($func_name,$type,$period){
30  return \Rsi\File::size($this->filename($func_name,$type,$period));
31  }
32 
33  protected function garbage(){
34  $period = max($this->countPeriod,$this->errorPeriod);
35  foreach($this->functions as $function) $period = max($period,$function['countPeriod'] ?? 0,$function['errorPeriod'] ?? 0);
36  foreach(\Rsi\File::find($this->_path,[
37  \Rsi\File::FIND_FILTER_NAME . '-*' => $this->_ext,
38  \Rsi\File::FIND_FILTER_TIME . '<' => time() - $period
39  ]) as $filename => $info) \Rsi\File::unlink($filename);
40  }
41 
42  public function __call($func_name,$params){
43  $config = $this->functions[$func_name] ?? [];
44  $count_period = $config['countPeriod'] ?? $this->countPeriod;
45  $error_period = $config['errorPeriod'] ?? $this->errorPeriod;
46  $callback = array_shift($params);
47  $result = $params ? array_shift($params) : false;
48 
49  if(
50  !$this->_path ||
51  ($this->count($func_name,'error',$error_period) > ($config['errorMax'] ?? $this->errorMax)) || //too many errors
52  (
53  ($count_max = $config['countMax'] ?? $this->countMax) &&
54  (($this->count($func_name,'start',$count_period) - $this->count($func_name,'stop',$count_period)) > $count_max) //too many open calls
55  )
56  ) return $result;
57 
58  if(!rand(0,$this->_garbageChance)) $this->garbage();
59  $log = $this->component('log');
60  $id = $log->start(Log::DEBUG,$message = "External call to '$func_name'",__FILE__,__LINE__,$config);
61  $this->inc($func_name,'start',$count_period);
62  $retry = $config['retryCount'] ?? 0;
63  do{
64  $time = microtime(true);
65  $error = null;
66  try{
67  $result = call_user_func($callback);
68  $time = microtime(true) - $time;
69  $retry = 0;
70  }
71  catch(\Exception $e){
72  $time = microtime(true) - $time;
73  $this->inc($func_name,'error',$error_period);
74  $log->warning($e);
75  $error = $e->getMessage() . ' (' . $e->getFile() . ':' . $e->getLine() . ')';
76  if($retry > 0) usleep(($config['retryInterval'] ?? 100) * 1000);
77  }
78  $log->add($this->logPrio,$message,__FILE__,__LINE__,['funcName' => $func_name,'time' => $time,'error' => $error]);
79  }
80  while($retry-- > 0);
81  $log->stop($id);
82  $this->inc($func_name,'stop',$count_period);
83  return $result;
84  }
85 
86 }
Rsi\Fred\External\$functions
$functions
Function (key) specific configurations (array).
Definition: External.php:14
Rsi\Fred\External\$logPrio
$logPrio
Prio for call results.
Definition: External.php:15
Rsi\Fred\External\__call
__call($func_name, $params)
Definition: External.php:42
Rsi
Rsi\Fred\External\$countPeriod
$countPeriod
Default count period (seconds).
Definition: External.php:10
Rsi\Fred\External\inc
inc($func_name, $type, $period)
Definition: External.php:25
Rsi\Fred\External\$errorPeriod
$errorPeriod
Default error period (seconds).
Definition: External.php:12
Rsi\Fred\Log\INFO
const INFO
Informational message.
Definition: Log.php:14
Rsi\Fred\External\$errorMax
$errorMax
Default maximum number of errors in a period.
Definition: External.php:13
Rsi\Fred\External\$countMax
$countMax
Default maximum number of open calls in a period (empty = no maximum).
Definition: External.php:11
Rsi\Fred\Component
Basic component class.
Definition: Component.php:8
Rsi\Fred\File\find
find($dir, $dirs, $time=null)
Find archive files in a directory.
Definition: File.php:99
Rsi\Fred\External\filename
filename($func_name, $type, $period)
Definition: External.php:21
Rsi\Fred\Component\component
component($name)
Get a component (local or default).
Definition: Component.php:81
Rsi\Fred\External\$_path
$_path
Definition: External.php:17
Rsi\Fred\External\count
count($func_name, $type, $period)
Definition: External.php:29
Rsi\Fred\External\$_garbageChance
$_garbageChance
Definition: External.php:19
Rsi\Fred\External
External call wrapper.
Definition: External.php:8
Rsi\Fred\External\garbage
garbage()
Definition: External.php:33
Rsi\Fred\External\$_ext
$_ext
Definition: External.php:18
Rsi\Fred\Log\DEBUG
const DEBUG
Debug-level message.
Definition: Log.php:15
Rsi\Fred\Exception
Definition: Exception.php:5
Rsi\Fred
Definition: Alive.php:3