Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

fix: withoutOverlapping on schedules, publisher token refresh lock, PublishPost tries:1, TokenExpired allows publish, API validation scoping

Paulo Castellanoa9184c94017a
diff --git a/app/Http/Requests/Api/Post/StorePostRequest.php b/app/Http/Requests/Api/Post/StorePostRequest.php
index 2fd77853f..4db0aa003 100644
--- a/app/Http/Requests/Api/Post/StorePostRequest.php
+++ b/app/Http/Requests/Api/Post/StorePostRequest.php
@@ -20,7 +20,7 @@ class StorePostRequest extends FormRequest
{
return [
'platforms' => ['required', 'array', 'min:1'],
- 'platforms.*.social_account_id' => ['required', 'uuid'],
+ 'platforms.*.social_account_id' => ['required', 'uuid', Rule::exists('social_accounts', 'id')->where('workspace_id', $this->workspace->id)],
'platforms.*.content_type' => ['required', 'string', Rule::in(array_column(ContentType::cases(), 'value'))],
'platforms.*.content' => ['nullable', 'string', 'max:63206'],
'scheduled_at' => ['nullable', 'date', 'after:now'],
diff --git a/app/Http/Requests/Api/Post/UpdatePostRequest.php b/app/Http/Requests/Api/Post/UpdatePostRequest.php
index c439b4300..88c2b587f 100644
--- a/app/Http/Requests/Api/Post/UpdatePostRequest.php
+++ b/app/Http/Requests/Api/Post/UpdatePostRequest.php
@@ -6,6 +6,7 @@ namespace App\Http\Requests\Api\Post;
use App\Enums\Post\Status;
use App\Enums\PostPlatform\ContentType;
+use App\Models\Post;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
@@ -22,7 +23,7 @@ class UpdatePostRequest extends FormRequest
'status' => ['required', 'string', Rule::in(array_column(Status::cases(), 'value'))],
'synced' => ['required', 'boolean'],
'platforms' => ['required', 'array'],
- 'platforms.*.id' => ['required', 'uuid'],
+ 'platforms.*.id' => ['required', 'uuid', Rule::exists('post_platforms', 'id')->where('post_id', $this->route('post') instanceof Post ? $this->route('post')->id : $this->route('post'))],
'platforms.*.content' => ['nullable', 'string', 'max:63206'],
'platforms.*.content_type' => ['required', 'string', Rule::in(array_column(ContentType::cases(), 'value'))],
'platforms.*.meta' => ['nullable', 'array'],
diff --git a/app/Jobs/PublishPost.php b/app/Jobs/PublishPost.php
index 928e05881..792a53681 100644
--- a/app/Jobs/PublishPost.php
+++ b/app/Jobs/PublishPost.php
@@ -13,9 +13,7 @@ class PublishPost implements ShouldQueue
{
use Queueable;
- public int $tries = 3;
-
- public int $backoff = 30;
+ public int $tries = 1;
public function __construct(public Post $post) {}
diff --git a/app/Jobs/PublishToSocialPlatform.php b/app/Jobs/PublishToSocialPlatform.php
index 20fd03d5f..69ee3f474 100644
--- a/app/Jobs/PublishToSocialPlatform.php
+++ b/app/Jobs/PublishToSocialPlatform.php
@@ -8,6 +8,7 @@ use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Enums\PostPlatform\Status as PostPlatformStatus;
use App\Enums\SocialAccount\Platform as SocialPlatform;
+use App\Enums\SocialAccount\Status;
use App\Events\PostPlatformStatusUpdated;
use App\Exceptions\Social\SocialPublishException;
use App\Exceptions\TokenExpiredException;
@@ -61,7 +62,7 @@ class PublishToSocialPlatform implements ShouldQueue
return;
}
- if ($this->postPlatform->socialAccount->isDisconnected()) {
+ if ($this->postPlatform->socialAccount->status === Status::Disconnected) {
$this->postPlatform->markAsFailed(__('posts.errors.account_disconnected'));
$this->updatePostStatus();
$this->broadcastStatus();
diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.php
index b2b906ee8..28c5f95ef 100644
--- a/app/Services/Social/BlueskyPublisher.php
+++ b/app/Services/Social/BlueskyPublisher.php
@@ -30,7 +30,7 @@ class BlueskyPublisher
// Refresh token if needed
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/Concerns/HasSocialHttpClient.php b/app/Services/Social/Concerns/HasSocialHttpClient.php
index ac823ec08..f914040b4 100644
--- a/app/Services/Social/Concerns/HasSocialHttpClient.php
+++ b/app/Services/Social/Concerns/HasSocialHttpClient.php
@@ -5,7 +5,9 @@ declare(strict_types=1);
namespace App\Services\Social\Concerns;
use App\Models\PostPlatform;
+use App\Models\SocialAccount;
use Illuminate\Http\Client\PendingRequest;
+use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
trait HasSocialHttpClient
@@ -22,6 +24,25 @@ trait HasSocialHttpClient
}
}
+ protected function refreshTokenWithLock(SocialAccount $account, callable $refreshFn): void
+ {
+ $lock = Cache::lock("token_refresh:{$account->id}", 30);
+
+ if (! $lock->get()) {
+ // Another process is refreshing, wait and reload
+ sleep(2);
+ $account->refresh();
+
+ return;
+ }
+
+ try {
+ $refreshFn();
+ } finally {
+ $lock->release();
+ }
+ }
+
protected function socialHttp(): PendingRequest
{
return Http::retry(
diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.php
index b8bf134d0..06cc40043 100644
--- a/app/Services/Social/InstagramPublisher.php
+++ b/app/Services/Social/InstagramPublisher.php
@@ -27,7 +27,7 @@ class InstagramPublisher
$account = $postPlatform->socialAccount;
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.php
index 0f890b9fd..a5e95bd76 100644
--- a/app/Services/Social/LinkedInPagePublisher.php
+++ b/app/Services/Social/LinkedInPagePublisher.php
@@ -41,7 +41,7 @@ class LinkedInPagePublisher
$this->hasRetried = false;
if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) {
- $this->refreshToken($this->account);
+ $this->refreshTokenWithLock($this->account, fn () => $this->refreshToken($this->account));
$this->account->refresh();
}
diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.php
index 80ec8ee6d..5ae0ec754 100644
--- a/app/Services/Social/LinkedInPublisher.php
+++ b/app/Services/Social/LinkedInPublisher.php
@@ -41,7 +41,7 @@ class LinkedInPublisher
$this->hasRetried = false;
if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) {
- $this->refreshToken($this->account);
+ $this->refreshTokenWithLock($this->account, fn () => $this->refreshToken($this->account));
$this->account->refresh();
}
diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.php
index cbce0587b..6e3af4d4c 100644
--- a/app/Services/Social/PinterestPublisher.php
+++ b/app/Services/Social/PinterestPublisher.php
@@ -28,7 +28,7 @@ class PinterestPublisher
$account = $postPlatform->socialAccount;
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
@@ -383,7 +383,7 @@ class PinterestPublisher
public function getBoards(SocialAccount $account): array
{
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.php
index 520cbdd55..a9744ce2d 100644
--- a/app/Services/Social/ThreadsPublisher.php
+++ b/app/Services/Social/ThreadsPublisher.php
@@ -27,7 +27,7 @@ class ThreadsPublisher
$account = $postPlatform->socialAccount;
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.php
index 17262c1b9..3de19220e 100644
--- a/app/Services/Social/TikTokPublisher.php
+++ b/app/Services/Social/TikTokPublisher.php
@@ -31,7 +31,7 @@ class TikTokPublisher
$account = $postPlatform->socialAccount;
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php
index dbbdc8760..ec6113d58 100644
--- a/app/Services/Social/XPublisher.php
+++ b/app/Services/Social/XPublisher.php
@@ -34,7 +34,7 @@ class XPublisher
// Refresh token if expired or expiring soon
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.php
index 291f5d6cc..1cb271e65 100644
--- a/app/Services/Social/YouTubePublisher.php
+++ b/app/Services/Social/YouTubePublisher.php
@@ -33,7 +33,7 @@ class YouTubePublisher
$account = $postPlatform->socialAccount;
if ($account->is_token_expired || $account->is_token_expiring_soon) {
- $this->refreshToken($account);
+ $this->refreshTokenWithLock($account, fn () => $this->refreshToken($account));
$account->refresh();
}
diff --git a/routes/console.php b/routes/console.php
index 5b2f7644e..d4657ea4c 100644
--- a/routes/console.php
+++ b/routes/console.php
@@ -8,7 +8,7 @@ use App\Console\Commands\RecoverStuckPosts;
use App\Console\Commands\RefreshExpiringTokens;
use Illuminate\Support\Facades\Schedule;
-Schedule::command(ProcessScheduledPosts::class)->everyMinute();
-Schedule::command(CheckSocialConnections::class)->daily();
-Schedule::command(RefreshExpiringTokens::class)->hourly();
-Schedule::command(RecoverStuckPosts::class)->everyThirtyMinutes();
+Schedule::command(ProcessScheduledPosts::class)->everyMinute()->withoutOverlapping()->onOneServer();
+Schedule::command(CheckSocialConnections::class)->daily()->withoutOverlapping()->onOneServer();
+Schedule::command(RefreshExpiringTokens::class)->hourly()->withoutOverlapping()->onOneServer();
+Schedule::command(RecoverStuckPosts::class)->everyThirtyMinutes()->withoutOverlapping()->onOneServer();
diff --git a/tests/Feature/Jobs/PublishToSocialPlatformTest.php b/tests/Feature/Jobs/PublishToSocialPlatformTest.php
index 4dcb4f989..7935ec0d1 100644
--- a/tests/Feature/Jobs/PublishToSocialPlatformTest.php
+++ b/tests/Feature/Jobs/PublishToSocialPlatformTest.php
@@ -204,7 +204,7 @@ test('publish to social platform skips publishing when account is inactive', fun
expect($this->postPlatform->error_message)->toBe(__('posts.errors.account_inactive'));
});
-test('publish to social platform skips publishing when account token is expired', function () {
+test('publish to social platform attempts publishing when account token is expired', function () {
Event::fake();
$this->socialAccount->update([
@@ -212,15 +212,17 @@ test('publish to social platform skips publishing when account token is expired'
]);
$publisher = Mockery::mock(LinkedInPublisher::class);
- $publisher->shouldNotReceive('publish');
+ $publisher->shouldReceive('publish')->andReturn([
+ 'id' => 'post-123',
+ 'url' => 'https://linkedin.com/post/123',
+ ]);
$this->app->instance(LinkedInPublisher::class, $publisher);
(new PublishToSocialPlatform($this->postPlatform))->handle();
$this->postPlatform->refresh();
- expect($this->postPlatform->status)->toBe(PlatformStatus::Failed);
- expect($this->postPlatform->error_message)->toBe(__('posts.errors.account_disconnected'));
+ expect($this->postPlatform->status)->toBe(PlatformStatus::Published);
});
test('publish to social platform dispatches success notification when all platforms published', function () {