Tools & Services

Long-Running Asynchronous Processes with Netlify Background Functions

Tools & Services

Long-Running Asynchronous Processes with Netlify Background Functions

Throughout December we'll be highlighting a different Netlify feature each day. It might just be the thing you need to unlock those creative juices, and dust off that domain you registered but never deployed! Keep an eye on the blog and on Twitter for each feature!

Ain't no serverless functions got time for that! If you noticed, that was a double negative because there is a serverless function that DOES have time for your long-running tasks: Netlify Background Functions. You can write these serverless functions in JavaScript, Typescript, or Go and like regular Netlify Functions, background functions are version-controlled, built, and deployed along with the rest of your Netlify site.

Here's a little sample Background Function in Go that you can try to see a function running longer than 10 seconds:

package main

import (
    "context"
    "log"
    "time"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) {
    for i := 0; i < 60; i++ {
        log.Println(i)
        time.Sleep(1 * time.Second)
    }
    return nil, nil
}

func main() {
    lambda.Start(handler)
}

You can check out some more detailed examples:

What exactly happens when you use a Background Function? Here are the details about the invocation and responses:

When a function is invoked asynchronously, there is an initial 202 success response. If function execution returns an error, an execution retry happens after one minute. If it fails again, another retry happens two minutes later. When a background function is successfully executed, you generally pass the result to a destination other than the originating client.

Background Functions are currently in beta but try 'em out, and take your time ;} Happy coding 👩🏻‍💻!

Resources for the Road

Keep reading

Recent posts

Book cover with the title Deliver web project 10 times faster with Jamstack enterprise

Deliver web projects 10× faster

Get the whitepaper