Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(framework): Remove Swoole Task Support
Browse files Browse the repository at this point in the history
1. Use Redis queue to deal with tracker announce function
2. Remove Swoole Task Support
3. Use new callback function to start swoole server.
4. Fix announce data can't current send to queue.

BREAKING CHANGE: Remove Swoole Task Support
  • Loading branch information
Rhilip committed Jun 28, 2019
1 parent ae2a359 commit 8188df9
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 173 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- **Email:** Fix Email Can't Send
- **Error:** Fix Error Handler
- **Invite:** Add Fine-grained control of recycle pending
- **Links:** Remove extra meta section
- **Register:** Add captcha checker
- **TorrentUpload:** Fix length 0 file cause ParseErrorException
- **Tracker:** Add miss port check for field ipv6_port
Expand All @@ -59,6 +60,7 @@
- **Redis:** Add more Redis arguments in debug output
- **Session:** Add Session Format Docs
- **SiteConfig:** change namespace `authority.route_` to `route.`
- **Tracker:** Separate announce data update function
- **Validator:** Add function buildDefaultValue()
- **ext2Icon:** Add more File format

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ So that tracker can record the peer's ip address.
7. Use the default `php bin/rid-httpd service start -d` to let *RidPT* RUN in the background. Or you can use other daemon work like:
- Systemctl: [ridpt.service](migration/ridpt.service)

## Basie Environment in `.env`
## Basie Environment Setting in `.env`

> Notice: Any change in file `.env` require the restart of Application to Make it effective

### Section `APP`

Expand Down
6 changes: 6 additions & 0 deletions apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
'type' => \Rid\Base\Timer::TICK,
'msec' => 5 * 60 * 1000, // TODO 单位为毫秒,应该为所有contab worker的最小公倍数(应该在面板有所提醒)
'callback' => 'init'
],
'tracker' => [
'class' => \apps\task\TrackerAnnounceTimer::class,
'type' => \Rid\Base\Timer::AFTER,
'msec' => 10 * 1000,
'callback' => 'init'
]
],

Expand Down
1 change: 0 additions & 1 deletion apps/config/httpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
'enable_coroutine' => false, // 开启协程
'reactor_num' => 1, // 连接处理线程数
'worker_num' => 5, // 工作进程数
'task_worker_num' => 10, // Task进程数
'pid_file' => '/var/run/rid-httpd.pid', // PID 文件
'log_file' => '/tmp/rid-httpd.log', // 日志文件路径
'max_request' => 3000, // 进程的最大任务数
Expand Down
10 changes: 7 additions & 3 deletions apps/controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,13 @@ private function checkSession($queries, $seeder, $userInfo, $torrentInfo)

private function sendToTaskWorker($queries, $role, $userInfo, $torrentInfo)
{
// Push to Redis Queue and quick response
app()->redis->lPush('Tracker:to_deal_queue', json_encode([
'worker' => \apps\task\TrackerAnnounceTask::class,
/**
* Push to Redis Queue and quick response
*
* Don't use json_{encode,decode} for the value of info_hash and peer_id will make
* those function return FALSE
*/
return app()->redis->lPush('Tracker:to_deal_queue', serialize([
'timestamp' => time(),
'queries' => $queries,
'role' => $role,
Expand Down
2 changes: 1 addition & 1 deletion apps/public/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ body{background-color:#f6f6f6}
.color-rss{color:#5c7cfa}

/* Main Container */
.main-container{background-color:#eaeaea;padding:10px}
.main-container{background-color:#eaeaea;padding:20px 10px}

/* Footer Menu */
#footer_menu{background-color:#383838;color:#C3C0B9;font-size:12px;margin-top:30px;padding-bottom:10px;padding-top:15px}
Expand Down
19 changes: 9 additions & 10 deletions apps/task/CronTabTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

class CronTabTimer extends Timer
{

private $_print_flag = 1; // FIXME debug model on

private function print_log($log)
{
$this->_print_flag = $this->_print_flag ?? config('debug.print_crontab_log');
Expand All @@ -41,7 +41,7 @@ public function init()
$job_start_time = time();
$this->{$job['job']}($job);
$job_end_time = time();
$hit ++;
$hit++;

// Update the run information
app()->pdo->createCommand('UPDATE `site_crontab` set last_run_at= NOW() , next_run_at= DATE_ADD(NOW(), interval job_interval second) WHERE id=:id')->bindParams([
Expand All @@ -66,7 +66,7 @@ public function init()
}
}
$end_time = time();
$this->print_log('This Cron Work period Start At ' . $start_time.', Cost Time: ' . number_format($start_time - $end_time, 10) . 's, With ' . $hit . 'Jobs hits.');
$this->print_log('This Cron Work period Start At ' . $start_time . ', Cost Time: ' . number_format($start_time - $end_time, 10) . 's, With ' . $hit . ' Jobs hits.');
}

protected function clean_dead_peer()
Expand All @@ -79,7 +79,8 @@ protected function clean_dead_peer()
$this->print_log('Success clean ' . $affect_peer_count . ' peers from our peer list');
}

protected function clean_expired_session() {
protected function clean_expired_session()
{
$timenow = time();

$expired_sessions = app()->redis->zRangeByScore('Site:Sessions:to_expire', 0, $timenow);
Expand All @@ -90,16 +91,14 @@ protected function clean_expired_session() {
}

$clean_record_count = app()->redis->zRemRangeByScore('Site:Sessions:to_expire', 0, $timenow);
$this->print_log('Success clean expired Sessions: Database(' . count($expired_sessions) .'), Redis(' . $clean_record_count .').');
$this->print_log('Success clean expired Sessions: Database(' . count($expired_sessions) . '), Redis(' . $clean_record_count . ').');
}

// TODO sync sessions from database to redis to avoid lost (Maybe)...

protected function expired_invitee () {
protected function expired_invitee()
{
app()->pdo->createCommand('UPDATE `invite` SET `used` = -1 WHERE `expire_at` < NOW() AND `used` = 0')->execute();



$count = app()->pdo->getRowCount();
$this->print_log('Success Expired ' . $count . ' invites');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/17
* Time: 13:52
* Date: 2019/6/28
* Time: 9:46
*/

namespace apps\task;

use Rid\Base\TaskInterface;
use apps\exceptions\TrackerException;
use Rid\Base\Timer;


class TrackerAnnounceTask implements TaskInterface
class TrackerAnnounceTimer extends Timer
{

public function run($data)
public function init()
{
app()->pdo->beginTransaction();
try {
$this->processAnnounceRequest($data['queries'], $data['role'], $data['userInfo'], $data['torrentInfo']);
app()->pdo->commit();
} catch (TrackerException $e) {
app()->pdo->rollback();
// TODO throw $e;
} catch (\Exception $e) {
app()->pdo->rollback();
// TODO throw new TrackerException(998, [":msg" => $e->getMessage()]);
while (true) {
$data_raw = app()->redis->rpoplpush('Tracker:to_deal_queue', 'Tracker:backup_queue');
if ($data_raw !== false) {
$data = unserialize($data_raw);
app()->pdo->beginTransaction();
try {
$this->processAnnounceRequest($data['queries'], $data['role'], $data['userInfo'], $data['torrentInfo']);
app()->pdo->commit();
app()->redis->lRem('Tracker:backup_queue', $data_raw, 0);
} catch (\Exception $e) {
println($e->getMessage());
app()->pdo->rollback();
// TODO deal with the items in backup_queue
}
} else {
sleep(1);
}
}
}


/** TODO 2018.12.12 Check Muti-Tracker behaviour when a Transaction begin
* @param $queries
* @param $seeder
* @param $userInfo
* @param $torrentInfo
* @throws TrackerException
*/
private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentInfo)
{
Expand Down Expand Up @@ -64,8 +68,8 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn
$trueUploaded = max(0, $queries['uploaded']);
$trueDownloaded = max(0, $queries['downloaded']);

app()->pdo->createCommand("INSERT INTO `peers` SET `user_id` =:uid, `torrent_id`= :tid, `peer_id`= :pid, `started_at`= CURRENT_TIMESTAMP
`agent`=:agent, `seeder` = :seeder, {$ipField} ,
app()->pdo->createCommand("INSERT INTO `peers` SET `user_id` =:uid, `torrent_id`= :tid, `peer_id`= :pid, `started_at`= CURRENT_TIMESTAMP , `last_action_at` = CURRENT_TIMESTAMP ,
`agent`= :agent, `seeder` = :seeder, {$ipField} ,
`uploaded` = :upload , `downloaded` = :download, `to_go` = :to_go,
`corrupt` = :corrupt , `key` = :key ;
")->bindParams([
Expand Down Expand Up @@ -122,7 +126,7 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn
} else {
// if session is exist but event!=stopped , we should continue the old session
app()->pdo->createCommand("UPDATE `peers` SET `agent`=:agent, {$ipField}," .
"`seeder`=:seeder,
"`seeder`=:seeder,
`uploaded`=`uploaded` + :uploaded, `downloaded`= `downloaded` + :download, `to_go` = :left,
`last_action_at`=NOW(), `corrupt`=:corrupt, `key`=:key
WHERE `user_id` = :uid AND `torrent_id` = :tid AND `peer_id`=:pid")->bindParams([
Expand Down Expand Up @@ -173,7 +177,6 @@ private function processAnnounceRequest($queries, $seeder, $userInfo, $torrentIn
* @param $trueUploaded
* @param $trueDownloaded
* @param $duration
* @throws TrackerException
*/
private function checkUpspeed($userInfo, $torrentInfo, $trueUploaded, $trueDownloaded, $duration)
{
Expand All @@ -200,7 +203,6 @@ private function checkUpspeed($userInfo, $torrentInfo, $trueUploaded, $trueDownl
])->execute();

app()->redis->del("TRACKER:user_passkey_" . $userInfo["passkey"] . "_content");
throw new TrackerException(170);
}

// Uploaded more than 1 GB with uploading rate higher than 25 MByte/S (For Consertive level). This is likely cheating.
Expand Down
2 changes: 1 addition & 1 deletion apps/views/admin/redis_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<pre><code><?= json_encode($value, JSON_PRETTY_PRINT) ?></code></pre>
</figure>
<?php elseif ($type == \Redis::REDIS_LIST): ?>
<h2>Values</h2>
<h2>List Values</h2>
<ol>
<?php foreach ($value as $item): ?>
<li><code><?= $item ?></code></li>
Expand Down
6 changes: 4 additions & 2 deletions bin/rid-httpd
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env php
<?php

// console入口文件
/**
* httpd 入口文件
*/

require __DIR__ . '/../vendor/autoload.php';

Rid\Base\Env::load(__DIR__ . '/../.env');
Rid\Base\Env::load(__DIR__ . '/../.env'); // 获取环境变量

$config = require __DIR__ . '/../apps/config/httpd.php';
$exitCode = (new Rid\Console\Application($config))->run();
Expand Down
15 changes: 0 additions & 15 deletions framework/Base/TaskInterface.php

This file was deleted.

7 changes: 4 additions & 3 deletions framework/Base/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function after(int $msec, callable $callback)
// 清除旧定时器
$this->clear();
// 设置定时器
$timerId = swoole_timer_after($msec, function () use ($callback) {
$timerId = \Swoole\Timer::after($msec, function () use ($callback) {
// 执行闭包
try {
call_user_func($callback);
Expand All @@ -74,7 +74,7 @@ public function tick(int $msec, callable $callback)
// 清除旧定时器
$this->clear();
// 设置定时器
$timerId = swoole_timer_tick($msec, function () use ($callback) {
$timerId = \Swoole\Timer::tick($msec, function () use ($callback) {
// 执行闭包
try {
call_user_func($callback);
Expand All @@ -95,7 +95,8 @@ public function tick(int $msec, callable $callback)
public function clear()
{
if (isset($this->_timerId)) {
return swoole_timer_clear($this->_timerId);
\Swoole\Timer::clear($this->_timerId);
return true;
}
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion framework/Component/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

class Config extends Component
{
/** @var \swoole_table */
/** @var \Swoole\Table */
private $cacheTable;

private $valueField = 'data';

public function onInitialize(array $config = [])
Expand Down
10 changes: 0 additions & 10 deletions framework/Exceptions/TaskException.php

This file was deleted.

Loading

0 comments on commit 8188df9

Please sign in to comment.