Get task results
You’ll read results three ways: waiting inline, polling by id from another request, and reading many at once.
Prerequisites
Section titled “Prerequisites”- An app with a result backend (
GisilaQueue.redishas one by default; with the plain constructor passbackend:). - A worker running the tasks.
1. Wait inline for short tasks
Section titled “1. Wait inline for short tasks”import 'package:gisila_queue/gisila_queue.dart';
final r = await thumbnail.delay([imageId]);final url = await r.get(timeout: const Duration(seconds: 5));get polls every 100 ms by default. Pass pollInterval: to poll less often for long
tasks.
2. Return the id and check later
Section titled “2. Return the id and check later”For anything longer than a request should wait:
// POST /exportsfinal r = await exportCsv.delay([reportId]);return {'job': r.id};
// GET /exports/{id}final job = app.resultFor<String>(id);final state = await job.state;if (state == TaskState.success) return {'state': 'done', 'url': await job.get()};if (state.isDone) return {'state': state.name};return {'state': 'running'};resultFor works in any process that shares the backend and namespace.
3. Handle failures and timeouts
Section titled “3. Handle failures and timeouts”try { final value = await r.get(timeout: const Duration(seconds: 30));} on TaskFailureException catch (e) { // failed after retries, or revoked log('task ${e.taskId} failed: ${e.message}');} on ResultTimeoutException { // not finished yet; the task keeps going}4. Read results of a group
Section titled “4. Read results of a group”final results = await app.group<String>([ for (final id in imageIds) thumbnail.sig(args: [id]),]).apply();
final urls = await results.get(timeout: const Duration(minutes: 2));GroupResult.get throws on the first failed member.
5. Keep JSON shapes in mind
Section titled “5. Keep JSON shapes in mind”Results are stored as JSON on Redis. Type TaskRefs with JSON-friendly types and convert
on read:
final stats = app.task<Map<String, dynamic>>('reports.stats', (ctx) async => { 'count': 3, 'ids': [1, 2, 3], });
final r = await stats.delay();final map = await r.get();final ids = (map!['ids'] as List).cast<int>();6. Clean up
Section titled “6. Clean up”Redis results expire after resultTtl (24 h by default; set it on
GisilaQueue.redis(resultTtl: ...) or RedisResultBackend). Delete one early with
await r.forget().
Verify it works
Section titled “Verify it works”final r = await add.delay([2, 3]);print(await r.state); // TaskState.pendingprint(await r.get()); // 5print(await r.successful); // true