enqueue method

Future<void> enqueue(
  1. Map<String, dynamic> data,
  2. String functionName,
  3. String? extensionId,
  4. TaskOptions? options,
)

Enqueues a task to the specified function's queue.

Implementation

Future<void> enqueue(
  Map<String, dynamic> data,
  String functionName,
  String? extensionId,
  TaskOptions? options,
) async {
  validateNonEmptyString(functionName, 'functionName');

  // Parse the function name to extract project, location, and function ID
  final resources = _parseResourceName(functionName, 'functions');

  return _httpClient.cloudTasks((api, projectId) async {
    // Fill in missing resource components
    resources.projectId ??= projectId;
    resources.locationId ??= _defaultLocation;

    validateNonEmptyString(resources.resourceId, 'resourceId');

    // Apply extension ID prefix if provided
    var queueId = resources.resourceId;
    if (extensionId != null && extensionId.isNotEmpty) {
      queueId = 'ext-$extensionId-$queueId';
    }

    // Build the task
    final task = _buildTask(data, resources, queueId, options);

    // Update task with proper authentication (OIDC token or Authorization header)
    await _updateTaskAuth(task, await _httpClient.client, extensionId);

    final parent = _httpClient.buildTasksParent(
      projectId: resources.projectId!,
      locationId: resources.locationId!,
      queueId: queueId,
    );

    try {
      await api.projects.locations.queues.tasks.create(
        tasks2.CreateTaskRequest(task: task),
        parent,
      );
    } on tasks2.DetailedApiRequestError catch (error) {
      // Handle 409 Conflict (task already exists)
      if (error.status == 409) {
        throw FirebaseFunctionsAdminException(
          FunctionsClientErrorCode.taskAlreadyExists,
          'A task with ID ${options?.id} already exists',
        );
      }
      rethrow; // Will be caught by _functionsGuard
    }
  });
}