Пример #1
0
 public function onReceive(swoole_server $serv, $fd, $from_id, $data)
 {
     echo $serv->worker_pid . PHP_EOL;
     $case = intval($data);
     switch ($case) {
         case 1:
             swoole_async_readfile(__DIR__ . "/Test.txt", function ($filename, $content) {
                 echo "{$filename}: {$content}";
             });
             break;
         case 2:
             swoole_async_writefile('test_2.log', "This is a test log", function ($filename) {
                 echo "wirte ok.\n";
             });
             break;
         case 3:
             swoole_async_read(__DIR__ . "/Test.txt", function ($filename, $content) {
                 if (empty($content)) {
                     return false;
                 } else {
                     echo "{$filename}: {$content}";
                     return true;
                 }
             }, 16);
             break;
         default:
             // 注:此处存在一个Bug,如果文件不存在并且没有指定offset(或者指定为-1),会引发一个错误。这里需要注意一下。
             swoole_async_write('test_1.log', "This is a test log\n", -1, function ($filename, $writen) {
                 var_dump(func_get_args());
             });
             break;
     }
 }
 /**
  * write asynchronously to file
  * @param array $record
  * @throws \UnexpectedValueException
  */
 protected function write(array $record)
 {
     if (!is_dir($this->logDir)) {
         $this->createDir();
     }
     $filename = $this->logDir . '/' . date('Y-m-d') . '.log';
     swoole_async_write($filename, (string) $record['formatted'], -1);
 }
 public function export()
 {
     $text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
     $self = $this;
     if ($this->enableRotation) {
         // clear stalt cache to ensure getting the real current file size and not a cached one
         // this may result in rotating twice when cached file size is used on subsequent calls
         clearstatcache();
     }
     if ($this->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {
         $this->rotateFiles();
     }
     swoole_async_write($this->logFile, $text, -1, function ($filename) use($self) {
         if ($self->fileMode !== null) {
             @chmod($self->logFile, $self->fileMode);
         }
     });
 }
Пример #4
0
 /**
  * 异步写日志
  * @param $data
  * @param string $filename
  * @return bool
  * @throws Exception
  */
 static function info($data, $filename = "log.log")
 {
     $configObj = config("config");
     $path = $configObj->get("filelogpath");
     if (!$path) {
         throw new Exception("filelogpath not config");
     }
     $path = $path . "/" . KERISY_ENV . "/" . date('Y-m-d') . "/";
     if (!is_dir($path)) {
         mkdir($path, 0777, true);
     }
     $filePath = $path . $filename;
     static $pid;
     $data = is_string($data) ? $data : json_encode($data);
     !$pid && ($pid = function_exists('posix_getpid') ? posix_getpid() : mt_rand(1, 9999));
     $ip = self::get_server_ip();
     $time = \Kerisy\Tool\RunTime::runtime();
     $newData = "[" . $ip . "]" . "[" . $pid . "]" . "[" . date('Y-m-d H:i:s') . "][cost:" . $time . "]" . $data;
     swoole_async_write($filePath, $newData . "\r\n");
     return true;
 }
<?php

date_default_timezone_set('Asia/Shanghai');
$serv = new swoole_server('0.0.0.0', 9094, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$serv->set(['worker_num' => 1, 'task_worker_num' => 4, 'daemonize' => false, 'log_file' => '/tmp/swoole_udp_server.log']);
$serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
    $task_id = $serv->task($data);
    $ymdhi = date('YmdHi');
    swoole_async_write('/tmp/' . $ymdhi . '.txt', $data . "\n", -1);
});
$serv->on('task', function ($serv, $task_id, $from_id, $data) {
    var_dump($serv->taskworker, $serv->worker_id, $serv->worker_pid);
    \ran\Ran::run($serv->worker_id, $data);
    return true;
});
$serv->on('finish', function () {
});
$serv->on('start', function ($serv) {
    swoole_async_writefile('/tmp/swoole_master_pid.log', $serv->master_pid, function ($filename) {
    });
    //  swoole_set_process_name("php swoole udp server master".$serv->master_pid);
});
$serv->on('workerStart', function ($serv, $worker_id) {
    if ($worker_id >= $serv->setting['worker_num']) {
        //超过worker_num,表示这是一个task进程
        // swoole_set_process_name("php swoole udp server tasker ".$worker_id);
        echo "{$worker_id} tasker start\n";
        require './vendor/autoload.php';
    } else {
        echo "{$worker_id} worker start\n";
        // swoole_set_process_name("php swoole udp server worker ".$worker_id);
Пример #6
0
<?php

function write_callback($file, $writen)
{
    echo "write {$file} [{$writen}]\n";
    //return true: write contine. return false: close the file.
    return true;
}
for ($i = 0; $i < 10; $i++) {
    swoole_async_write("data.txt", str_repeat('A', 10) . "\n", -1, "write_callback");
}
Пример #7
0
 /**
  * @brief 写日志
  * @param string $logContent
  * @return boolean
  * @throws Exception
  */
 protected function _write($logContent)
 {
     //file_put_contents()
     if (false === swoole_async_write($this->_strLogFile, $logContent, -1)) {
         throw new Exception("log file {$this->_strLogFile} write failed!");
     }
     return true;
 }
Пример #8
0
<?php

function write_callback($file, $writen)
{
    echo "write [{$writen}]\n";
    //return true: write contine. return false: close the file.
    return true;
}
for ($i = 0; $i < 10; $i++) {
    swoole_async_write("data.txt", str_repeat('A', 10) . "\n", -1);
}
Пример #9
0
<?php

for ($i = 0; $i < 10; $i++) {
    swoole_async_write("data.txt", str_repeat('S', 8192), $i * 8192, function ($file, $writen) {
        echo "write [{$writen}]\n";
        //return true: write contine. return false: close the file.
        return true;
    });
}
Пример #10
0
<?php

for ($i = 0; $i < 10; $i++) {
    swoole_async_write("data.txt", str_repeat('A', 10) . "\n", -1, function ($file, $writen) {
        echo "write [{$writen}]\n";
        //return true: write contine. return false: close the file.
        return true;
    });
}