在rails中使用workling处理45k数据库记录而不影响服务器性能
在我的 Rails 应用程序中,我使用工作进程每 6 小时扫描一次 45k 数据库记录,并在满足特定条件时发送邮件。这会导致工作进程处理时服务器 CPU/负载出现峰值。结果,其他服务器请求的性能受到影响。我尝试使用 find_in_batch 一次检索 1000 条记录并进行处理。但CPU利用率仍处于峰值水平。我看不出有什么大的区别。有什么办法可以处理这个问题,这样CPU利用率就不会达到最大限制吗?
In my rails app, I use a worker process to scan 45k database records once in 6 hours and send out mails if certain condition is met. This causes the server CPU/Load to spike when the worker is processing. As a result of which other server request gets a performance hit. I tried using find_in_batch to retrieve 1000 records at a time and do the processing. But the CPU utilization is still at the peak level. No big difference i was able to see. Is there any way to handle this , so the CPU utilization doesn't hit the max limit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您通过 cron 和 script/runner 调用该作业。您可以尝试使用
nice
降低进程的优先级:I assume you're invoking the job via cron and
script/runner
. You might try lowering the priority of process withnice
:使用
nice
调整进程优先级是一种方法,但另一种方法是告诉您的应用现在放松一下,然后使用sleep
或 < code>select 命令:select
调用将阻塞一小段时间,从而允许系统上的其他任务自由运行。该值设置得越高,作业运行速度越慢,但对 CPU 负载水平的影响越小。Fiddling with the process priority level using
nice
is one way to do it, but another is to tell your app to chill out a little bit now and then using thesleep
orselect
command:The
select
call will block for a brief period of time allowing other tasks on the system to run freely. The higher you set this value the slower your job will run, but the lower the impact on the CPU load level.