2020-10-24 00:08:35 +00:00
---
2022-05-30 00:08:35 +00:00
stage: Systems
2020-12-01 18:09:42 +00:00
group: Distribution
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-24 00:08:35 +00:00
---
2021-05-27 21:10:59 +00:00
# Debugging tips **(FREE SELF)**
2016-06-08 02:40:38 +00:00
Sometimes things don't work the way they should. Here are some tips on debugging issues out
in production.
2020-04-06 15:10:04 +00:00
## Starting a Rails console session
2016-06-08 02:40:38 +00:00
2020-10-14 15:08:42 +00:00
Troubleshooting and debugging your GitLab instance often requires a Rails console.
Your type of GitLab installation determines how
[to start a rails console ](../operations/rails_console.md ).
2020-07-28 06:09:53 +00:00
See also:
- [GitLab Rails Console Cheat Sheet ](gitlab_rails_cheat_sheet.md ).
2016-07-01 15:18:33 +00:00
2020-04-06 15:10:04 +00:00
### Enabling Active Record logging
You can enable output of Active Record debug logging in the Rails console
session by running:
```ruby
2021-06-08 15:10:00 +00:00
ActiveRecord::Base.logger = Logger.new($stdout)
2020-04-06 15:10:04 +00:00
```
2022-07-06 09:08:10 +00:00
This shows information about database queries triggered by any Ruby code
2020-04-06 15:10:04 +00:00
you may run in the console. To turn off logging again, run:
```ruby
ActiveRecord::Base.logger = nil
```
### Disabling database statement timeout
You can disable the PostgreSQL statement timeout for the current Rails console
session by running:
```ruby
ActiveRecord::Base.connection.execute('SET statement_timeout TO 0')
```
2022-07-06 09:08:10 +00:00
This change only affects the current Rails console session and is
not persisted in the GitLab production environment or in the next Rails
2020-04-06 15:10:04 +00:00
console session.
### Output Rails console session history
If you'd like to output your Rails console command history in a format that's
easy to copy and save for future reference, you can run:
```ruby
puts Readline::HISTORY.to_a
```
2020-09-14 00:09:36 +00:00
## Using the Rails runner
2020-04-06 15:10:04 +00:00
2020-04-10 21:10:45 +00:00
If you need to run some Ruby code in the context of your GitLab production
2020-09-14 00:09:36 +00:00
environment, you can do so using the [Rails runner ](https://guides.rubyonrails.org/command_line.html#rails-runner ). When executing a script file, the script must be accessible by the `git` user.
2020-04-06 15:10:04 +00:00
**For Omnibus installations**
```shell
sudo gitlab-rails runner "RAILS_COMMAND"
# Example with a two-line Ruby script
sudo gitlab-rails runner "user = User.first; puts user.username"
2020-04-10 21:10:45 +00:00
2020-07-22 15:09:28 +00:00
# Example with a ruby script file (make sure to use the full path)
2020-04-10 21:10:45 +00:00
sudo gitlab-rails runner /path/to/script.rb
2020-04-06 15:10:04 +00:00
```
**For installations from source**
```shell
sudo -u git -H bundle exec rails runner -e production "RAILS_COMMAND"
# Example with a two-line Ruby script
sudo -u git -H bundle exec rails runner -e production "user = User.first; puts user.username"
2020-04-10 21:10:45 +00:00
2020-07-22 15:09:28 +00:00
# Example with a ruby script file (make sure to use the full path)
2020-04-10 21:10:45 +00:00
sudo -u git -H bundle exec rails runner -e production /path/to/script.rb
2020-04-06 15:10:04 +00:00
```
2019-07-24 13:10:06 +00:00
## More information
2016-06-08 02:40:38 +00:00
2021-08-11 15:10:57 +00:00
- [Debugging Stuck Ruby Processes ](https://newrelic.com/blog/best-practices/debugging-stuck-ruby-processes-what-to-do-before-you-kill-9 )