Decorate your runner session like a pro

… and check why 5600+ Rails engineers read also this

Paweł described some tricks you could use to tune-up your Rails console. I want to tell you about the runner method you can use to enhance your runner sessions.

Decorating runner sessions is a little bit less convenient as we don’t have a module like Rails::ConsoleMethods that is included when runner session is started. So adding some methods available for runner scripts is not that easy. However you can still add some code that will be executed at the start and the end of your runner sessions.

For example you can send some notifications so you don’t need to look at the terminal to check if the script evaluation has been finished. You can also measure time or set some Rails Event Store metadata to easily determine what has been changed during your session.

Here is an example we are using in one of our projects to log the elapsed time, set some RES metadata and send Slack notifications. You can just add it to your config/application.rb.

runner do
  session_id = SecureRandom.uuid
  script = ARGV.join(" ")
  Rails.configuration.event_store.set_metadata(
    causation_id: session_id,
    correlation_id: session_id,
    script: script,
    locale: I18n.locale.to_s,
  )
  t = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  notify_slack(
    username: "Script runner",
    text: "[#{Rails.env}] Runner script session #{session_id} has started: '#{script}'",
    channel: "notifications",
    icon_emoji: ":robot_face:"
  )

  at_exit do
    notify_slack(
      username: "Script runner",
      text: "[#{Rails.env}] Runner script session #{session_id} has finished: '#{script}' (elapsed: #{Process.clock_gettime(Process::CLOCK_MONOTONIC) - t} seconds)",
      channel: "notifications",
      icon_emoji: ":robot_face:"
    )
  end
end

With such snippet, each time you run some script (or evaluate some inline Ruby code) with rails runner it will set the metadata for your RES instance and it will send a Slack notification at the beginning and the end of the runner session.

You might also like