NativePHP Queue System with background processing#7
Open
dircm wants to merge 5 commits intoNativePHP:mainfrom
Open
NativePHP Queue System with background processing#7dircm wants to merge 5 commits intoNativePHP:mainfrom
dircm wants to merge 5 commits intoNativePHP:mainfrom
Conversation
Implements a queue driver that processes Laravel jobs while the app is in the foreground. Jobs are stored in SQLite and processed by native coordinators (Swift/Kotlin) between UI interactions to avoid blocking. Key components: - Native queue driver (`native`) extending DatabaseQueue - NativeQueueController with HTTP API (/_native/queue/*) - NativeQueueCoordinator for iOS and Android - Bridge functions (Queue.JobsAvailable) to notify native layer - Events: JobCompleted, JobFailed, QueueEmpty for Livewire listeners - Configurable throttling (min_delay, batch_size, poll_interval) Critical fix: All PHP execution (WebView + queue) uses shared serial queue (PHPSchemeHandler.phpSerialQueue) to prevent concurrent PHP crashes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Change QUEUE_CONNECTION from 'sync' to 'native' on Android so jobs are stored in the database rather than processed synchronously - Remove debug logging from Swift and Kotlin queue coordinators - Remove dead code (unused imports, variables) from queue files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implement platform-native background task APIs to process Laravel queue jobs when the app is in the background: iOS (BGTaskScheduler): - Add BackgroundQueueWorker.swift with BGProcessingTask support - Register task handler in AppDelegate at launch - Schedule background tasks via scenePhase observer in NativePHPApp - Process up to 50 jobs in 25-second background window - Add 'processing' background mode to Info.plist Android (WorkManager): - Add BackgroundQueueWorker.kt extending CoroutineWorker - Schedule periodic work (15-min intervals) when app stops - Cancel background work and resume foreground on app start - Process up to 50 jobs in 9-minute background window - Add WorkManager dependency to build.gradle.kts Shared: - Add PHPBridge singleton accessor for background worker access - Extract durationMs from queue work response in coordinators - Add background queue config to nativephp.php - Return background config in NativeQueueController status response Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces a queue processing system that allows NativePHP Mobile apps to dispatch and process Laravel jobs both while the app is active (foreground) and when it's in the background. Jobs are stored in the native SQLite database and processed by native coordinators (Swift/Kotlin) that automatically hand off between foreground and background processing based on app lifecycle.
Why This Matters
Mobile apps need background job processing for tasks like syncing data with remote servers, processing uploads/downloads, deferring processing and more..
Traditional Laravel queue workers dont currently run on nativephp mobile. This implementation provides a mobile-native solution that:
MyJob::dispatch())Architecture
Features
One job at a time: Jobs are processed sequentially to avoid concurrent PHP execution (PHP is single-threaded in the embedded context)
Serial queue on iOS: All PHP execution (WebView requests + queue work) uses a shared
DispatchQueueto prevent crashes from concurrent PHP accessPolling with notifications: The coordinator polls for jobs but also receives immediate notifications when new jobs are dispatched, reducing latency
Configurable throttling: Batch limits and delays prevent queue processing from starving UI responsiveness
Automatic lifecycle handoff: When the app backgrounds, the foreground coordinator pauses and schedules a background task. When foregrounding, background work is cancelled and foreground processing resumes. NOTE: Backgrounded tasks can be run very slowly ~15mins on an idle ios device or not at all if the device is doing other things..
Laravel queue features
Fully Supported ✅
Partially Supported⚠️
Not Supported ❌
File Structure
PHP Components
src/Queue/NativeQueue.phpDatabaseQueue, notifies native layer on job pushsrc/Queue/NativeQueueConnector.phpnativedriversrc/Queue/NativeQueueController.php/work,/status,/retry)src/Queue/NativeQueueServiceProvider.phpsrc/Queue/Events/JobCompleted.phpsrc/Queue/Events/JobFailed.phpsrc/Queue/Events/QueueEmpty.phpiOS (Swift) Components
Queue/NativeQueueCoordinator.swiftQueue/BackgroundQueueWorker.swiftBridge/Functions/QueueFunctions.swiftQueue.JobsAvailableAndroid (Kotlin) Components
queue/NativeQueueCoordinator.ktqueue/BackgroundQueueWorker.ktbridge/functions/QueueFunctions.ktQueue.JobsAvailableUsage
1. Configure Queue Connection
In your app's
.env:In
config/queue.php, add the native connection:2. Dispatch Jobs
Use Laravel's standard job dispatch API:
3. Listen for Events in Livewire
4. Optional Configuration
In
config/nativephp.php:Testing
Testing Background Processing
iOS (Xcode Debugger):
While the app is running in Xcode, use the debugger console to simulate a background task:
Replace
<bundle-id>with your app's bundle identifier (e.g.,com.myapp.example).Android (ADB):
Monitor background processing via logcat:
Look for:
App stopped - scheduled background queue processingProcessed job X: JobClassNameApp started - resumed foreground queue processingManual Testing Flow
Limitations
Background timing is system-controlled: iOS and Android decide when to run background tasks based on battery, device state, and usage patterns. Jobs will eventually process, but not immediately.
No priority queues: All jobs processed in FIFO order. Multiple queue support exists but priority is not implemented.
pauseDuringScrollnot implemented: The coordinator has this config option but scroll detection is not wired up yet.