Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
adrolli committed Sep 4, 2024
1 parent c6d21e3 commit fb2bcaf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 53 deletions.
98 changes: 98 additions & 0 deletions packages/sync/src/Jobs/PrepareSyncJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Moox\Sync\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Moox\Core\Traits\LogLevel;
use Moox\Sync\Models\Platform;

class PrepareSyncJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, LogLevel, Queueable, SerializesModels;

protected $modelId;

protected $modelClass;

protected $eventType;

protected $platformId;

public function __construct($modelId, $modelClass, $eventType, $platformId)
{
$this->modelId = $modelId;
$this->modelClass = $modelClass;
$this->eventType = $eventType;
$this->platformId = $platformId;
}

public function handle()
{
$this->logDebug('DeferredSyncJob started', [
'model_id' => $this->modelId,
'model_class' => $this->modelClass,
'event_type' => $this->eventType,
'platform_id' => $this->platformId,
]);

$model = $this->modelClass::findOrFail($this->modelId);
$platform = Platform::findOrFail($this->platformId);

$modelData = $model->toArray();

if ($model instanceof \Moox\Press\Models\WpUser) {
$userMeta = $model->getAllMetaAttributes();
$this->logDebug('User meta data retrieved in deferred job', ['user_meta' => $userMeta]);
$modelData = array_merge($modelData, $userMeta);
}

$syncData = [
'event_type' => $this->eventType,
'model' => $modelData,
'model_class' => $this->modelClass,
'platform' => $platform->toArray(),
];

$this->logDebug('Sync data prepared in deferred job', ['sync_data' => $syncData]);

$this->invokeWebhooks($syncData);
}

protected function invokeWebhooks(array $data)
{
$targetPlatforms = Platform::where('id', '!=', $this->platformId)->get();
$this->logDebug('Moox Sync: Invoking webhooks', ['target_platforms' => $targetPlatforms->pluck('id')]);

foreach ($targetPlatforms as $targetPlatform) {
$webhookUrl = 'https://'.$targetPlatform->domain.'/sync-webhook';

$this->logDebug('Moox Sync: Sending webhook', ['url' => $webhookUrl, 'target_platform' => $targetPlatform->id]);

try {
$response = Http::withToken($targetPlatform->api_token)
->post($webhookUrl, $data);

if ($response->successful()) {
$this->logDebug('Moox Sync: Webhook sent successfully', ['target_platform' => $targetPlatform->id]);
} else {
Log::error('Moox Sync: Webhook failed', [
'target_platform' => $targetPlatform->id,
'status' => $response->status(),
'body' => $response->body(),
]);
}
} catch (\Exception $e) {
Log::error('Moox Sync: Webhook error', [
'target_platform' => $targetPlatform->id,
'error' => $e->getMessage(),
]);
}
}
}
}
57 changes: 4 additions & 53 deletions packages/sync/src/Listener/SyncListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Moox\Core\Traits\LogLevel;
use Moox\Sync\Jobs\DeferredSyncJob;
use Moox\Sync\Models\Platform;
use Moox\Sync\Models\Sync;
use Moox\Sync\Services\SyncService;
Expand Down Expand Up @@ -88,63 +88,14 @@ protected function handleModelEvent($model, $eventType)
return;
}

$this->logDebug('Handling model event', [
$this->logDebug('Dispatching DeferredSyncJob', [
'model' => get_class($model),
'id' => $model->id,
'event' => $eventType,
'platform' => $this->currentPlatform->id,
]);

$modelData = $model->toArray();

// If this is a WordPress user model, include all meta data
if ($model instanceof \Moox\Press\Models\WpUser) {
$userMeta = $model->getAllMetaAttributes();
$this->logDebug('User meta data retrieved', ['user_meta' => $userMeta]);
$modelData = array_merge($modelData, $userMeta);
}

$syncData = [
'event_type' => $eventType,
'model' => $modelData,
'model_class' => get_class($model),
'platform' => $this->currentPlatform->toArray(),
];

$this->logDebug('Sync data prepared', ['sync_data' => $syncData]);

$this->invokeWebhooks($syncData);
}

protected function invokeWebhooks(array $data)
{
$targetPlatforms = Platform::where('id', '!=', $this->currentPlatform->id)->get();
$this->logDebug('Moox Sync: Invoking webhooks', ['target_platforms' => $targetPlatforms->pluck('id')]);

foreach ($targetPlatforms as $targetPlatform) {
$webhookUrl = 'https://'.$targetPlatform->domain.'/sync-webhook';

$this->logDebug('Moox Sync: Sending webhook', ['url' => $webhookUrl, 'target_platform' => $targetPlatform->id]);

try {
$response = Http::withToken($targetPlatform->api_token)
->post($webhookUrl, $data);

if ($response->successful()) {
$this->logDebug('Moox Sync: Webhook sent successfully', ['target_platform' => $targetPlatform->id]);
} else {
Log::error('Moox Sync: Webhook failed', [
'target_platform' => $targetPlatform->id,
'status' => $response->status(),
'body' => $response->body(),
]);
}
} catch (\Exception $e) {
Log::error('Moox Sync: Webhook error', [
'target_platform' => $targetPlatform->id,
'error' => $e->getMessage(),
]);
}
}
DeferredSyncJob::dispatch($model->id, get_class($model), $eventType, $this->currentPlatform->id)
->delay(now()->addSeconds(5));
}
}

0 comments on commit fb2bcaf

Please sign in to comment.