Files
nexusphp/app/Jobs/GenerateTemporaryInvite.php

102 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2022-12-13 13:51:39 +08:00
<?php
namespace App\Jobs;
use App\Models\Invite;
use App\Repositories\ToolRepository;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
2024-02-26 14:25:54 +08:00
use Nexus\Database\NexusDB;
2022-12-13 13:51:39 +08:00
class GenerateTemporaryInvite implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private int $count;
2024-02-26 14:25:54 +08:00
private string $idRedisKey;
2022-12-13 13:51:39 +08:00
private int $days;
/**
* Create a new job instance.
*
* @return void
*/
2024-02-26 14:25:54 +08:00
public function __construct(string $idRedisKey, int $days, int $count)
2022-12-13 13:51:39 +08:00
{
2024-02-26 14:25:54 +08:00
$this->idRedisKey = $idRedisKey;
2022-12-13 13:51:39 +08:00
$this->days = $days;
$this->count = $count;
}
2023-02-21 01:49:17 +08:00
public $tries = 1;
public $timeout = 1800;
2022-12-13 13:51:39 +08:00
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2024-02-27 00:18:41 +08:00
$beginTimestamp = microtime(true);
2022-12-13 13:51:39 +08:00
$toolRep = new ToolRepository();
2024-02-26 14:25:54 +08:00
$idStr = NexusDB::cache_get($this->idRedisKey);
2024-02-27 00:18:41 +08:00
$logPrefix = "idRedisKey: " . $this->idRedisKey;
2024-02-26 14:25:54 +08:00
if (empty($idStr)) {
2024-02-27 00:18:41 +08:00
do_log("$logPrefix, no idStr...");
2024-02-26 14:25:54 +08:00
return;
}
$idArr = explode(",", $idStr);
2024-02-27 00:18:41 +08:00
$count = count($idArr);
$logPrefix .= ", count: $count";
do_log("$logPrefix, going to handle...");
2024-02-26 23:44:17 +08:00
$now = Carbon::now();
$expiredAt = Carbon::now()->addDays($this->days);
2024-02-26 14:25:54 +08:00
foreach ($idArr as $uid) {
2022-12-13 13:51:39 +08:00
try {
$hashArr = $toolRep->generateUniqueInviteHash([], $this->count, $this->count);
$data = [];
foreach($hashArr as $hash) {
$data[] = [
'inviter' => $uid,
'invitee' => '',
'hash' => $hash,
'valid' => 0,
2024-02-26 23:44:17 +08:00
'expired_at' => $expiredAt,
'created_at' => $now,
2022-12-13 13:51:39 +08:00
];
}
if (!empty($data)) {
Invite::query()->insert($data);
}
2024-02-27 00:18:41 +08:00
do_log("$logPrefix, success add $this->count temporary invite ($this->days days) to $uid");
2022-12-13 13:51:39 +08:00
} catch (\Exception $exception) {
2024-02-27 00:18:41 +08:00
do_log("$logPrefix, fail add $this->count temporary invite ($this->days days) to $uid: " . $exception->getMessage(), 'error');
2022-12-13 13:51:39 +08:00
}
}
2024-02-27 00:18:41 +08:00
NexusDB::cache_del($this->idRedisKey);
do_log("$logPrefix, handle done, cost time: " . (microtime(true) - $beginTimestamp) . " seconds.");
2022-12-13 13:51:39 +08:00
}
2023-03-04 01:13:41 +08:00
/**
* Handle a job failure.
*
* @param \Throwable $exception
* @return void
*/
public function failed(\Throwable $exception)
{
do_log("failed: " . $exception->getMessage() . $exception->getTraceAsString(), 'error');
}
2022-12-13 13:51:39 +08:00
}