brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.65k stars 114 forks source link

Wait for queues to be idle. #251

Closed metaskills closed 2 years ago

metaskills commented 2 years ago

Followup from discussion in #250. Some background on this feature. At Custom Ink we are exploring migrating our "messaging" and "jobs" pods from K8s to Lambda Containers. We can do this using the same ECR images in our deploy pipeline leveraging internal tools and perhaps one day Amazon Controller for Kubernetes (ACK). Sounds complicated but at the end of the day we use the same ECR images on Lambda by changing the ENTRYPOINT and COMMAND. The result would be Rails application processes that respond to AMQP/EventBridge events and/or Sidekiq style jobs using managed AWS Resources and scale to 0 Compute without writing custom software or changing code per-runtime.

There are a few gotchas. Because AWS Lambda freezes the execution freezes the execution environment between requests, background processing by long running processes typically break down. A good example is NewRelic. Thankfully, they have an interface to flush the metric data. For Rails projects that are 100% on AWS Lambda, we use a SuckerPunch inspired gem called LambdaPunch. https://github.com/customink/lambda_punch

Would LambdaPunch work here? It totally could. But it would be another extension we would have to install in the container and require a bit more orchestration in things like our Rollbar config. It would also require a lot of event handling delegation code to to pass down the Lambda context. Ideally, it would be awesome if SuckerPunch had a flush/wait interface similar to NewRelic. So the proposed change would appear like this to us.

class MyJob < ApplicationJob
  after_perform do |_job| 
    NewRelic::Agent.agent.flush_pipe_data
    SuckerPunch::Queue.wait
  end

  def perform(event)
    # ...
  end
end

More On The Topic

In case you are curious, here is one open source project that leans into a mantra I call (container code need not care where containers are running) that helps AWS Lambda mimic encrypted environment variables using AWS Systems Manager which K8s secrets manager does.

https://github.com/customink/crypteia

This helps ensure container portability with no code changes and leans into twelve factor. This is not directly related to the pull request but thought it might be helpful to share since we are asking for an interface that SuckerPunch (free puppy) vs implementing an internal gem or per-project freedom patches. I hope this context is helpful.

Pull Request Next Steps

I did not see any tests for shutdown_all so added none here. I would totally be willing to add some tho using your feedback? Also, is the method name right? Should it be wait_all or something different?

brandonhilkert commented 2 years ago

Looks good to me. Let's:

metaskills commented 2 years ago

Cool, I just got back from a few days off. Would you like me to do any of that work listed above in this PR or followup ones?

metaskills commented 2 years ago

I saw this test head failure but I think main is failing already? https://github.com/brandonhilkert/sucker_punch/runs/8102223916?check_suite_focus=true

brandonhilkert commented 2 years ago

If you could all posted, that’d be really helpful. I’ve fixed master (that’s where the test failure came from), so pulling master in should do the trick.

metaskills commented 2 years ago

WOW, I really love how GitHub makes that so easy. Just pushed a change with main up to date.

metaskills commented 2 years ago

Sorry, I misunderstood your request I just checked these boxes.

What do you think of the new Queue Class Methods section in the README? Felt like an opportunity to document all those. I could change those around, format, text, etc. Lemme know and again, thank you.

brandonhilkert commented 2 years ago

3.1.0 has been released. Thanks!

metaskills commented 2 years ago

Thank you! Tested this out today in production. Things look to be working as expected. Really appreciate it.

brandonhilkert commented 2 years ago

No worries. Glad it helped!