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

Commit

Permalink
feat(Cleanup): Add disable cleanup job by set priority to 0
Browse files Browse the repository at this point in the history
1. Add disable cleanup job by set priority to 0
2. Fix Tracker Error when not torrent hash exist in our database
3. The info_hash of deleted torrent will add to `no_exist_torrent_info_hash_set`
  • Loading branch information
Rhilip committed Mar 19, 2019
1 parent 0d83408 commit be94de3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
10 changes: 4 additions & 6 deletions apps/controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private function getTorrentInfoByHash($hash, $field)
if ($exist === 0) { // This cache key is not exist , get it's information from db and then cache it
$torrentInfo = app()->pdo->createCommand("SELECT id , info_hash , owner_id , status , incomplete , complete, downloaded , added_at FROM torrents WHERE info_hash = :info LIMIT 1")
->bindParams(["info" => $hash])->queryOne();
if ($torrentInfo === false) { // No-exist torrent
if ($torrentInfo === false || $torrentInfo['status'] == 'deleted') { // No-exist or deleted torrent
app()->redis->sAdd('Tracker:no_exist_torrent_info_hash_set', $hash);
return [];
}
Expand Down Expand Up @@ -293,7 +293,7 @@ private function generateScrapeResponse($info_hash_array, &$rep_dict)
$torrent_details = [];
foreach ($info_hash_array as $item) {
$metadata = $this->getTorrentInfoByHash($item, ['incomplete', 'complete', 'downloaded']);
if (!is_null($metadata)) $torrent_details[$item] = $metadata; // Append it to tmp array only it exist.
if (!empty($metadata)) $torrent_details[$item] = $metadata; // Append it to tmp array only it exist.
}

$rep_dict = ["files" => $torrent_details];
Expand Down Expand Up @@ -588,7 +588,7 @@ private function checkPortFields($port)
private function getTorrentInfo($queries, $userInfo, &$torrentInfo)
{
$torrentInfo = $this->getTorrentInfoByHash($queries["info_hash"], ['id', 'info_hash', 'owner_id', 'status', 'incomplete', 'complete', 'added_at']);
if (is_null($torrentInfo)) throw new TrackerException(150);
if (empty($torrentInfo)) throw new TrackerException(150);

switch ($torrentInfo["status"]) {
case 'confirmed' :
Expand All @@ -608,11 +608,9 @@ private function getTorrentInfo($queries, $userInfo, &$torrentInfo)
throw new TrackerException(151, [":status" => $torrentInfo["status"]]);
break;
}
case 'deleted' :
default:
{
// For Deleted Torrent , no one can connect anymore..
throw new TrackerException(151, [":status" => $torrentInfo["status"]]);
throw new TrackerException(152, [":status" => $torrentInfo["status"]]);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/exceptions/TrackerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TrackerException extends \Exception
// Error message about Torrent
150 => 'Torrent not registered with this tracker.',
151 => 'You do not have permission to download a :status torrent.',
152 => 'The torrent status :status is not valid.',

// Error message about Download Session
160 => 'You cannot seed the same torrent from more than :count locations.',
Expand Down
2 changes: 1 addition & 1 deletion apps/task/CronTabTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private function print_log($log)
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();
$to_run_jobs = app()->pdo->createCommand('SELECT * FROM `site_crontab` WHERE `priority` > 0 AND `next_run_at` < NOW() ORDER BY priority ASC;')->queryAll();
foreach ($to_run_jobs as $job) {
if (method_exists($this, $job['job'])) {
app()->pdo->beginTransaction();
Expand Down
7 changes: 4 additions & 3 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 19, 2019 at 02:18 PM
-- Generation Time: Mar 19, 2019 at 03:13 PM
-- Server version: 8.0.14
-- PHP Version: 7.3.1

Expand Down Expand Up @@ -423,11 +423,12 @@ DROP TABLE IF EXISTS `site_crontab`;
CREATE TABLE IF NOT EXISTS `site_crontab` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`job` varchar(64) NOT NULL,
`priority` int(10) UNSIGNED NOT NULL DEFAULT '100',
`priority` int(10) UNSIGNED NOT NULL DEFAULT '100' COMMENT '0 - disable this crontab work, else the lower number job have higher priority, by default 100',
`job_interval` int(11) NOT NULL,
`last_run_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`next_run_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
KEY `IN_site_crontab_priority_next_run_at` (`priority`,`next_run_at`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
Expand Down

0 comments on commit be94de3

Please sign in to comment.