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

Commit

Permalink
feat(Cleanup): Cleanup dead peers
Browse files Browse the repository at this point in the history
This is the first example of CronTab Job which Help system to clean those
long no-announce peers
By default is the expired time is the 1.8 times of the config 'tracker.interval'

BREAKING CHANGE: dbstructure of `site_crontab` Change
  • Loading branch information
Rhilip committed Mar 19, 2019
1 parent 65f09ff commit 0d83408
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
1 change: 0 additions & 1 deletion apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function getOwnerId()
} else {
return $this->owner_id;
}

}

public function getOwner()
Expand Down
57 changes: 57 additions & 0 deletions apps/task/CronTabTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,65 @@

class CronTabTimer extends Timer
{

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

private function print_log($log)
{
$this->_print_flag = $this->_print_flag ?? app()->config->get('debug.print_crontab_log');
if ($this->_print_flag) {
println($log);
}
}

/**
* @throws \Exception
*/
public function init()
{
// Get all run
$to_run_jobs = app()->pdo->createCommand('SELECT * FROM `site_crontab` WHERE `next_run_at` < NOW() ORDER BY priority ASC;')->queryAll();
foreach ($to_run_jobs as $job) {
if (method_exists($this, $job['job'])) {
app()->pdo->beginTransaction();
$this->print_log('CronTab Worker Start To run job : ' . $job['job']);
try {
// Run this job
$start_time = time();
$this->{$job['job']}($job);
$end_time = time();

// 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([
'id' => $job['id']
])->queryOne();
$next_run_at = app()->pdo->createCommand('SELECT `next_run_at` FROM `site_crontab` WHERE id=:id')->bindParams([
'id' => $job['id']
])->queryScalar(); // FIXME Bad Code
$this->print_log('The run job : ' . $job['job'] . ' Finished. ' .
'Cost time: ' . number_format($end_time - $start_time, 10) . 's, ' . 'Next run at : ' . $next_run_at
);

// Finish The Transaction and commit~
app()->pdo->commit();
} catch (\Exception $e) {
app()->pdo->rollback();
app()->log->critical('The run job throw Exception : ' . $e->getMessage());
if (env('APP_DEBUG')) throw $e;
}
} else {
app()->log->critical('CronTab Worker Tries to run a none-exist job:' . $job['job']);
}
}
}

protected function clean_dead_peer()
{
$deadtime = floor(app()->config->get('tracker.interval') * 1.8);
app()->pdo->createCommand('DELETE FROM `peers` WHERE last_action_at < DATE_SUB(NOW(), interval :deadtime second )')->bindParams([
'deadtime' => $deadtime
])->execute();
$affect_peer_count = app()->pdo->getRowCount();
$this->print_log('Success clean ' . $affect_peer_count . ' peers from our peer list');
}
}
14 changes: 13 additions & 1 deletion framework/Base/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class Timer implements StaticInstanceInterface
* @var int
*/
protected $_timerId;

protected $_config;

/**
* @return int
*/
public function getTimerId(): int
{
return $this->_timerId;
}

/**
* 在指定的时间后执行函数
* 一次性执行
Expand Down Expand Up @@ -91,11 +102,12 @@ public function clear()

public function run($config)
{
$this->_config = $config;
$type = $config['type'];
$msec = $config['msec'];
$callback = $config['callback'];

println('New Timer '. self::class .' added. (Type: ' . $type . ', msec: ' . $msec . ', callback function: ' . $callback . ')');
$this->{$type}($msec, [$this, $callback]);
println('New Timer ' . self::class . ' added. (Type: ' . $type . ', msec: ' . $msec . ', callback function: ' . $callback . ')');
}
}
2 changes: 1 addition & 1 deletion framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function onWorkerStart()
foreach (app()->env('timer') as $timer_name => $timer_config) {
$timer_class = $timer_config['class'];
$timer = new $timer_class();
if ($timer instanceof \Rid\Base\TimerInterface) {
if ($timer instanceof Timer) {
$timer->run($timer_config);
}
}
Expand Down
22 changes: 18 additions & 4 deletions migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 14, 2019 at 03:59 PM
-- Generation Time: Mar 19, 2019 at 02:18 PM
-- Server version: 8.0.14
-- PHP Version: 7.3.1

Expand Down Expand Up @@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS `bookmarks` (
`tid` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UN_bookmarks_uid_tid` (`tid`,`uid`),
KEY `IN_bookmarks_users_id` (`uid`)
KEY `IN_bookmarks_users_id` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
Expand Down Expand Up @@ -421,17 +421,31 @@ INSERT INTO `site_config` (`name`, `value`) VALUES

DROP TABLE IF EXISTS `site_crontab`;
CREATE TABLE IF NOT EXISTS `site_crontab` (
`id` int(10) UNSIGNED NOT NULL,
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`job` varchar(64) NOT NULL,
`priority` int(10) UNSIGNED NOT NULL DEFAULT '100',
`job_interval` int(11) NOT NULL,
`last_run_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`next_run_at` timestamp NOT NULL
`next_run_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
-- RELATIONSHIPS FOR TABLE `site_crontab`:
--

--
-- Truncate table before insert `site_crontab`
--

TRUNCATE TABLE `site_crontab`;
--
-- Dumping data for table `site_crontab`
--

INSERT INTO `site_crontab` (`id`, `job`, `priority`, `job_interval`) VALUES
(1, 'clean_dead_peer', 1, 600);

-- --------------------------------------------------------

--
Expand Down

0 comments on commit 0d83408

Please sign in to comment.