job property

Future<QueueJob?> get job

Returns the persisted QueueJob record for this job payload, if one already exists.

The payload hash is derived from _name plus the serialized job fields, allowing equivalent jobs to resolve to the same stored queue record.

Returns null when no matching queue record exists.

Example:

final existing = await SendWelcomeEmail('jane@example.com').job;
print(existing?.status.name);

Implementation

Future<QueueJob?> get job async {
  final modelFields = toJson();
  final jobNameField = {'_name': runtimeType.toString()};

  // this line achieves what sorting would:
  // consistence data shape for hashing
  final payloadData = {...jobNameField, ...modelFields};

  // performance hit for sorting maps vs placing _name in-front of toJson
  // so far passing equality test for the same job payloads, as hash
  // final payload = Helpers.sortRecursive(payloadData);

  final payloadString = jsonEncode(payloadData);

  final payloadBytes = utf8.encode(payloadString);

  final payloadHash = sha256.convert(payloadBytes).toString();

  return await Model.firstWhere<QueueJob>(field: "hash", value: payloadHash);
}