It’s not unusual for Ruby builders to govern manufacturing knowledge through Rails console. Generally it’s simply essential. The essential factor is to depart a hint of what instructions you will have issued.
- Perhaps you’d be requested to do comparable modifications once more within the function.
- Probably one thing would go fallacious, and you’ll have to analyze what.
- I’m positive you wish to preserve our teammates knowledgeable on what’s going on.
There are a lot of extra causes to have some type of logging.
In his weblog publish, Paweł confirmed tips on how to load the helper module with the Rails console’s begin.
This time, in an identical manner, we are going to “hack” our console to get Slack notifications of what instructions are being known as, by whom, and for what goal.
Let’s put together a Console
module with a setup
technique that does the next actions:
- warns developer about engaged on non-development knowledge
- asks for his title
- sends notification about session’s begin
- asks for a goal of the present session
- sends notification in regards to the goal if there’s any
- sends notification about instructions issued (besides the final one which is usually
exit
orstop
) - sends notification about session’s end
require 'readline'
module Console
EXIT_COMMANDS = %w[quit exit]
class << self
def setup
warn except Rails.env.improvement?
return except Rails.env.manufacturing?
get_name
notify_session_started
get_purpose
notify_purpose
at_exit do
notify_commands_issued
notify_session_finished
finish
finish
non-public
def warn
app_name = Rails.software.class.module_parent_name
places "Welcome in #{app_name} console. You're accessing #{Rails.env} knowledge now."
finish
def get_name
whereas @title.clean?
@title =
start
Readline.readline("Please enter your title: ")
rescue Exception
exit
finish
finish
finish
def get_purpose
@goal =
start
Readline.readline("Please enter the aim of this session (or depart it clean): ")
rescue Exception
exit
finish
finish
def notify_purpose
return except @goal.current?
textual content = "The aim of *#{@title}'s* session is to: #{@goal}."
SlackBot.dev_notification textual content
finish
def notify_session_started
textual content = "New #{Rails.env} console session began by *#{@title}*."
SlackBot.dev_notification textual content
finish
def notify_session_finished
textual content = "*#{@title}'s* #{Rails.env} console session completed."
SlackBot.dev_notification textual content
finish
def notify_commands_issued
skip_last = Reline::HISTORY.final.in?(EXIT_COMMANDS)
Reline::HISTORY[0..(skip_last ? -2 : -1)].every do |command|
SlackBot.dev_notification "*#{@title}* issued a command: ```#{command}```"
finish
finish
finish
finish
To make it really works, now we have to append our Utility
class with these strains:
class Utility < Rails::Utility
#...
console do
Console.setup
finish
#...
finish
Working the console, you can be requested on your title and the aim of the present session. Then you’ll be able to function usually, and all of the instructions you typed shall be posted to your crew’s Slack channel.
Test additionally Kuba’s weblog publish to know tips on how to do the same factor with rails runner classes.