ahmetb / cloud-run-faq

Unofficial FAQ and everything you've been wondering about Google Cloud Run.
https://cloud.run
Creative Commons Attribution 4.0 International
2.32k stars 124 forks source link

Using background tasks pkgs #158

Open yarelm opened 3 years ago

yarelm commented 3 years ago

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

ahmetb commented 3 years ago

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.

jokester commented 1 year ago

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.