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

Allow hooking job failure for generic error handling #205

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions sisyphus/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ def worker_wrapper(job, task_name, call):
return call


def on_job_failure(job):
"""
Job failure hook.

Can be used for generic job-independent error monitoring, handling or retry
logic.

Sispyhus will call this function w/ the job instance if the job enters the
failure state. The callback itself is then responsible for any retry logic,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
failure state. The callback itself is then responsible for any retry logic,
error state. The callback itself is then responsible for any retry logic,

I think it is called "error" everywhere else. cf. also "interrupted_resumable" and "interrupted_non_resumable" which I would also name failures but they are not handled here.

Maybe the function name should be renamed as well.

realized by e.g. analyzing the job log file and removing error files in the job
directory as needed.

Do:
- use with caution
- ensure you don't build infinite retry loops
- limit to specific use cases (e.g. local disk full, GPU broken, etc.)
"""
pass


def update_engine_rqmt(last_rqmt: Dict, last_usage: Dict):
"""Update requirements after a job got interrupted, double limits if needed

Expand Down
15 changes: 14 additions & 1 deletion sisyphus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import threading
import time
from typing import TYPE_CHECKING, Dict, List
NeoLegends marked this conversation as resolved.
Show resolved Hide resolved
import warnings

from multiprocessing.pool import ThreadPool
Expand All @@ -15,6 +16,9 @@
from sisyphus.tools import finished_results_cache
import sisyphus.global_settings as gs

if TYPE_CHECKING:
from sisyphus.job import Job

NeoLegends marked this conversation as resolved.
Show resolved Hide resolved

class JobCleaner(threading.Thread):
"""Thread to scan all jobs and clean if needed"""
Expand Down Expand Up @@ -572,6 +576,12 @@ def maybe_clear_state(state, always_clear, action):
self.job_cleaner.start()
return True

def handle_job_failure(self, prev_jobs: Dict[str, List[Job]], cur_jobs: Dict[str, List[Job]]):
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a short doc string here as well.

prev_jobs = set(prev_jobs.get(gs.STATE_ERROR, []))
for job in cur_jobs.get(gs.STATE_ERROR, []):
if job not in prev_jobs:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible that this line should be if job not in prev_jobs.get(gs.STATE_ERROR, []):?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think overwriting that variable is just confusing and not what I intended. I've since changed the var names, so it should be clearer now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed that, but yes it's better to use a different name.

gs.on_job_failure(job)

Copy link
Contributor

Choose a reason for hiding this comment

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

would you call self.update_jobs() after finishing the loop to account for changes in the state of jobs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed, since this will loop around immediately when the processing is done.

@tools.default_handle_exception_interrupt_main_thread
def run(self):
if not self.startup():
Expand All @@ -593,7 +603,9 @@ def run(self):
self.check_output(write_output=self.link_outputs)

config_manager.continue_readers()
self.update_jobs()

NeoLegends marked this conversation as resolved.
Show resolved Hide resolved
prev_jobs = self.jobs
cur_jobs = self.update_jobs()
Copy link
Contributor

Choose a reason for hiding this comment

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

How will this interact with the block directly below. self.clear_states removes some errors, call self.update_jobs() and then cycle the outer loop again. This would mess up the logic to detect new jobs in error state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is ok, the loop cycles around if jobs were cleared before any of the logic is run.


if gs.CLEAR_ERROR or self.clear_errors_once:
self.clear_errors_once = False
Expand All @@ -619,6 +631,7 @@ def run(self):
self.setup_holded_jobs()
self.resume_jobs()
self.run_jobs()
self.handle_job_failure(prev_jobs, cur_jobs)

# Stop config reader
config_manager.cancel_all_reader()
Expand Down
Loading