Skip to content

A self-hosted media server and file management system

fix: async plausible request does not run

aminnausin4b1a30cd12d6
diff --git a/app/Http/Controllers/Proxy/PlausibleProxyController.php b/app/Http/Controllers/Proxy/PlausibleProxyController.php
index 6ec5b1e7..2c53bb93 100644
--- a/app/Http/Controllers/Proxy/PlausibleProxyController.php
+++ b/app/Http/Controllers/Proxy/PlausibleProxyController.php
@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Proxy;
use App\Http\Controllers\Controller;
+use App\Jobs\Proxy\AsyncPlausibleEvent;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
@@ -43,11 +44,20 @@ class PlausibleProxyController extends Controller {
abort(413);
}
- Http::withHeaders([
- 'User-Agent' => $request->userAgent(),
- 'X-Forwarded-For' => $request->ip(),
- 'Content-Type' => self::EVENT_CONTENT_TYPE,
- ])->withBody($request->getContent(), self::EVENT_CONTENT_TYPE)->async()->post($baseUrl . '/api/event');
+ // Http::withHeaders([
+ // 'User-Agent' => $request->userAgent(),
+ // 'X-Forwarded-For' => $request->ip(),
+ // 'Content-Type' => self::EVENT_CONTENT_TYPE,
+ // ])->withBody($request->getContent(), self::EVENT_CONTENT_TYPE)->post($baseUrl . '/api/event');
+
+ AsyncPlausibleEvent::dispatch(
+ body: $request->getContent(),
+ headers: [
+ 'User-Agent' => $request->userAgent(),
+ 'X-Forwarded-For' => $request->ip(),
+ 'Content-Type' => 'application/json',
+ ],
+ );
return response('ok', 202, ['Content-Type' => self::EVENT_CONTENT_TYPE]);
}
diff --git a/app/Jobs/Proxy/AsyncPlausibleEvent.php b/app/Jobs/Proxy/AsyncPlausibleEvent.php
new file mode 100644
index 00000000..fbea4685
--- /dev/null
+++ b/app/Jobs/Proxy/AsyncPlausibleEvent.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Jobs\Proxy;
+
+class AsyncPlausibleEvent extends AsyncProxyEvent {
+ protected function url(): string {
+ return rtrim(config('services.plausible.domain'), '/') . '/api/event';
+ }
+}
diff --git a/app/Jobs/Proxy/AsyncProxyEvent.php b/app/Jobs/Proxy/AsyncProxyEvent.php
new file mode 100644
index 00000000..b7e6e55a
--- /dev/null
+++ b/app/Jobs/Proxy/AsyncProxyEvent.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Jobs\Proxy;
+
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Foundation\Queue\Queueable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Facades\Http;
+
+abstract class AsyncProxyEvent implements ShouldQueue {
+ use Dispatchable, InteractsWithQueue, Queueable;
+
+ public function __construct(
+ public readonly string $body,
+ public readonly array $headers = [],
+ ) {
+ $this->onQueue('high');
+ }
+
+ abstract protected function url(): string;
+
+ public function handle(): void {
+ Http::timeout(2)
+ ->connectTimeout(1)
+ ->withHeaders($this->headers)
+ ->withBody($this->body, $this->headers['Content-Type'] ?? 'application/json')
+ ->post($this->url());
+ }
+}