在生产环境中如何部署一个定时任务脚本?

huangapple go评论87阅读模式
英文:

how do you deploy a cron script in production?

问题

我想编写一个脚本,用于在一天中安排各种任务。不幸的是,每天要执行的任务数量超过100个,接近500个,并且未来可能达到10,000个。

所有的任务都是独立的,可以将我的脚本视为为注册并希望我为他们安排任务的终端用户提供服务。所以,如果有5个人注册,A想让我在早上9点给他发送电子邮件,而B可能希望我在晚上10点30分查询一个API等等。

概念上,我计划拥有一个数据库,告诉我每个人的任务是什么,他们要求在什么时间安排这个任务以及频率。每天一次,我将从数据库中获取这些数据,以便我有一个最新的记录,知道所有需要在当天执行的任务。

通过循环运行它们,我可以创建通道,为每个任务执行计时器或定时器。

我的问题是,如何将其部署到生产环境中,例如Google App Engine?因为这些平台是用于Web服务器的,我不确定这将如何工作...或者我应该使用Google Compute Engine,并让其作为24小时的计算?Google Compute Engine能够进行HTTP调用吗?

另外,如果我必须拥有500个在Go中打开的通道,每天持续24小时,这是否意味着在Google App Engine中有500个容器?我想这将非常快速地变得非常昂贵,尽管实际上是一个成本很低的产品。

所以,问题再次回到,如何在生产环境中部署cron脚本?

非常感谢任何帮助或指导,因为我已经做了很多搜索,不幸的是,所有的结果都指向了Google App Engine中有100个任务限制的cron调度程序...

英文:

i would like to write a script that schedules various things throughout the day. unfortunately it will do > 100 different tasks a day, closer to 500 and could be up to 10,000 in the future.

All the tasks are independent in that you can think of my script as a service for end users who sign up and want me to schedule a task for them. so if 5 ppl sign up and person A wants me to send them an email at 9 am, this will be different than person B who might want me to query an api at 10:30 pm etc.

now, conceptually I plan to have a database that tells me what each persons task will be and what time they asked to schedule that task and the frequency. once a day I will get this data from my database so I have an up-to-date record of all the tasks that need to be executed in the day

running them through a loop I can create channels that can execute timers or tickers for each task.

the question I have is how does this get deployed in production to, for example google app engine? since those platforms are for Web servers I'm not sure how this would work...Or am I supposed to use Google Compute Engine and have it act as a computation for 24 hours? Can google compute engine even make http calls?

also if I have to have say 500 channels in go open 24 hrs a day, does that count as 500 containers in google app engine? I imagine that will get very costly quickly, despite what is essentially a very low cost product.

so again the question comes back to, how does a cron script get deployed in production?

any help or guidance will be greatly appreciated as I have done a lot of googling and unfortunately everything leads back to a cron scheduler that has a limit of 100 tasks in google app engine...

答案1

得分: 1

关于GAE上的cron操作的详细信息可以在这里找到。

从您的角度来看,棘手的部分是cron配置的更新是从应用程序外部进行的,因此根据您的应用程序用户的操作自定义cron作业至少是困难的(如果不是不可能的)。

但是,可以只运行一个通用的cron作业(例如每分钟运行一次),并且该作业的处理程序可以读取用户的自定义作业配置,并进一步生成相应的任务来处理它们。通常每天运行约10,000个任务通常不是问题,它们甚至可能适合在免费应用配额内(具体取决于任务实际执行的操作)。

相同的技术可以应用于常规的Linux操作系统(包括在GCE VM上)。我还没有使用过GCE,所以无法确定如何在其中实现动态更新的cron。

英文:

Details about cron operation on GAE can be found here.

The tricky portion from your prospective is that updating the cron configuration is done from outside the application, so it's at least difficult (if not impossible) to customize the cron jobs based on your app user's actions.

It is however possible to just run a generic cron job (once a minute, for example) and have that job's handler read the users' custom job configs and further generate tasks accordingly to handle them. Running ~10K tasks per day is usually not an issue, they might even fit inside the free app quotas (depending on what the tasks are actually doing).

The same technique can be applied on a regular Linux OS (including on a GCE VM). I didn't yet use GCE, so I can't tell exactly if/how would a dynamically updated cron be possible with it.

答案2

得分: 0

你只需要一个cron job来满足你的需求。这个cron job可以每30分钟运行一次,或者每天运行一次。它会查看接下来的一段时间内需要完成的任务,并创建这些任务并将其添加到队列中。

所有这些都可以由一个单独的App Engine实例完成。你需要执行任务的实例数量当然取决于每个任务运行的时间长短。你可以对任务队列的运行有很多控制权。

英文:

You only need one cron job for your requirements. This cron job can run every 30 minutes - or once per day. It will see what has to be done over the next period of time, create tasks to do it, and add these tasks to the queue.

It can all be done by a single App Engine instance. The number of instances you need to execute your tasks depends, of course, on how long each task runs. You have a lot of control over running the task queue.

huangapple
  • 本文由 发表于 2016年4月5日 10:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/36416364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定