getJobsByJobKey method

Future<List<BackgroundJob>> getJobsByJobKey(
  1. String jobKey, {
  2. BackgroundJobStatus? status,
  3. int? limit,
  4. int? offset,
})

Fetches jobs by their jobKey.

  • jobKey: The specific job key to filter by.
  • status: Optional BackgroundJobStatus to further filter by.
  • limit: Optional limit on the number of jobs to return.
  • offset: Optional offset for pagination.

Implementation

Future<List<BackgroundJob>> getJobsByJobKey(
  String jobKey, {
  BackgroundJobStatus? status,
  int? limit,
  int? offset,
}) async {
  String query = "SELECT * FROM background_service_jobs WHERE job_key = ?";
  final params = <Object?>[jobKey];

  if (status != null) {
    query += " AND status = ?";
    params.add(status.name.toUpperCase());
  }
  query += " ORDER BY created_at DESC"; // Default to newest first

  if (limit != null) {
    query += " LIMIT ?";
    params.add(limit);
    if (offset != null) {
      query += " OFFSET ?";
      params.add(offset);
    }
  }
  final List<Map<String, Object?>> results = await db.getAll(query, params);
  return results.map((map) => BackgroundJob.fromMap(map)).toList();
}