我怎样才能创建一个脚本,在失败的情况下从它停止的地方开始? [英] How can I create a script that picks up where it left off in case of failure?

查看:27
本文介绍了我怎样才能创建一个脚本,在失败的情况下从它停止的地方开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个在 AWS 中自动创建大量资源的脚本(以及一些其他任务),但是开发和测试非常耗时,因为我只能一直走到最后让脚本失败,然后我必须更正错误并再次运行脚本.

I'm working on a script automating the creation of a number of resources in AWS (in addition to some other tasks), but it is very time consuming to develop and test because I can go all the way to the end only to have the script fail, and then I have to correct the error and run the script again.

是否有任何 shell 脚本工具(unix 或 ruby​​ 甚至 Windows)可以在发生故障时在中间启动脚本?也许有什么方法可以让 rake 在没有所有依赖任务的情况下运行任务?

Are there any shell scripting tools (unix or ruby or even Windows) where I can start the script in the middle in the event of a failure? Maybe there is some way to get rake to run a task without all the dependent tasks?

更新:

澄清一下,我不是在寻找在调用之间具有持久状态的任何复杂的东西;我说的是编写一个简单的 shell 脚本,其中可能有几十个连续步骤,每个步骤都需要很长时间.换句话说,如果脚本在第 10 步出现故障,我想在第 10 步再次启动脚本,而不必重新运行之前的所有步骤.

To clarify, I'm not looking for anything sophisticated with persisting state between invocations; I'm talking about writing a simple shell script with maybe a few dozen sequential steps that each take a long time. In other words, if the script blows up on step 10, I want to start the script again on step 10 without having to rerun all of the previous steps.

推荐答案

你需要某种方式在你前进的过程中保持你的进度状态,以便你知道从哪里重新开始.

You need someway to persist your state of progress as you go, so you know where to start again.

不过,这种坚持可能是一百万件事情.它可以是您连接的数据库、您写入的文件、内存缓存服务器.

That persistence could be a million things, though. It could be a database you connect to, a file you write to, a memcached server.

所以这真的取决于你在做什么,你需要记录什么数据,以及你可以访问什么数据持久性.

So it really depends on what you are doing, what data you need to record, and what data persistence you have access to.

如果你有一个数据库来存储记录,这就像这样简单:

If you had a database to store records in, this would be as simple as somehting like:

MyThing.where(uploaded: false).each do |thing|
  thing.upload!
  thing.update_attribute uploaded: true
end

如果中途失败,它会从上次中断的地方继续,因为它正在标记完整的记录.

If that fails half way through, it picks up where it left off because it's marking the complete records as it goes.

这篇关于我怎样才能创建一个脚本,在失败的情况下从它停止的地方开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆