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

Add additional check if task already run for report scheduler #3900

Merged
merged 5 commits into from
Nov 28, 2024

Conversation

jpbruinsslot
Copy link
Contributor

@jpbruinsslot jpbruinsslot commented Nov 27, 2024

Changes

Since we're allowing scheduled tasks that have a single recurrence. We need to check if the task is already on the queue and we need to check if the task has been executed before when there isn't a cron expression is specified. This PR addsthis additional check if task already run for the report scheduler.

QA notes

With #3840 check if you can schedule different type of reporting jobs with different recurrence and make sure that it executes tasks as expected.


Code Checklist

  • All the commits in this PR are properly PGP-signed and verified.
  • This PR only contains functionality relevant to the issue.
  • I have written unit tests for the changes or fixes I made.
  • I have checked the documentation and made changes where necessary.
  • I have performed a self-review of my code and refactored it to the best of my abilities.
  • Tickets have been created for newly discovered issues.
  • For any non-trivial functionality, I have added integration and/or end-to-end tests.
  • I have informed others of any required .env changes files if required and changed the .env-dist accordingly.
  • I have included comments in the code to elaborate on what is not self-evident from the code itself, including references to issues and discussions online, or implicit behavior of an interface.

Checklist for code reviewers:

Copy-paste the checklist from the docs/source/templates folder into your comment.


Checklist for QA:

Copy-paste the checklist from the docs/source/templates folder into your comment.

@jpbruinsslot jpbruinsslot added the mula Issues related to the scheduler label Nov 27, 2024
@jpbruinsslot jpbruinsslot self-assigned this Nov 27, 2024
@jpbruinsslot jpbruinsslot requested a review from a team as a code owner November 27, 2024 15:50
Comment on lines 90 to 113

# When the schedule has no schedule, but a task is
# already executed we should not push the task again
task_db = None
try:
task_db = self.ctx.datastores.task_store.get_latest_task_by_hash(report_task.hash)
if task_db and schedule.schedule is None:
self.logger.debug(
"Schedule has no schedule, but task already executed",
schedule_id=schedule.id,
scheduler_id=self.scheduler_id,
organisation_id=self.organisation.id,
)
continue
except storage.errors.StorageError as exc_db:
self.logger.error(
"Could not get latest task by hash %s",
report_task.hash,
scheduler_id=self.scheduler_id,
organisation_id=self.organisation.id,
exc_info=exc_db,
)
continue

Copy link
Contributor Author

@jpbruinsslot jpbruinsslot Nov 27, 2024

Choose a reason for hiding this comment

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

For now I've opted to check if there is already a task available for the schedule. I want to optimize this a bit more by having 2 separate queries (and combine the results to one) of schedules that have no cron expression and no tasks associated, and schedules that have a cron expression. Since for those two cases we need to create report task.

However since the PR #3840 is time-sensitive I've opted for this change atm. I'll make a ticket for this optimalisation and implement that so that it can be merged at a later time.

@Rieven
Copy link
Contributor

Rieven commented Nov 28, 2024

Checklist for QA:

  • I have checked out this branch, and successfully ran a fresh make reset.
  • I confirmed that there are no unintended functional regressions in this branch:
    • I have managed to pass the onboarding flow
    • Objects and Findings are created properly
    • Tasks are created and completed properly
  • I confirmed that the PR's advertised feature or hotfix works as intended.
  • I checked the logs for errors and/or warnings and made issues where necessary

What works:

It works

@underdarknl
Copy link
Contributor

How does this handle 'reruns' that are use initiated?

@jpbruinsslot
Copy link
Contributor Author

How does this handle 'reruns' that are use initiated?

I've update the code to check if tasks have run for that particular schedule. Meaning if a user wants a rerun a new scheduled run should be created.

@minvws minvws deleted a comment from sonarcloud bot Nov 28, 2024
Copy link

sonarcloud bot commented Nov 28, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
6.2% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@underdarknl underdarknl merged commit c86325a into main Nov 28, 2024
20 of 22 checks passed
@underdarknl underdarknl deleted the fix/mula/schedule-report-once-check branch November 28, 2024 14:10
jpbruinsslot added a commit that referenced this pull request Dec 2, 2024
* main: (64 commits)
  Bug fix: KAT-alogus parameter is now organization member instead of organization code (#3895)
  Remove sigrid workflows (#3920)
  Updated packages (#3898)
  Fix mula migrations Debian package (#3919)
  Adds loggers to report flow (#3872)
  Add additional check if task already run for report scheduler (#3900)
  Create separate finding for Microsoft RDP port (#3882)
  fix: 🐛 allow boefje completion with 404 (#3893)
  Feature/improve rename bulk modal (#3885)
  Update scheduler folder structure (#3883)
  Translations update from Hosted Weblate (#3870)
  Increase max number of PostgreSQL connections (#3889)
  Fix for task id as valid UUID (#3744)
  Add `auto_calculate_deadline` attribute to Scheduler (#3869)
  Ignore specific url parameters when following location headers (#3856)
  Let mailserver inherit l1 (#3704)
  Change plugins enabling in report flow to checkboxes (#3747)
  Fix rocky katalogus tests and delete unused fixtures (#3884)
  Enable/disable scheduled reports (#3871)
  optimize locking in katalogus.py, reuse available data (#3752)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mula Issues related to the scheduler
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants