Cron Job to clear Rails Cache

The rails file cache is something that keeps growing.
Here are some 'handy' snippets.

Delete all files older then 7 days. (replace -delete with -print to test it)

find ./tmp/cache/ -type f -mtime +7 -delete

Delete all empty folders. (mindepth prevents the deletion of the root folder)

find ./tmp/cache/ -type d -empty -delete -mindepth 1

The whenever.rb I use

job_type :command_in_path, "cd :path && :task :output"

every :day, at: "0:40" do
  command_in_path "find ./tmp/cache/ -type f -mtime +7 -delete"

every :day, at: "1:10" do
  command_in_path "find ./tmp/cache/ -type d -empty -delete -mindepth 1"

Spree Issues config.action_view.raise_on_missing_translations

I'm working on a Rails Spree 4.1 app.
I enabled raising exception on missing translations via the option config.action_view.raise_on_missing_translations in config/environment/development.rb.
It's nice to know I a tranlsation is missing.

This was working fine, until I included the spree_mollie_gateway.

I couldn't boot the rails application anymore. It constantly raised:

gems/i18n-1.8.3/lib/i18n.rb:373:in `handle_exception': translation missing: nl.spree.validation.must_be_int (I18n::MissingTranslationData)

The woraround I now use is this: (config/development.rb)
Just enable it after the GEM is loaded

# Raises error for missing translations.
  config.action_view.raise_on_missing_translations = false
  Rails.application.config.to_prepare do
    ActionView::Base.raise_on_missing_translations = true

FreeBSD, MySQL out of swap space

My FreeBSD server is giving a lot of swap errors. Every day the daily reporting email was filled with kernel messages like these: kernel log messages:
+swap_pager_getswapspace(31): failed
+swap_pager_getswapspace(32): failed
 ... and many more ...

After figuring out which process was causing this, it seemed MySQL was to blame.
That was odd, because these are the number of mysqltuner is reporting about my configuration:

[--] Physical Memory     : 8.0G
[--] Max MySQL memory    : 845.4M

Max 850 MB

After a lot of googling, reading MySQL, it seemd the performance schema could be to blame. Because is is completely stored in memory.

Now for several days in a row, I don't have the swaperrors ☺️
To disable the MySQL permance schema place the following in /usr/local/etc/mysql/my.cnf

performance_schema = 0

Btw. I'm not sure if I need the performance scheme ..

⚠️ Followup a few weeks later

Unfortunately this isn't the solution. MySQL is still running out of swap space. I simply don't get it, because the memory usage should be limited


ActionText rendering issues and Spree 4

ActionText is a nice replacement for CKEditor/TinyMCE for content editing. It's embedded in the Rails core, works directly with ActiveStorage, and integrates very nicely.

Last week, I tried ActionText for the first time in the backend of my Spree store. To make this work I had to make several adjustments.

First I followed the default instructions of ActionText:

I had to make webpacker active in the Spree backend. So I've included the javascript pack. (Is used content_for :head in mij CMS plugin view)

<% content_for :head do %>
    <%= javascript_pack_tag('application', 'data-turbolinks-track': 'reload') %>
<% end %>

And activate the css in the spree shop stylesheets/spree/backlend/all.css

 *= require spree/backend
 *= require ./../../actiontext
 *= require_self
 *= require_tree .

At this point the ActionText editing was operational.

BUT, rendering didn't work as expected.
Images weren't shown when rendering the actiontext.

<%= cms.my_action_text %>

After a lot of debugging and playing around with /activestorage/blobs/_blob.html.erb, I discovered the IMG tags were simply stripped!

I discovered the allowed tags were extremely limited. The IMG tag wasn't included??
To solve it, I added severy other allowed tags in a rails initializer:

  # Workaround for issue with ActiveText... Somehow the Rails Sanetizer list is very restricted!!
  ActionText::ContentHelper.allowed_tags = (ActionText::ContentHelper.allowed_tags +
    ["strong", "em", "b", "i", "p", "code", "pre", "tt", "samp", "kbd", "var", "sub", "sup", "dfn", "cite",
      "big", "small", "address", "hr", "br", "div", "span", "h1", "h2", "h3", "h4", "h5", "h6", "ul", "ol",
      "li", "dl", "dt", "dd", "abbr", "acronym", "a", "img", "blockquote", "del", "ins"]).uniq

  ActionText::ContentHelper.allowed_attributes = (ActionText::ContentHelper.allowed_attributes +
    ["href", "src", "width", "height", "alt", "cite", "datetime", "title", "class", "name", "xml:lang", "abbr"]).uniq