Category: Hosting

Ruby & Rails: Making sure rake task won’t slow the site down

If you don't have multiple cores and/or you have a small VPN, you may end up with a huge slow down of your web app, when rake tasks are executed. This can be a big issue especially when you use something like whenever to perform periodic tasks. Luckily there's a nice program:

nice is a program found on Unix and Unix-like operating systems such as Linux. It directly maps to a kernel call of the same name. nice is used to invoke a utility or shell script with a particular priority, thus giving the process more or less CPU time than other processes. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. The default niceness for processes is inherited from its parent process and is usually 0.

With nice you can tell your Linux kernel to give your rake tasks the lowest priority possible. Thanks to that, you can make sure, that any task with higher priority (like web UI server) won't get stuck because of the background rake task.

Here's how you can use it:

RAILS_ENV=:environment nice -n 19 bundle exec rake :your_task 

If you use a gem like whenever, you can easily integrate nice with it:

job_type :runner, "cd :path && nice -n 19 script/rails runner -e :environment ':task' :output"

every 30.minutes do
  runner 'Worker.new.perform'
end

or for a rake task with whenever, you can use this:

job_type :runner, "cd :path && :environment_variable=:environment nice -19 bundle exec rake :task --silent :output"

every 15.minutes do
  runner 'your_task'
end

Note: Of course this will help, when CPU is your bottleneck. Keep in mind that you're rake task might slow down your website because of many more reasons (IO, DB, etc).

Undefined and cache/xxx and invalid %-encoding weird requests

Recently I've been having a lot of weird looking requests that would end up reported in Errbit. Initially I thought that it is my fault but after double checking all the JS code, I would still get those errors.

Fixing this is quite easy. Undefined/cache/xxx requests come from Complitly malware chrome plugin. Solution to this is quite simple (source). Just add this to every page you have (or every with a form):

<script type="text/javascript">
    window.suggestmeyes_loaded = true;
</script>

this will stop the malware script.

The second issue:

ArgumentError: invalid %-encoding (arizona1�0���U��� scottsdale1%0#��U� ��starfield
technologies, inc.1301��U���*http://certs.starfieldtech.com/repository/1402��U���+starfield
secure certificate authority - g2/html>)

The rest of weird requests was coming from EasouSpider crawling engine. So just:

User-agent: EasouSpider
Disallow: /

and don't care anymore.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑