Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions iznik-batch/app/Console/Commands/TrashNothing/TNSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\UserAboutMe;
use App\Models\UserReplyTime;
use App\Services\LokiService;
use App\Services\TrashNothing\Sync\PostSyncer;
use App\Traits\GracefulShutdown;
use App\Traits\LogsBatchJob;
use Illuminate\Console\Command;
Expand All @@ -20,7 +21,7 @@ class TNSyncCommand extends Command
{
use GracefulShutdown;
use LogsBatchJob;

// TODO Finnbarr: remove this after testing is complete. The Laravel scheduler's withoutOverlapping will handle locking.
use PreventsOverlapping;

Expand Down Expand Up @@ -104,25 +105,38 @@ public function handle(): int
// Merge duplicate TN users.
$duplicatesMerged = $this->mergeDuplicateTNUsers();

// Sync posts (API-based path, off by default — flip FREEGLE_TN_INGEST_POSTS_VIA_API=true to enable).
$postsProcessed = 0;
if (config('freegle.trashnothing.ingest_posts_via_api') || $this->localTesting) {
$postSyncer = new PostSyncer($this->dryRun, $this->localTesting, $this->apiKey, $this->apiBaseUrl, $this->loki);
[$postsProcessed, $postsMaxDate] = $postSyncer->sync($from, $to);
if ($postsMaxDate && (!$maxChangeDate || $postsMaxDate > $maxChangeDate)) {
$maxChangeDate = $postsMaxDate;
}
} else {
Log::info('TN-SYNC-TRACE [POSTS-SKIP] reason=feature-flag-off');
}

// Store the max change date for next sync.
if ($maxChangeDate) {
$this->storeSyncDate($maxChangeDate);
} else {
Log::info('No change date to store - no data processed');
}

if ($ratingsProcessed === 0 && $changesProcessed === 0 && $duplicatesMerged === 0) {
if ($ratingsProcessed === 0 && $changesProcessed === 0 && $duplicatesMerged === 0 && $postsProcessed === 0) {
$this->alertIfSyncStale();
}

$this->info("TN sync complete: {$ratingsProcessed} ratings, {$changesProcessed} user changes, {$duplicatesMerged} duplicates merged.");
$this->info("TN sync complete: {$ratingsProcessed} ratings, {$changesProcessed} user changes, {$duplicatesMerged} duplicates merged, {$postsProcessed} posts.");
Log::info('TN sync complete', [
'ratings_processed' => $ratingsProcessed,
'changes_processed' => $changesProcessed,
'duplicates_merged' => $duplicatesMerged,
'posts_processed' => $postsProcessed,
]);

Log::info("TN-SYNC-TRACE [END] ratings={$ratingsProcessed} changes={$changesProcessed} merges={$duplicatesMerged} max_date=" . ($maxChangeDate ?? 'null'));
Log::info("TN-SYNC-TRACE [END] ratings={$ratingsProcessed} changes={$changesProcessed} merges={$duplicatesMerged} posts={$postsProcessed} max_date=" . ($maxChangeDate ?? 'null'));

return Command::SUCCESS;
});
Expand Down
Loading