cloud-run-faq icon indicating copy to clipboard operation
cloud-run-faq copied to clipboard

Using background tasks pkgs

Open yarelm opened this issue 4 years ago • 2 comments
trafficstars

Hya, Using Cloud Run (in Go for example), occasionally there is a need to use a 3rd party pkg that does some background processing. Sometimes, this processing is done out of the request context.

For example, I'm using the (https://github.com/google/go-metrics-stackdriver) pkg for sending metrics to stackdriver. It launches a goroutine that periodically sends pending metrics. This is done async to the request flow.

I understand Cloud Run can throttle the CPU to 0 when not handling requests - I am intrigued about how does that affects such use cases. In some cases, I can be OK with my periodic handling being suspended for a while. Just trying to better understand what to expect.

I know the best practice is to avoid background jobs, however, I believe in many use cases it's a requirement (I prefer to not re-write plumbing logic when I don't have to 😄 )

Cheers

yarelm avatar Sep 02 '21 07:09 yarelm

Most metrics libraries have flush() methods that can be called before container shuts down via a SIGTERM signal. Consider using that facility.

There's a feature in preview that keeps the CPU always on in the background but it has different pricing and not publicly available yet.

ahmetb avatar Sep 02 '21 15:09 ahmetb

I'm guessing that CPU throttling is different to terminating the process? But I don't see much document about what people should do in a process being throttled or "suspended". To be more specific I personally have no idea what to do with DB connection pools.

BTW the "always allocate CPU" flag is in GA now.

jokester avatar Apr 12 '23 06:04 jokester