Skip to content

Get task results

You’ll read results three ways: waiting inline, polling by id from another request, and reading many at once.

  • An app with a result backend (GisilaQueue.redis has one by default; with the plain constructor pass backend:).
  • A worker running the 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.

For anything longer than a request should wait:

// POST /exports
final 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.

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
}
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.

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>();

Redis results expire after resultTtl (24 h by default; set it on GisilaQueue.redis(resultTtl: ...) or RedisResultBackend). Delete one early with await r.forget().

final r = await add.delay([2, 3]);
print(await r.state); // TaskState.pending
print(await r.get()); // 5
print(await r.successful); // true