如何正确配置Luigi任务重试?

2024-10-01 15:40:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图配置Luigi的重试机制,这样失败的任务将被重试几次。但是,当任务重试成功时,Luigi退出失败:

===== Luigi Execution Summary =====

Scheduled 3 tasks of which:
* 2 ran successfully:
    - 1 FailOnceThenSucceed(path=/tmp/job-id-18.subtask)
    - 1 MasterTask(path=/tmp/job-id-18)
* 1 failed:
    - 1 FailOnceThenSucceed(path=/tmp/job-id-18.subtask)

This progress looks :( because there were failed tasks

所以问题是:我如何配置Luigi(我已经用pip install安装了2.3.3版),这样当一个任务失败一次,但又成功重试时,Luigi将使用This progress looks :)成功退出,而不是使用This progress looks :(失败?在

下面是我提出的一个最小的调度程序和worker配置,以及演示该行为的任务:

^{pr2}$

在我的任务.py公司名称:

import luigi


class FailOnceThenSucceed(luigi.Task):
    path = luigi.Parameter()

    def output(self):
        return luigi.LocalTarget(self.path)

    def run(self):
        failmarker = luigi.LocalTarget(self.path + ".hasfailedonce")
        if failmarker.exists():
            with self.output().open('w') as target:
                target.write('OK')
        else:
            with failmarker.open('w') as marker:
                marker.write('Failed')
            raise RuntimeError("Failed once")


class MasterTask(luigi.Task):
    path = luigi.Parameter()

    def requires(self):
        return FailOnceThenSucceed(path=self.path + '.subtask')

    def output(self):
        return luigi.LocalTarget(self.path)

    def run(self):
        with self.output().open('w') as target:
            target.write('OK')

示例执行:

PYTHONPATH=. luigi --module mytasks MasterTask --workers=2 --path='/tmp/job-id-18'


Tags: pathselfidtargetoutputdefjobthis

热门问题