enqueueJob method
Enqueues a new job.
This mirrors the logic in GenericBackgroundService.enqueueJob
but is part
of this manager for completeness if direct management is needed.
It's generally recommended to use GenericBackgroundService.enqueueJob
as it also signals the running service.
jobKey
: Identifier for the function/task.payload
: Optional data for the job (will be JSON encoded).priority
: Job priority (0 = normal).maxAttempts
: Maximum number of retries. Returns the ID of the newly inserted job row if successful, otherwisenull
.
Implementation
Future<void> enqueueJob({
required String jobKey,
Map<String, dynamic>? payload,
int priority = 1,
int maxAttempts = 3,
}) async {
try {
final String? encodedPayload =
payload != null ? jsonEncode(payload) : null;
await db.execute(
'''
INSERT INTO background_service_jobs (job_key, payload, priority, max_attempts, status, created_at, updated_at)
VALUES (?, ?, ?, ?, 'PENDING', STRFTIME('%Y-%m-%dT%H:%M:%fZ', 'NOW'), STRFTIME('%Y-%m-%dT%H:%M:%fZ', 'NOW'))
''',
[jobKey, encodedPayload, priority, maxAttempts],
);
return;
} catch (e) {
// Consider logging the error
// print('BackgroundJobManager: Error enqueuing job "$jobKey": $e');
return;
}
}