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

Support Tasks with Params with CLI #428

Merged
merged 1 commit into from
Jun 3, 2021

Conversation

adrianna-chang-shopify
Copy link
Contributor

@adrianna-chang-shopify adrianna-chang-shopify commented Jun 2, 2021

Follow up to #413

The final piece of supporting Tasks with custom parameters in the gem - this allows arguments to be specified via the CLI using an --arguments flag.

As discussed here, the use of Strong Parameters in TasksController to permit only keys matching the Task's attributes is a bit problematic because it results in a 500 instead of alerting the user. We also wanted to apply this same behaviour to the CLI. As such, I moved the check on the argument keys out of the controller and into task.rb. However, I'm not sure if this is necessary - we could just allow ActiveModel::UnknownAttributeError to be raised on assign_attributes and let that bubble up. Let me know if you think we're being overly defensive here!

🎩 Instructions

  • cd test/dummy; bundle exec maintenance_tasks perform Maintenance::ParamsTask --arguments=post_ids:"1,2,3"
    Should see the Task successfully enqueued. To see the Task perform successfully, you can change the Active Job queue adapter to be inline.
  • cd test/dummy; bundle exec maintenance_tasks perform Maintenance::ParamsTask --arguments=post_ids:"xyz"
    Should see Validation failed: Arguments are invalid: :post_ids is invalid

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
app/controllers/maintenance_tasks/tasks_controller.rb Outdated Show resolved Hide resolved
app/models/maintenance_tasks/run.rb Outdated Show resolved Hide resolved
app/models/maintenance_tasks/run.rb Outdated Show resolved Hide resolved
lib/maintenance_tasks/cli.rb Outdated Show resolved Hide resolved
option, passing arguments as a set of <key>:<value> pairs:

```bash
$ bundle exec maintenance_tasks perform Maintenance::ParamsTask --arguments post_ids:1,2,3 content:"Hello, World!"
Copy link
Member

Choose a reason for hiding this comment

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

I'd love if the example Task in the README match the CLI call here and the task in the dummy app, but I can look into this later, it doesn't need to be in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, let's revisit this later. I'd like to ship custom value types in some form (even if not exactly what's in #414), so we can circle back to this example.

README.md Outdated Show resolved Hide resolved
app/models/maintenance_tasks/run.rb Outdated Show resolved Hide resolved
Copy link
Member

@etiennebarrie etiennebarrie left a comment

Choose a reason for hiding this comment

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

We may want to fix the exception in arguments_match_task_attributes even if it doesn't impact anything too much, example repro:

diff --git i/test/models/maintenance_tasks/run_test.rb w/test/models/maintenance_tasks/run_test.rb
index 1d0449e..23de80f 100644
--- i/test/models/maintenance_tasks/run_test.rb
+++ w/test/models/maintenance_tasks/run_test.rb
@@ -6,3 +6,3 @@ class RunTest < ActiveSupport::TestCase
     test "invalid if the task doesn't exist" do
-      run = Run.new(task_name: "Maintenance::DoesNotExist")
+      run = Run.new(task_name: "Maintenance::DoesNotExist", arguments: { a: 0 })
       refute_predicate run, :valid?

Other feedback but feel free to ignore and 🚢

@adrianna-chang-shopify adrianna-chang-shopify merged commit 541c637 into main Jun 3, 2021
@adrianna-chang-shopify adrianna-chang-shopify deleted the params-cli-support branch June 3, 2021 18:24
lawrencewong pushed a commit to lawrencewong/maintenance_tasks that referenced this pull request Apr 29, 2023
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.

3 participants