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

Make background tasks lazy #95

Merged
merged 1 commit into from
Dec 16, 2024
Merged

Conversation

schneems
Copy link
Member

Previously when a background task was defined it would perform work on initialization, this fails if the first task is not actually executed yet:

:::>- pre.erb background.start('heroku run:inside <%= dyno_ps_name %> "bash"', wait: "$", timeout: 120, name: "heroku_run")
:::-- background.stdin_write("ls -lah", name: "heroku_run", wait: "$", timeout: 30)
:::-- background.stdin_write("exit", name: "heroku_run", wait: "exit")
:::-> background.stop(name: "heroku_run")

Gives:

/Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/code_command/background/process_spawn.rb:41:in `find': Could not find task with name "heroku_run", known task names: [] (RuntimeError)
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/code_command/background/stdin_write.rb:9:in `initialize'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc.rb:13:in `new'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc.rb:13:in `code_command_from_keyword'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/peg_parser.rb:210:in `block in <class:PegTransformer>'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `instance_eval'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `call_on_match'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:235:in `block in transform_elt'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:232:in `each'

Because the background.stdin_write call to new() happens at parse time, but the background.start call to new() happens at runtime, (Because pre.erb is the command, and it hasn't yet created the start command and pushed it onto the stack).

This change makes the lookup for background tasks lazy, so as long as the task is started by execution (runtime) of the command it will succeed.

The added test fails on main (with the error below) and passes on this branch.

  1) Error:
BackgroundTest#test_stdin_with_cat_echo:
RuntimeError: Could not find task with name "cat", known task names: ["script", "tail"]
    lib/rundoc/code_command/background/process_spawn.rb:41:in `find'
    lib/rundoc/code_command/background/stdin_write.rb:9:in `initialize'
    test/rundoc/code_commands/background_test.rb:9:in `new'
    test/rundoc/code_commands/background_test.rb:9:in `block (2 levels) in test_stdin_with_cat_echo'
    test/rundoc/code_commands/background_test.rb:6:in `chdir'
    test/rundoc/code_commands/background_test.rb:6:in `block in test_stdin_with_cat_echo'
    /Users/rschneeman/.rubies/ruby-3.3.1/lib/ruby/3.3.0/tmpdir.rb:99:in `mktmpdir'
    test/rundoc/code_commands/background_test.rb:5:in `test_stdin_with_cat_echo'

87 runs, 249 assertions, 0 failures, 1 errors, 0 skips

Previously when a background task was defined it would perform work on initialization, this fails if the first task is not actually executed yet:

```term
:::>- pre.erb background.start('heroku run:inside <%= dyno_ps_name %> "bash"', wait: "$", timeout: 120, name: "heroku_run")
:::-- background.stdin_write("ls -lah", name: "heroku_run", wait: "$", timeout: 30)
:::-- background.stdin_write("exit", name: "heroku_run", wait: "exit")
:::-> background.stop(name: "heroku_run")
```

Gives:

```
/Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/code_command/background/process_spawn.rb:41:in `find': Could not find task with name "heroku_run", known task names: [] (RuntimeError)
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/code_command/background/stdin_write.rb:9:in `initialize'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc.rb:13:in `new'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc.rb:13:in `code_command_from_keyword'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/rundoc-4.1.1/lib/rundoc/peg_parser.rb:210:in `block in <class:PegTransformer>'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `instance_eval'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:217:in `call_on_match'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:235:in `block in transform_elt'
	from /Users/rschneeman/.gem/ruby/3.3.1/gems/parslet-2.0.0/lib/parslet/transform.rb:232:in `each'
```

Because the `background.stdin_write` call to `new()` happens at parse time, but the `background.start` call to `new()` happens at runtime, (Because `pre.erb` is the command, and it hasn't yet created the start command and pushed it onto the stack).

This change makes the lookup for background tasks lazy, so as long as the task is started by execution (runtime) of the command it will succeed.

The added test fails on main (with the error below) and passes on this branch.

```
  1) Error:
BackgroundTest#test_stdin_with_cat_echo:
RuntimeError: Could not find task with name "cat", known task names: ["script", "tail"]
    lib/rundoc/code_command/background/process_spawn.rb:41:in `find'
    lib/rundoc/code_command/background/stdin_write.rb:9:in `initialize'
    test/rundoc/code_commands/background_test.rb:9:in `new'
    test/rundoc/code_commands/background_test.rb:9:in `block (2 levels) in test_stdin_with_cat_echo'
    test/rundoc/code_commands/background_test.rb:6:in `chdir'
    test/rundoc/code_commands/background_test.rb:6:in `block in test_stdin_with_cat_echo'
    /Users/rschneeman/.rubies/ruby-3.3.1/lib/ruby/3.3.0/tmpdir.rb:99:in `mktmpdir'
    test/rundoc/code_commands/background_test.rb:5:in `test_stdin_with_cat_echo'

87 runs, 249 assertions, 0 failures, 1 errors, 0 skips
```
@schneems schneems force-pushed the schneems/rundoc-pre-erb-background branch from ce33599 to 6e3cb40 Compare December 16, 2024 17:17
@schneems schneems marked this pull request as ready for review December 16, 2024 17:17
@schneems schneems merged commit 8ed2ea2 into main Dec 16, 2024
13 checks passed
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.

1 participant