Skip to content

Cancel running jobs by event rather than previous commit SHA#3076

Open
nforro wants to merge 1 commit intopackit:mainfrom
nforro:canceling
Open

Cancel running jobs by event rather than previous commit SHA#3076
nforro wants to merge 1 commit intopackit:mainfrom
nforro:canceling

Conversation

@nforro
Copy link
Member

@nforro nforro commented Mar 24, 2026

Some triggers such as comment commands have no relation to a commit SHA, so canceling doesn't work for them. We can use project event instead to identify running jobs that should be canceled, but event IDs are reused and we need to exclude current jobs, i.e. those that have been started by our trigger. To make that possible, before starting any tasks for a job record datetime of the most recent pipeline of the triggering event and use that as a cutoff time when filtering running jobs to cancel.

TODO:

  • Write new tests or update the old ones to cover new functionality.

Related to #3005.

RELEASE NOTES BEGIN

Packit now cancels running jobs even after manual retriggering.

RELEASE NOTES END

Some triggers such as comment commands have no relation to a commit SHA,
so canceling doesn't work for them. We can use project event instead to
identify running jobs that should be canceled, but event IDs are reused
and we need to exclude current jobs, i.e. those that have been started
by our trigger. To make that possible, before starting any tasks
for a job record datetime of the most recent pipeline
of the triggering event and use that as a cutoff time when filtering
running jobs to cancel.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces a cancel_cutoff_time mechanism to prevent accidental cancellation of newly triggered jobs within the same event batch. This involves adding cancel_cutoff_time to Event and EventData classes, and modifying get_running methods across various build and test models (Copr, Koji, Testing Farm, Log Detective) to filter jobs based on this new timestamp and project_event_type/event_id instead of commit_sha. A new get_latest_datetime_for_event method was added to PipelineModel to determine this cutoff time. Review comments highlight the need for robust checks to ensure cancel_cutoff_time and db_project_event are not None to prevent unintended job cancellations and to improve docstring clarity for the created_before parameter in get_running methods.

Comment on lines +1027 to +1031
yield from CoprBuildGroupModel.get_running(
project_event_type=self.db_project_event.type,
event_id=self.db_project_event.event_id,
created_before=self.metadata.cancel_cutoff_time,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

It's important to ensure that self.db_project_event and self.metadata.cancel_cutoff_time are always properly populated. If these values are None, the query will return all running jobs for the given project event, potentially canceling unrelated jobs. Add a check to ensure these values are not None and log a warning if they are.

Comment on lines 2197 to +2203
@classmethod
def get_running(cls, commit_sha: str) -> Iterable["CoprBuildTargetModel"]:
"""Get list of currently running Copr builds matching the passed
arguments.
def get_running(
cls,
project_event_type: ProjectEventModelType,
event_id: int,
created_before: Optional[datetime] = None,
) -> Iterable["CoprBuildTargetModel"]:
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The docstring should mention that created_before is inclusive, i.e., 'at or before'. This clarifies the behavior for users of this method.

Comment on lines +856 to +860
if self.event.db_project_event:
self.event.cancel_cutoff_time = PipelineModel.get_latest_datetime_for_event(
project_event_type=self.event.db_project_event.type,
event_id=self.event.db_project_event.event_id,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a check to ensure that self.event.db_project_event is not None before attempting to retrieve the cancel_cutoff_time. If it's None, it might indicate an issue with the event processing, and logging a warning would be beneficial.

@centosinfra-prod-github-app
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants