Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gapic-ruby-generator): add logic to stop background process from toys gen on SIGINT #1036

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aandreassa
Copy link
Contributor

Convenience tool to stop all processes spawned via toys gen (which may live in the background)

@aandreassa aandreassa self-assigned this Jan 23, 2024
@aandreassa aandreassa changed the title feat(gapic-ruby-generator): add tool to stop background process from … feat(gapic-generator-ruby): add tool to stop background process from … Jan 23, 2024
@aandreassa aandreassa marked this pull request as ready for review January 23, 2024 01:02
@aandreassa aandreassa requested a review from a team as a code owner January 23, 2024 01:02
@aandreassa aandreassa changed the title feat(gapic-generator-ruby): add tool to stop background process from … feat(gapic-ruby-generator): add logic to stop background process from toys gen on SIGINT Jan 23, 2024
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is useful in cases where you have a broken logic in the generation step (e.g. referencing wrong variable in .erb files). Otherwise, toys gen on SIGINT cleans up correctly as is.

Copy link
Member

@dazuma dazuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this technique should be really helpful. Just have some requests:

@@ -135,6 +135,17 @@ def run
include :exec, e: true
include :terminal

Signal.trap("SIGINT") do
puts "SIGINT received. Cleaning up processes."
toys_gen_pids = `ps aux`.each_line.filter_map { |line| line.split[1].to_i if line.match?(/ruby.*protoc/)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ruby.*protoc/ seems pretty lenient. Are you confident this won't be likely to hit false positives?

@@ -135,6 +135,17 @@ def run
include :exec, e: true
include :terminal

Signal.trap("SIGINT") do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this in the implementation of toys gen itself so it won't affect any other tools? (e.g. suppose I kick off a toys gen in one terminal, so there are a bunch of generation processes running in the background, and then concurrently I run a totally unrelated toys tool in another terminal, and then interrupt it. Then this trap will kick in during the unrelated run, and clobber the generation.)

One way to do this is, rather than trapping the signal globally here, add an interrupt handler on the gen tool implementation, which is located in shared/.toys.rb. Use the on_interrupt directive.

toys_gen_pids = `ps aux`.each_line.filter_map { |line| line.split[1].to_i if line.match?(/ruby.*protoc/)}
puts "No toys gen processes running." if toys_gen_pids.empty?
toys_gen_pids.each do |pid|
puts "Terminating pid #{pid}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard Ruby style is to indent 2 spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants