每分钟间隔触发x动作的最有效方法是什么?

时间:2012-11-30 18:10:38

标签: php timer intervals

想象一下,每分钟拨打2次以上的呼叫中心不会被客户淹没。因此,该范围之外的任何人都会收到电子邮件支持链接($ bTrigger = FALSE)。其他人($ bTrigger = TRUE)将获得技术支持电话号码。

脚本是PHP。那么,构建它的最有效和最准确的方法是什么?

这是我到目前为止所做的,但遗憾的是它每分钟只触发一次。我似乎无法弄清楚为什么它不会每分钟运行两次。

<?php

$bTrigger = FALSE;
$sDir = dirname(__FILE__);
$sDir = rtrim($sDir,'/');
$sFile = $sDir . '/MINUTE-TIMER.txt';

$sLine = @ file_get_contents($sFile);
$sLine = str_replace("\r\n",'',$sLine);
$sLine = str_replace("\r",'',$sLine);
$sLine = str_replace("\n",'',$sLine);
$sLine = str_replace("\t",'',$sLine);
$asParts = explode(',',$sLine);
$nLetThru = @ $asParts[0];
$nLetThru = intval($nLetThru);
$nLastMin = @ $asParts[1];
$nLastMin = intval($nLastMin);
$nCurMin = intval(date('i'));
if (empty($sLine)) {
  $nLetThru = 0;
  $nLastMin = 0;
}

$nMaxLetThru = 2;

if ($nCurMin != $nLastMin) { // meaning, a new minute since last checked
  if ($nLetThru <= $nMaxLetThru) { // meaning, we haven't hit more than max allowed
    $bTrigger = TRUE;
    ++$nLetThru;
    file_put_contents($sFile,"$nLetThru,$nCurMin");
  } else {
    file_put_contents($sFile,"0,$nCurMin");
  }
}

if ($bTrigger) {
  echo 'TRIGGERED!!!!';
} else {
  echo 'not triggered';
}

2 个答案:

答案 0 :(得分:2)

问题是一个简单的编码错误:当分钟改变时,$ nLetThru没有被重置。 (另外,你的&lt; =应该是&lt;,但你已经注意到了。)

这是固定代码(基于原始版本,在问题中):

if (empty($sLine)) {
  $nLastMin = -1; # (instead of 0) just affects the 1st time thru, 1 chance in 60
}

...

if ($nCurMin != $nLastMin) { // new minute
  $bTrigger = TRUE;
  $nLetThru = 1;
} else { // another hit, same minute
  if ($nLetThru < $nMaxLetThru) { // not too many yet
    $bTrigger = TRUE;
    ++$nLetThru;
  }
}
if ($bTrigger) {
  file_put_contents($sFile,"$nLetThru,$nCurMin");
}

答案 1 :(得分:1)

<?php

$bTrigger = TRUE;
$config = (object) array();
$config->THROTTLE_ENABLED = TRUE;
$config->THROTTLE_MAX_PER_MINUTE = 2;
if ($config->THROTTLE_ENABLED) {

    $bThrottleTrigger = FALSE;
    $sDir = dirname(__FILE__);
    $sDir = rtrim($sDir,'/');
    $sFile = $sDir . '/MINUTE-TIMER.txt';

    $sLine = @ file_get_contents($sFile);
    $sLine = str_replace("\r\n",'',$sLine);
    $sLine = str_replace("\r",'',$sLine);
    $sLine = str_replace("\n",'',$sLine);
    $sLine = str_replace("\t",'',$sLine);
    $asParts = explode(',',$sLine);
    $nLetThru = @ $asParts[0];
    $nLetThru = intval($nLetThru);
    $nLastMin = @ $asParts[1];
    $nLastMin = intval($nLastMin);
    $nCurMin = intval(date('i'));
    if (empty($sLine)) {
        $nLetThru = 0;
        $nLastMin = 0;
    }

    if ($nCurMin != $nLastMin) { // meaning, a new minute since last checked
        if ($nLetThru < $config->THROTTLE_MAX_PER_MINUTE) { // meaning, we haven't hit more than max allowed
            $bThrottleTrigger = TRUE;
            ++$nLetThru;
            @ file_put_contents($sFile,"$nLetThru,$nLastMin");
        } else {
            @ file_put_contents($sFile,"0,$nCurMin");
        }
    } else {
        @ file_put_contents($sFile,"0,$nCurMin");
    }

    if (!$bThrottleTrigger) { // will be like most of the time
        $bTrigger = FALSE; // don't show the number
    }

} // end if ($config->THROTTLE_ENABLED)