简体   繁体   English

如何调试生产Rails应用程序?

[英]How to debug a production Rails app?

I have been assigned to find a bug in the dashboard of our (production) order management app. 我被分配到我们(生产)订单管理应用程序的仪表板中找到一个错误。 It shows wrong data. 它显示错误的数据。 As I don't have that particular data locally, and can't get it soon, I will have to do it in the production app. 由于我本地没有特定的数据,并且很快就无法获得,我将不得不在生产应用程序中执行此操作。

I have never worked with or in an app that is running live before and have no idea about best practices, no-gos, security precautions etc. 我从未使用过或在以前运行的应用程序中工作,并且不了解最佳实践,非gos,安全预防措施等。

If someone could provide some clues to where to start reading (Software, Tools, Tutorials, ...) that would help me a lot. 如果有人可以提供一些线索,从哪里开始阅读(软件,工具,教程......),这将对我有很大的帮助。 For some reason I only find info on how to prepare for and deploy Rails apps. 出于某种原因,我只找到有关如何准备和部署Rails应用程序的信息。 But not about how to actually manage a running app (that has been deployed by someone else). 但不是关于如何实际管理正在运行的应用程序(已由其他人部署)。

An example: As I needed to compare the stats with the real data, I wanted to run a console ( rails console --sandbox ) and have a look around, fire some queries. 一个例子:由于我需要将统计数据与实际数据进行比较,我想运行一个控制台( rails console --sandbox )并浏览一下,发出一些查询。 I tried: 我试过了:

  • ssh into to the server which runs the app ssh到运行应用程序的服务器
  • change into the rails app folder ( www/project-name/current ) 转到rails app文件夹( www/project-name/current
  • run rails console production 运行rails console production

But that last command gives me: 但最后一个命令给了我:

The program 'rails' can be found in the following packages:
* ruby-railties-3.2
* ruby-railties-4.0

So I guess I am not supposed to do that. 所以我想我不应该这样做。 How would I investigate such an error then? 那我怎么调查这样的错误呢? Querying the database directly seems dangerous. 直接查询数据库似乎很危险。

Any tips? 有小费吗? THX! 谢谢!

As written by @ahmed-samir-shahin you need to use bundler to fetch the current ruby environment. 正如@ ahmed-samir-shahin所写,您需要使用bundler来获取当前的ruby环境。 When I have to debug production errors in my projects I typically use a command like 当我必须在我的项目中调试生产错误时,我通常使用类似的命令

RAILS_ENV=production bundle exec rails console

I tend to use the ENV var instead of the rails console <env> notation. 我倾向于使用ENV var而不是rails console <env>表示法。

As you are pointing out the error occurs on some dashboard. 正如您所指出的,某些仪表板上发生了错误。 I assume that you are only reading data from your database there. 我假设您只是从那里读取数据库中的数据。 If so I then use the code involved (most likely code from the controller action) for trying to debug the behaviour. 如果是这样,那么我使用所涉及的代码(很可能是来自控制器动作的代码)来尝试调试行为。

By running the code in the console you can narrow down to point where things are getting messed up. 通过在控制台中运行代码,您可以缩小范围,使事情变得混乱。 This is what I normally do when there is some kind of urgency involved and ONLY when you are just reading data from the database. 这是我通常在涉及某种紧急情况时所做的事情,并且仅在您从数据库中读取数据时。

BUT you should try to get someone to make the data available in a staging environment where it is safe to play around with the data. 但是,您应该尝试让某人在可以安全地使用数据的临时环境中提供数据。

After figuring out why the problem occurs start setting up a test case on your local machine and fix the problem. 找出问题出现的原因后,开始在本地计算机上设置测试用例并解决问题。 :) :)

Alternative 替代

Run the code / queries in the rails console. 在rails控制台中运行代码/查询。 Store the results (copy and paste) and reuse the data to create a test case which you can then debug on your local machine. 存储结果(复制和粘贴)并重用数据以创建测试用例,然后可以在本地计算机上进行调试。

General advice - ask your boss for a strategy 一般建议 - 向老板询问策略

In general you should talk about this with your manager how to deal with these kind of situations. 一般来说,您应该与您的经理讨论如何处理这类情况。 It is important to everyone in the team how to tackle problems in production systems. 团队中的每个人如何解决生产系统中的问题非常重要。 I think someone should be responsible for this kind of emergencies. 我认为应该对这种紧急情况负责。 Also some more experienced people in your company should help / assist you while you make your first steps in debugging production issues. 在您调试生产问题的第一步时,贵公司的一些经验丰富的人员应该帮助/协助您。

I think it's in the company's best interest to train you in that field. 我认为培养你在这个领域是最符合公司利益的。 Imho your company has to provide some tools to make it easy to dive into the problem. Imho贵公司必须提供一些工具,以便轻松解决问题。 Otherwise such debugging sessions take forever. 否则这样的调试会话将永远。 Anyway no idea how your company is structured :) 无论如何不知道你的公司是如何组织的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM