add command event:fire + fetch imdb when torrent created

This commit is contained in:
xiaomlove
2024-02-23 02:38:42 +08:00
parent 5c6a2b13f9
commit 9efa5b2ae5
12 changed files with 227 additions and 24 deletions
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Console\Commands;
use App\Events\TorrentCreated;
use Illuminate\Console\Command;
class FireEvent extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'event:fire {--name=} {--id=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fire a event, options: --name, --id';
protected array $eventMaps = [
"torrent_created" => TorrentCreated::class
];
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$name = $this->option('name');
$id = $this->option('id');
$log = "FireEvent, name: $name, id: $id";
if (isset($this->eventMaps[$name])) {
$result = call_user_func([$this->eventMaps[$name], "dispatch"], $id);
$this->info("$log, success call dispatch, result: " . var_export($result, true));
} else {
$this->error("$log, no event match this name");
}
return Command::SUCCESS;
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace App\Console\Commands;
use App\Events\TorrentCreated;
use Illuminate\Console\Command;
class TorrentFetchImdb extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'torrent:fetch_imdb {torrent_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch torrent imdb info';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$torrentId = $this->argument("torrent_id");
$this->info("torrentId: $torrentId");
if (!$torrentId) {
$this->error("require argument torrent_id");
return Command::FAILURE;
}
TorrentCreated::dispatch($torrentId);
return Command::SUCCESS;
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class TorrentCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public int $torrentId;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(int $torrentId)
{
$this->torrentId = $torrentId;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace App\Listeners;
use App\Models\Torrent;
use App\Repositories\TorrentRepository;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class FetchTorrentImdb implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
$torrentId = $event->torrentId;
$torrentRep = new TorrentRepository();
$torrentRep->fetchImdb($torrentId);
do_log("fetchImdb for torrent: $torrentId done!");
}
}
+5
View File
@@ -3,7 +3,9 @@
namespace App\Providers;
use App\Events\SeedBoxRecordUpdated;
use App\Events\TorrentCreated;
use App\Events\TorrentUpdated;
use App\Listeners\FetchTorrentImdb;
use App\Listeners\RemoveSeedBoxRecordCache;
use App\Listeners\SyncTorrentToEs;
use Illuminate\Auth\Events\Registered;
@@ -28,6 +30,9 @@ class EventServiceProvider extends ServiceProvider
SeedBoxRecordUpdated::class => [
RemoveSeedBoxRecordCache::class,
],
TorrentCreated::class => [
FetchTorrentImdb::class,
],
];
/**
+1 -1
View File
@@ -350,7 +350,7 @@ class BonusRepository extends BaseRepository
->where('seedbonus', $oldUserBonus)
->update($userUpdates);
if ($affectedRows != 1) {
do_log("update user seedbonus affected rows != 1, query: " . last_query(), 'error');
do_log("update user seedbonus affected rows: ".$affectedRows." != 1, query: " . last_query(), 'error');
throw new \RuntimeException("Update user seedbonus fail.");
}
$nowStr = now()->toDateTimeString();
+30 -1
View File
@@ -34,8 +34,8 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Nexus\Database\NexusDB;
use Nexus\Imdb\Imdb;
use Rhilip\Bencode\Bencode;
use function Sodium\compare;
class TorrentRepository extends BaseRepository
{
@@ -862,4 +862,33 @@ HTML;
return compact('total', 'success');
}
public function fetchImdb(int $torrentId): void
{
$torrent = Torrent::query()->findOrFail($torrentId, ["id", "url", "cache_stamp"]);
$imdb_id = parse_imdb_id($torrent->url);
$log = sprintf("fetchImdb torrentId: %s", $torrentId);
if (!$imdb_id) {
do_log("$log, no imdb_id");
return;
}
$thenumbers = $imdb_id;
$imdb = new Imdb();
$torrent->cache_stamp = time();
$torrent->save();
$imdb->purgeSingle($imdb_id);
try {
$imdb->updateCache($imdb_id);
NexusDB::cache_del('imdb_id_'.$thenumbers.'_movie_name');
NexusDB::cache_del('imdb_id_'.$thenumbers.'_large', true);
NexusDB::cache_del('imdb_id_'.$thenumbers.'_median', true);
NexusDB::cache_del('imdb_id_'.$thenumbers.'_minor', true);
do_log("$log, done");
} catch (\Exception $e) {
$log .= ", error: " . $e->getMessage() . ", trace: " . $e->getTraceAsString();
do_log($log, 'error');
}
}
}