Create App Engine task handlers

This page demonstrates how to create an App Engine task handler, the worker code that handles an App Engine task. The Cloud Tasks queue sends HTTP requests to your task handler. Upon successful completion of processing, the handler must send an HTTP status code between 200 and 299 back to the queue. Any other value indicates the task has failed and the queue retries the task.

App Engine Task Queue requests are sent from the IP address 0.1.0.2. Also refer to the IP range for requests sent to the App Engine environment.

C#

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(builder => builder.AddDebug());
        services.AddRouting();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var logger = loggerFactory.CreateLogger("testStackdriverLogging");

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // Configure error reporting service.
            app.UseExceptionHandler("/Home/Error");
        }

        var routeBuilder = new RouteBuilder(app);

        routeBuilder.MapPost("log_payload", context =>
        {
            // Log the request payload
            var reader = new StreamReader(context.Request.Body);
            var task = reader.ReadToEnd();

            logger.LogInformation($"Received task with payload: {task}");
            return context.Response.WriteAsync($"Printed task payload: {task}");
        });

        routeBuilder.MapGet("hello", context =>
        {
            // Basic index to verify app is serving
            return context.Response.WriteAsync("Hello, world!");
        });

        routeBuilder.MapGet("_ah/health", context =>
        {
            // Respond to GAE health-checks
            return context.Response.WriteAsync("OK");
        });

        routeBuilder.MapGet("/", context =>
        {
            return context.Response.WriteAsync("Hello, world!");
        });

        var routes = routeBuilder.Build();
        app.UseRouter(routes);
    }
}

Go


// Sample task_handler is an App Engine app demonstrating Cloud Tasks handling.