Skip to content

Conversation

wdbaruni
Copy link
Member

@wdbaruni wdbaruni commented Mar 5, 2025

Currently when users submit a job without specifying a count, it's not clear what happens:

  • Batch/service jobs silently fail to run as count defaults to 0, which means no executions are needed.
  • Users have to debug why their job isn't executing
  • No indication that count needs to be set

This change makes it more intuitive:

  • Batch/service jobs default to count=1 if not specified
  • Explicit count=0 is still respected (useful for stopping jobs)
  • No changes to daemon/ops jobs which run on all matching nodes

This should reduce confusion and make job submission more user-friendly, especially
for new users trying out the system.

Examples

Job will run and default to count=1

Type: batch
Tasks:
  - Name: main
    Engine:
      Type: docker
      Params:
        Image: ubuntu
        Parameters:
          - echo
          - hello

Summary by CodeRabbit

  • New Features

    • Enhanced job submission logic with improved handling of the count field, setting default and validation rules based on job types.
  • Documentation

    • Updated API and Swagger documentation to clearly describe how the count field behaves for different job scenarios.
  • Tests

    • Expanded test coverage with descriptive cases to verify proper handling, normalization, and validation of the count field.
    • Improved error message assertions for invalid field types in tests.

Copy link

linear bot commented Mar 5, 2025

Copy link
Contributor

coderabbitai bot commented Mar 5, 2025

Walkthrough

The pull request introduces a custom JSON unmarshaling method for the Job struct to handle the Count field based on job type. It updates the normalization and validation methods to enforce defaults and error conditions. The test suite is enhanced with descriptive test cases and a new function to verify JSON handling. Additionally, the Swagger documentation and JSON schema files are updated to reflect the detailed behavior of the Count field for batch, service, daemon, and ops jobs.

Changes

File(s) Change Summary
pkg/models/job.go Added a custom UnmarshalJSON method for the Job struct; modified Normalize and ValidateSubmission to enforce job type-specific rules for the Count field.
pkg/models/job_test.go Enhanced tests for job normalization, validation, and JSON handling; added descriptive test case naming and a new TestJobJSONHandling function with helper functions.
pkg/swagger/docs.go, pkg/swagger/swagger.json, webui/lib/api/schema/swagger.json Updated the description of the Count property in Swagger documentation and JSON schema to specify behavior for batch/service (default to 1 or stop on 0) and daemon/ops jobs (forced 0 with invalid values >1).
pkg/lib/marshaller/utils_test.go Modified error message assertion in test for invalid field types in UnmarshalJob function to change "Job" to "job" for case sensitivity.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant J as Job Model

    C->>J: Submit JSON payload with "Count"
    J->>J: Execute custom UnmarshalJSON
    alt Batch/Service Job
        J->>J: If Count omitted, set to 1
        J->>J: If Count explicitly 0, mark to stop executions
    else Daemon/Ops Job
        J->>J: Force Count to 0, validate errors for Count > 1
    end
    J->>C: Return processed Job instance
Loading

Suggested reviewers

  • jamlo

Poem

I'm a hopping rabbit in the code field,
With JSON tricks and tests well-yielded.
I set the count, so crisp and true,
Batch or daemon, I know what to do.
Swagger docs now sing a clearer tune,
Hoppin' for a future bright as the moon!

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

{"Issues":[{"FromLinter":"typecheck","Text":"pattern build/: no matching files found","Severity":"","SourceLines":["//go:embed build/"],"Replacement":null,"Pos":{"Filename":"webui/webui.go","Offset":0,"Line":23,"Column":12},"ExpectNoLint":false,"ExpectedNoLintLinter":""}],"Report":{"Linters":[{"Name":"asasalint"},{"Name":"asciicheck"},{"Name":"bidichk"},{"Name":"bodyclose","Enabled":true},{"Name":"canonicalheader"},{"Name":"containedctx"},{"Name":"contextcheck"},{"Name":"copyloopvar","Enabled":true},{"Name":"cyclop"},{"Name":"decorder"},{"Name":"deadcode"},{"Name":"depguard","Enabled":true},{"Name":"dogsled","Enabled":true},{"Name":"dupl"},{"Name":"dupword"},{"Name":"durationcheck"},{"Name":"errcheck","Enabled":true,"EnabledByDefault":true},{"Name":"errchkjson"},{"Name":"errname"},{"Name":"errorlint"},{"Name":"execinquery"},{"Name":"exhaustive"},{"Name":"exhaustivestruct"},{"Name":"exhaustruct"},{"Name":"exportloopref"},{"Name":"forbidigo","Enabled":true},{"Name":"forcetypeassert"},{"Name":"fatcontext"},{"Name":"funlen","Enabled":true},{"Name":"gci"},{"Name":"ginkgolinter"},{"Name":"gocheckcompilerdirectives"},{"Name":"gochecknoglobals"},{"Name":"gochecknoinits","Enabled":true},{"Name":"gochecksumtype"},{"Name":"gocognit"},{"Name":"goconst","Enabled":true},{"Name":"gocritic"},{"Name":"gocyclo","Enabled":true},{"Name":"godot"},{"Name":"godox"},{"Name":"err113"},{"Name":"gofmt","Enabled":true},{"Name":"gofumpt"},{"Name":"goheader"},{"Name":"goimports","Enabled":true},{"Name":"golint"},{"Name":"mnd","Enabled":true},{"Name":"gomnd"},{"Name":"gomoddirectives"},{"Name":"gomodguard"},{"Name":"goprintffuncname","Enabled":true},{"Name":"gosec","Enabled":true},{"Name":"gosimple","Enabled":true,"EnabledByDefault":true},{"Name":"gosmopolitan"},{"Name":"govet","Enabled":true,"EnabledByDefault":true},{"Name":"grouper"},{"Name":"ifshort"},{"Name":"iface"},{"Name":"importas"},{"Name":"inamedparam"},{"Name":"ineffassign","Enabled":true,"EnabledByDefault":true},{"Name":"interfacebloat"},{"Name":"interfacer"},{"Name":"intrange"},{"Name":"ireturn"},{"Name":"lll","Enabled":true},{"Name":"loggercheck"},{"Name":"maintidx"},{"Name":"makezero"},{"Name":"maligned"},{"Name":"mirror"},{"Name":"misspell"},{"Name":"musttag"},{"Name":"nakedret","Enabled":true},{"Name":"nestif"},{"Name":"nilerr"},{"Name":"nilnil"},{"Name":"nlreturn"},{"Name":"noctx","Enabled":true},{"Name":"nonamedreturns"},{"Name":"nosnakecase"},{"Name":"nosprintfhostport"},{"Name":"paralleltest"},{"Name":"perfsprint"},{"Name":"prealloc"},{"Name":"predeclared"},{"Name":"promlinter"},{"Name":"protogetter"},{"Name":"reassign"},{"Name":"recvcheck"},{"Name":"revive"},{"Name":"rowserrcheck"},{"Name":"sloglint"},{"Name":"scopelint"},{"Name":"sqlclosecheck"},{"Name":"spancheck"},{"Name":"staticcheck","Enabled":true,"EnabledByDefault":true},{"Name":"structcheck"},{"Name":"stylecheck","Enabled":true},{"Name":"tagalign"},{"Name":"tagliatelle"},{"Name":"tenv"},{"Name":"testableexamples"},{"Name":"testifylint"},{"Name":"testpackage"},{"Name":"thelper"},{"Name":"tparallel"},{"Name":"typecheck","Enabled":true,"EnabledByDefault":true},{"Name":"unconvert","Enabled":true},{"Name":"unparam"},{"Name":"unused","Enabled":true,"EnabledByDefault":true},{"Name":"usestdlibvars"},{"Name":"varcheck"},{"Name":"varnamelen"},{"Name":"wastedassign"},{"Name":"whitespace","Enabled":true},{"Name":"wrapcheck"},{"Name":"wsl"},{"Name":"zerologlint"},{"Name":"nolintlint","Enabled":true}]}}

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
pkg/swagger/swagger.json (1)

1699-1702: Documentation Clarity and Consistency for "Count" Field

The updated description for the "Count" property in the "models.Job" definition is clear and comprehensive. It explains that for batch and service jobs:

  • If the count is not provided in the JSON, it defaults to 1.
  • An explicit value of 0 stops all executions.
  • A value greater than 0 specifies the exact number of replicas.

It also correctly details that for daemon and ops jobs:

  • Values of 0 or 1 are ignored (i.e., the job runs on all matching nodes),
  • Values greater than 1 are invalid and will fail validation.

This update aligns well with the PR objectives to improve job submission usability and prevent silent failures when the count is omitted.

Suggestion: If the Swagger UI supports enhanced formatting, consider using markdown bullet points or line breaks in the description for improved readability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7a0d340 and 43c64b3.

📒 Files selected for processing (5)
  • pkg/models/job.go (5 hunks)
  • pkg/models/job_test.go (3 hunks)
  • pkg/swagger/docs.go (1 hunks)
  • pkg/swagger/swagger.json (1 hunks)
  • webui/lib/api/schema/swagger.json (1 hunks)
🔇 Additional comments (11)
pkg/swagger/docs.go (1)

1704-1704: Clarified job Count specifications in Swagger docs.
This updated description matches the newly introduced logic for handling the Count field across different job types.

pkg/models/job.go (5)

5-5: Approved import for JSON handling.
Importing "encoding/json" is essential for the custom UnmarshalJSON method introduced below.


103-110: Accurate and thorough Count field documentation.
These comments align well with the intended behavior for batch/service vs. daemon/ops job types.


192-195: Correct default enforcement for daemon and ops jobs.
Setting Count = 0 for these job types prevents unintended replicas and aligns with the documentation.


270-272: Validation prevents invalid replica counts for daemon/ops.
Raising an error for Count > 1 on daemon/ops jobs is consistent with the documented restrictions.


411-443: Well-structured custom JSON unmarshal for Count defaults.
This logic correctly defaults Count to 1 for batch/service when omitted, and preserves explicit zero while deferring other cases to validation.

webui/lib/api/schema/swagger.json (1)

700-701: Clear and comprehensive documentation update for the Count field.

The updated description clearly explains the behavior of the Count field for different job types, which aligns with the PR objectives of improving user experience by defaulting batch and service jobs to a count of 1 when not specified.

pkg/models/job_test.go (4)

20-106: Well-structured test cases for job normalization.

The test cases are well organized with descriptive names and cover all important scenarios for different job types:

  1. Batch and service jobs retaining their original count values (0 or positive)
  2. Ops and daemon jobs being forced to count=0 regardless of input value

Good job on refactoring the tests to be more readable and comprehensive.


108-186: Comprehensive validation test cases.

The test cases now properly validate important constraints:

  1. Negative count values are rejected
  2. Daemon jobs cannot have count > 1
  3. Ops jobs cannot have count > 1

This ensures the system enforces the rules documented in the Swagger file.


280-380: Excellent JSON handling test coverage.

This new test function is a crucial addition that verifies the JSON unmarshaling behavior which implements the core feature of this PR - defaulting to count=1 for batch and service jobs when count is omitted. It thoroughly tests:

  1. All job types (batch, service, daemon, ops)
  2. Explicit count values (0, positive numbers)
  3. Omitted count field behavior
  4. Default value application during unmarshaling

The test structure makes it easy to understand the expected behavior for each scenario.


382-385: Clean utility function implementation.

This helper function simplifies the test code by providing a concise way to create integer pointers, which are needed for the JSON marshaling tests where some fields might be omitted.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
pkg/models/job.go (1)

412-444: Good custom unmarshaling approach; consider a single-source defaulting.

The logic here correctly applies defaults for batch/service jobs and preserves explicit zero values. Since you also override Count for daemon/ops jobs in Normalize(), you may consider consolidating all defaults or overrides in one location to reduce complexity, although this is optional.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 43c64b3 and 0df09b5.

📒 Files selected for processing (2)
  • pkg/lib/marshaller/utils_test.go (1 hunks)
  • pkg/models/job.go (5 hunks)
✅ Files skipped from review due to trivial changes (1)
  • pkg/lib/marshaller/utils_test.go
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: build / Build Binary (windows, amd64)
  • GitHub Check: build / Build Binary (darwin, arm64)
  • GitHub Check: build / Build Binary (darwin, amd64)
  • GitHub Check: build / Build Binary (linux, armv6)
  • GitHub Check: build / Build Binary (linux, armv7)
  • GitHub Check: build / Build Binary (linux, arm64)
  • GitHub Check: build / Build Binary (linux, amd64)
🔇 Additional comments (4)
pkg/models/job.go (4)

5-5: No issues with new import.

The addition of encoding/json for custom unmarshaling is appropriate and consistent with the implementation that follows.


104-110: Documentation is clear and consistent.

These doc comments accurately explain the intended behavior for the Count field across different job types.


192-196: Consistent override for daemon and ops jobs.

Forcing Count to 0 at normalization ensures that daemon and ops jobs behave consistently, regardless of any user-supplied value.


270-272: Validation aligns with specification.

Restricting Count to 1 or less for daemon and ops jobs prevents invalid configurations, matching the documented behavior.

@wdbaruni wdbaruni merged commit 7c46e90 into main Mar 5, 2025
14 checks passed
@wdbaruni wdbaruni deleted the eng-554-better-behavior-on-no-count-for-batch-job branch March 5, 2025 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant