All posts by Kenji

Rails 4 Send mail when Error occurs


I wrote code to send mail when error occurs in Rails 4.

Environment

  • Ubuntu 15.05 (64bit)
  • Ruby 2.2.2
  • Rails 4.1.8

Old Way

In Rails 3, I could use rescue_in_public method in ApplicationController and send error notification mail.

current_user returns user who logging in. It’s defined in devise gem.

But in rails 4, it doesn’t work.

Solution

I used rescue_from, which also exists in Rails 3.

rescue_from Exception catches all exception occurs in controller, but rescue_from StandardError doesn’t. It is because RuntimeError, for example, is inherits Exception but doesn’t inherit StandardError, which is raised by raise "exception!" simply. And, syntax error like 0 = 1 couldn’t be caught. Error caught by rescue_from Exception is errors occurs in running program. And errors occur before controller procedure can’t be caught.

The point is raise exception in handle_exception method. I don’t want to erase exception by handle_exception, but want only to send notification mail. It is like AOP. In the above, add method into application_controller.rb simply.

current_user in send_error_mail method returns logging in user, which is defined in the gem, devise. self is used for reporting in which controller the error occurs. request is for reporting request url, etc. The code !Rails.env.development? should be replace to config value. I heard that we can use custom configuration in Rails 4.2, so it is the issue after rails upgrading.

Attention

Exceptions before controller procedure should be reported by other way.

I know handling exception outside of controller is a way, but there are variables can be referred in controller and objects useful in controller, I thought, so I wrote code into applicatoin_controller.rb.

Continue reading Rails 4 Send mail when Error occurs

Simple Rails Log Rotation


Here’s the way of simple log rotation in Rails.

Environment

  • Rails 4.1.8
  • Ruby 2.2.2
  • Ubuntu 15.04 (64bit)

Setting

First, add the following code to config/application.rb.

In the above, log file will be split by 100 MB (104,857,600 bytes) and limit 10 files. Namely, it will save about 1 GB logs. また、 10 ファイル を超えると、古いものから削除されていきます。

Rails can configure logger as daily, weekly, etc., but in that case old log will not be deleted.

Test

Before testing in development environment, create log file with the following command.

bs represents block size, count represents times to write.

And launch server with rails s, access some pages. Then, development.log and development.log.0 will be created.

About Rails logging, you can use the other ways like SyslogLogger and Linux logrotate, for example.


When I was said, “Unable to boot”


I tried to install Lubuntu into really old machine, Let’s Note CF-W2. Then I got the following message.

Let’s Note CF-W2 のスペックは次の通りです。

Environment

Specification

  • CPU: Intel Pemtium M 32bit 1GHz
  • RAM: 1GB

OS Image (ISO)

  • lubuntu-14.04.3-desktop-i386.iso

Reason

The message seems to imply “You are installing 64 bit image into 32 bit machine”, but it’s not correct.

The exact reason is that Intel Pentium M doesn’t support PAE.

PAE

Technology to enable 32 bit CPU to handle memory more than 4 GB. It is implemented not only by Intel, also by AMD.

Solution

私がとった解決策は次の方法です。 Ubuntu の forcepae オプション を使います。

  1. Boot Lubuntu from DVD.
  2. Select language.
  3. Press F6 key and ESC key.
  4. Then you can see the command like the following.
  5. Now select “Install” by up or down key.
  6. Then change the command displayed, like the following. Put “forcepae” into 2 places.
    • The first one is an option for kernel booted for installation, and the second one is an option for kernel to be installed.

It worked.

The next site introduce several solutions.

reference: How can I install on a non-PAE CPU?


Soccer Ball and Regular Polyhedron


On the article Calculate Pentagons and Hexagons in a Soccer Ball, I calculated pentagons and hexagons in a soccer ball. Here, think about it from another aspect, without using Euler’s formula.

Soccer Ball
Soccer Ball

Connect Centers of Hexagons

Imagine a polyhedron, of which vertexes are the center of hexagons in a soccer ball. Its each face is regular pentagon, and it is composed only from pentagons. It is regular dodecahedron.

Dodecahedron
Regular Dodecahedron

The number of faces in a regular dodecahedron is the same as one of regular pentagons contained in a soccer ball. Therefore, the number of pentagons in a soccer ball is 12.

The number of vertexes in a regular dodecahedron is the same as one of regular hexagons contained in a soccer ball. Therefore, the number of regular hexagons in a soccer ball is ( 5 times 12 div 3 = 20 ) .

Then, the number of faces in a soccer ball is

[ 12 + 20 = 32 . ]

Now, calculate the number of edges in a soccer ball. Add edges of pentagons and hexagons and remove duplication, then

[ ( 5 times 12 + 6 times 20 ) div 2 = 90 . ]

Lastly, calculate the number of vertexes in a soccer ball. A vertex in a soccer ball belongs to 3 faces, so add the vertexes of pentagons and hexagons and divide it by 3, then

[ ( 5 times 12 + 6 times 20 ) div 3 = 60 . ]

Above, we thought polyhedron whose vertexes are the centers of hexagons in a soccer ball. We can calculate if we think about a polyhedron whose vertexes are the centers of pentagons in a soccer ball, too.

Connect Centers of Pentagons

Imagine a polyhedron, whose vertexes are the center of pentagons in a soccer ball. It is regular icosahedron. It is obvious that it’s the same as a polyhedron made by connecting the centers of the faces in a regular dodecahedron.

Regular Icosahedron

Truncated Polyhedron

Now truncate the vertexes of regular icosahedron.

Truncating the vertexes of regular icosahedron, then soccer ball appears. This soccer ball shape is called truncated regular icosahedron.

If you deeply truncate the vertexes of regular dodecahedron, soccer ball appears, too. On the following regular dodecahedron image, if you truncate along the line, sort of soccer ball will appear. (The line is too deep, so the hexagon is not regular hexagon but hexagons and pentagons appear.)

Dodecahedron
Regular Dodecahedron