-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto resume failed but recoverable tasks (#67)
* feat: auto resume failed but recoverable tasks * fix: make program not crash even the data is wrong * feat: resume is actually schedule_resume * test: runresume command
- Loading branch information
Showing
7 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import logging | ||
from time import sleep | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from task.models import Task | ||
|
||
logger = logging.getLogger("runresume") | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "resume failed but recoverable tasks." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--once", | ||
action="store_true", | ||
help="resume all failed tasks once and exit immediately.", | ||
) | ||
|
||
def resume_once(self): | ||
for task in Task.filter_failed(): | ||
if not task.recoverable: | ||
continue | ||
if task.retry_times >= settings.RETRY_TIMES_LIMIT: | ||
continue | ||
task.schedule_resume() | ||
|
||
def handle(self, *args, **options): | ||
logger.info("auto resume failed but recoverable tasks.") | ||
while True: | ||
self.resume_once() | ||
if options["once"]: | ||
logger.info("auto resume tasks once and exit now.") | ||
return | ||
sleep(settings.RUNNER_SLEEP_SECONDS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.conf import settings | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from task.management.commands.runresume import Command as ResumeCommand | ||
from task.models import Task | ||
|
||
|
||
class ResumeCommandTest(TestCase): | ||
def setUp(self): | ||
self.task = Task.objects.create(shared_id="foo", shared_password="foo") | ||
self.task.status = Task.Status.SAMPLING_DOWNLOADED | ||
self.task.started_at = self.task.created_at | ||
self.task.transfer_completed_at = self.task.created_at | ||
self.task.sample_downloaded_at = self.task.created_at | ||
self.task.failed = True | ||
self.task.message = "BaiduPCS._request" | ||
self.task.save() | ||
|
||
def test_resume(self): | ||
assert self.task.retry_times == 0 | ||
|
||
ResumeCommand().resume_once() | ||
|
||
task = Task.objects.get(pk=self.task.id) | ||
assert task.status == Task.Status.TRANSFERRED | ||
assert not task.failed | ||
assert task.retry_times == 1 | ||
|
||
def test_resume_not_recoverable_task(self): | ||
self.task.message = "error_code: 105," | ||
self.task.save() | ||
assert self.task.retry_times == 0 | ||
|
||
ResumeCommand().resume_once() | ||
|
||
task = Task.objects.get(pk=self.task.id) | ||
assert task.retry_times == 0 | ||
assert task.failed | ||
assert task.status == Task.Status.SAMPLING_DOWNLOADED | ||
|
||
def test_resume_too_many_times(self): | ||
self.task.retry_times = settings.RETRY_TIMES_LIMIT + 1 | ||
self.task.save() | ||
assert self.task.retry_times == settings.RETRY_TIMES_LIMIT + 1 | ||
assert self.task.get_resume_method_name() == "restart_downloading" | ||
|
||
ResumeCommand().resume_once() | ||
|
||
task = Task.objects.get(pk=self.task.id) | ||
assert task.retry_times == settings.RETRY_TIMES_LIMIT + 1 | ||
assert task.get_resume_method_name() == "restart_downloading" | ||
assert task.failed | ||
assert task.status == Task.Status.SAMPLING_DOWNLOADED | ||
|
||
def test_run_command(self): | ||
assert self.task.retry_times == 0 | ||
|
||
call_command("runresume", "--once") | ||
|
||
task = Task.objects.get(pk=self.task.id) | ||
assert task.status == Task.Status.TRANSFERRED | ||
assert not task.failed | ||
assert task.retry_times == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters