使用 Kiba 实现“rescue-ensure”类功能的推荐方法?
Recommended way to achieve `rescue-ensure` kind of functionality with Kiba?
我们有一个 Kiba 管道,我们需要在作业结束后执行一些任务,无论是否有错误(整个管道不会失败,我们只是有几个验证错误或类似错误) .
文档是这么说的:
:warning: Post-processors won't get called if an error occurred before them.
https://github.com/thbar/kiba/wiki/Implementing-pre-and-post-processors
这是推荐的方法吗:
Kiba.run(
Kiba.parse do
source(...)
transform(...)
destination(....)
post_process do
# we cannot do it here, because it won't get called
end
end
)
# is this the location to do it?
Job.some_special_cleanup_task
谢谢!
PS什么意思:
Post-processors won't get called if an error occurred before them.
这是否意味着如果错误发生并且没有被挽救?
确实如记录和您指出的那样,如果出现错误,post_process
将不会被调用!
此时,最好的解决办法是使用ensure
语句的形式:
一种常见的结构方式是:
module ETL
module Job
module_function
def setup(config)
Kiba.parse do
source(...)
transform(...)
destination(....)
end
end
def some_special_cleanup_task
# ...
end
def run(job)
Kiba.run(job)
ensure
Job.some_special_cleanup_task
end
end
end
这样做可以让 always-运行 任务的代码接近 ETL 作业,这很好。
如果您的任务与作业非常独立,并且您希望鼓励作业之间的重用,您还可以创建一个通用的块形式组件:
module ETL
module Middlewares
module CommonTask
module_function
def with_common_task
yield
ensure
do_the_common_task
end
end
end
end
你会这样使用:
ETL::Middlewares::CommonTask.with_common_task do
Kiba.run(...)
end
例如,Kiba Pro FileLock 使用第二种形式。
将来,Kiba ETL 将引入一种中间件形式,使这更容易。
希望对您有所帮助,如果能正确解决您的问题,请将问题标记为已回答!
我们有一个 Kiba 管道,我们需要在作业结束后执行一些任务,无论是否有错误(整个管道不会失败,我们只是有几个验证错误或类似错误) .
文档是这么说的:
:warning: Post-processors won't get called if an error occurred before them. https://github.com/thbar/kiba/wiki/Implementing-pre-and-post-processors
这是推荐的方法吗:
Kiba.run(
Kiba.parse do
source(...)
transform(...)
destination(....)
post_process do
# we cannot do it here, because it won't get called
end
end
)
# is this the location to do it?
Job.some_special_cleanup_task
谢谢!
PS什么意思:
Post-processors won't get called if an error occurred before them.
这是否意味着如果错误发生并且没有被挽救?
确实如记录和您指出的那样,如果出现错误,post_process
将不会被调用!
此时,最好的解决办法是使用ensure
语句的形式:
一种常见的结构方式是:
module ETL
module Job
module_function
def setup(config)
Kiba.parse do
source(...)
transform(...)
destination(....)
end
end
def some_special_cleanup_task
# ...
end
def run(job)
Kiba.run(job)
ensure
Job.some_special_cleanup_task
end
end
end
这样做可以让 always-运行 任务的代码接近 ETL 作业,这很好。
如果您的任务与作业非常独立,并且您希望鼓励作业之间的重用,您还可以创建一个通用的块形式组件:
module ETL
module Middlewares
module CommonTask
module_function
def with_common_task
yield
ensure
do_the_common_task
end
end
end
end
你会这样使用:
ETL::Middlewares::CommonTask.with_common_task do
Kiba.run(...)
end
例如,Kiba Pro FileLock 使用第二种形式。
将来,Kiba ETL 将引入一种中间件形式,使这更容易。
希望对您有所帮助,如果能正确解决您的问题,请将问题标记为已回答!