I'm using fragment caching in my 3.1. Rails app and have one fragment that isn't expiring and I don't know why. It's a fragment that I want to expire based on time (every hour). I'm on Heroku, using the Memcachier Add-on for my caching. I don't seem to have any other caching issues.
In my app, there are three models: User, Community, and Activity. On the Community#index, there is a fragment that shows Activity by Users in this Community that I want to expire hourly. The calculation, which is a method in the Activity model, works fine - it's just that the fragment is expiring hourly (and refreshing).
In my view, I have:
<% cache("activity_#{community.id}", :expires_in => 1.hour) do %>
<-- content >
<% end %>
I've also tried making it a scheduled task, by adding an expiration for the cache in the User model.
def self.expire_activity
Community.find_each do |community|
ActionController::Base.new.expire_fragment('activity_#{community.id}')
end
end
I tried to follow the answer to this question to determine how to expire the cache from a model, but with this this code, I get the error:
NoMethodError: undefine method 'expire_fragment' for #<Class:0x5ffc3e8>
I was facing a similar issue on Rails 4.2.1. It was caused by a mismatch between the servers set timezone and that of the rails app. Setting both to be the same fixed my issue.
On the server, assuming Ubuntu, as root run this command and follow the prompts:
dpkg-reconfigure tzdata
Within the application.rb config file, be sure that the following line matches the server (default is UTC).
# config.time_zone = 'Central Time (US & Canada)'
To get a list of available time zones for rails, run:
bundle exec rake time:zones:all
I know this is an old question, but is returned high up in google when searching for a solution.
Related
I've recently tried to perform a simple task: Install a package if it does not exist by pulling distribution out of web location (in-house repo) and deleting it once it is not longer needed.
Learning about :before notification I came up with following elegant code (in this example variable "pkg" keeps name of distribution image, "pkg_src_location" is URL of my web repository, "name_of_package" is named installed package):
local_image = "#{Chef::Config['file_cache_path']}/#{pkg}"
remote_file 'package_image' do
path local_image
source "#{pkg_src_location}/#{pkg}"
action :nothing
end
package name_of_package do
source local_image
notifies :create, 'remote_file[package_image]', :before
notifies :delete, 'remote_file[package_image]', :delayed
end
I was quite surprised that it does not work... Actually 'package' resource is being converged without 'remote_file' being created - and it fails due to source local_image not being in place...
I did a simple test:
log 'before' do
action :nothing
end
log 'after' do
action :nothing
end
log 'at-the-end' do
action :nothing
end
log 'main' do
notifies :write, 'log[before]', :before
notifies :write, 'log[at-the-end]', :delayed
notifies :write, 'log[after]', :immediately
end
What I learned is that 'main' is actually converged twice! Once when first encountered and once again, after 'before' resource is converged...
Recipe: notify_test::default
* log[before] action nothing (skipped due to action :nothing)
* log[after] action nothing (skipped due to action :nothing)
* log[at-the-end] action nothing (skipped due to action :nothing)
* log[main] action write
* log[before] action write
* log[main] action write
* log[after] action write
* log[at-the-end] action write
Is it a bug or feature? If this is 'feature', it is a really bad one and Chef shouldn't have it at all. It is simply useless the way it works and only wastes people's time...
Can anyone having more in-depth Chef understanding comment on it? Is there any way to make ':before' work? Maybe I'm just doing something wrong here?
To be a bit more specific, the before timing uses the "why run" system to make a guess about if the resource needs to be updated. In this case, the package resource is invalid to begin with so whyrun can't tell that an update is needed.
After a little rethink about it I get what's going wrong here:
The before notification is fired if the actual resource will have to be updated, in chef inner this mean getting the actual resource state to compare to the desired resource state ('\load_current_resource\ in providers).
Here you're willing to install a package, chef will ask the system about this package and it's version, and then will compare this result with the source you provided.
And here comes the problem, you can't compare with the source package as it is not installed.
For your case, the best bet is to leave the file on the system and get rid of notifications.
The before notification could be of interest if you want to launch a database backup before upgrading the DB system for example or as mentioned in the RFC for :before to stop a service before upgrading its package.
But it should not be used to trigger another resource providing one of the "calling" resource properties.
When I am sending a a GET request to rails server it takes too long time to respond (29 minute)
Below is the log snippet
Log says that there is an error in code, it is ok, but why take so long to respond (1723579 ms) I am unable to find any reason of such kind of behavior. Previously when server was working fine this js request only take 9 ms to respond. but suddenly it started to behave like this. How should i debug the application to trace the root cause of such unexpected behavior.
Started GET "/my-server/jobs/workers?_=1356363515400" for 27*.*.*.* at 2012-12-24 21:08:35 +0530
ActionView::Template::Error ():
1: <% #jobs.each do |job| %>
2: $('#cron_<%= job.id %>').attr('data-content', '<%= distance_of_time_in_words_to_now(job.next_fire_info, true) %>');
3: <% end %>
4:
5: <% #workers.each do |worker| %>
app/models/job.rb:16:in `next_fire_info'
app/views/jobs/workers.js.erb:2:in `block in _app_views_jobs_workers_js_erb__101155230_81985760'
app/views/jobs/workers.js.erb:1:in `_app_views_jobs_workers_js_erb__101155230_81985760'
Rendered jobs/workers.js.erb (1718348.7ms)
Completed 500 Internal Server Error in 1723579ms
I am on Rails 3.1.3,
Ruby 1.9.3p194,
MongoDB version v2.2.0, pdfile version 4.5,
32 bit Ubuntu (12.04) with 2 gb ram
Versions of rails 3.1 before 3.1.5 have a bug whereby when an exception is raised from a view rails takes a very long time to generate the exception message.
If you can't update to 3.1.5, the fix is very simple (see the commit that fixes it) - you just need to monkey patch inspect:
module ActionDispatch
module Routing
module RouteSet
alias to_s inspect
end
end
end
I used to dump this in an initializer. There's also a gem (safe_inspect) that claims to do this for you although I never tried it.
Finally i have found the reason for taking so long time to respond. The workers are running periodically based on cron expression.Root cause of this particular issue is one cron expression for job was entered erroneously. that's why at the time of executing "distance_of_time_in_words_to_now" took too much time.
I had similar problem and it was caused by authentication, so this is just for record if someone has the same problem in the future. The server did not let the user in and then redirected to the page which was restricted as well etc. etc.
Working on upgrading an application from Ruby 1.8.6/Rails 2.3.5 to Ruby 1.8.7/Rails3.0.11.
In application.rb, config statements have been moved (from environment.rb) into the:
module AppName
class Application < Rails::Application
...
end
end
block, including the statement:
config.time_zone = 'Eastern Time (US & Canada)'
In the old environment.rb file, there was also some code for initialization of some constants, which i've moved into application.rb. I've tried it after the module/class block (as it was before) and within it, but the following statement:
Time.zone.parse(external_config_date)
is producing this error:
config/application.rb:49: undefined method `parse' for nil:NilClass (NoMethodError)
I'm a little baffled by this (as often happens with Rails date/times :-/); mostly it looks like Time.zone is an acceptable way to access the default time zone, but i've also seen it said that 'zone' is an instance method of Time instead of a class method.
Any insight on this (or thoughts on how to troubleshoot further) much appreciated!
It looks like moving the extra config code from environment.rb into application.rb was my mistake here. 'Time.zone.parse' appears to be a Rails add-on, and it hadn't been enabled in application.rb yet. Moving the config code back to the bottom of environment.rb, after the line
AppName::Application.initialize!
got it working.
This is a direct follow-on to this question: What is the fastest way to render json in rails?
My app does a database query and render to JSON for a JS callback. It takes at least 8 seconds for a small (1 MB) dataset, and more like 20 for a large (3.5 MB) one. This is basically going to kill my app as an idea. My users aren't going to put up with this sort of wait.
I've read about multi_json and oj and yajl, and I think I've got them installed, but none of the ways I've tried to activate the various gems in my Gemfile show any improvement in serializing time. How can I prove that I'm using one over the other, so that I compare results between them? I can't find any way of outputting (to the Rails debug log or the JS console in the browser) which library might have gotten used for the actual 'render :json => #data' call.
Instead of fiddling with your controller, a better way is to use the Rails console, like so:
$ rails console
Loading development environment (Rails 3.2.8)
1.8.7 :001 > MultiJson.engine
=> MultiJson::Adapters::JsonGem
You can interact directly with your Rails stack that way.
I finally figured out I could do 'render :text => MultiJson.engine' in my controller. This yielded "MultiJson::Engines::Oj".
It confirms that I'm already using the supposedly fastest JSON library, and I may be hosed. I guess I'll try to return pure text through the controller (which takes 2 seconds compared to 8) and see how fast a routine to convert that to a hash will take...
I'm writing a book on Rails 3 at the moment and past-me has written in Chapter 3 or so that when a specific feature is run that a routing error is generated. Now, it's unlike me to go writing things that aren't true, so I'm pretty sure this happened once in the past.
I haven't yet been able to duplicate the scenario myself, but I'm pretty confident it's one of the forgotten settings in the environment file.
To duplicate this issue:
Generate a new rails project
important: Remove the public/index.html file
Add cucumber-rails and capybara to the "test" group in your Gemfile
run bundle install
run rails g cucumber:skeleton
Generate a new feature, call it features/creating_projects.feature
Inside this feature put:
This:
Feature: Creating projects
In order to value
As a role
I want feature
Scenario: title
Given I am on the homepage
When you run this feature using bundle exec cucumber features/creating_projects.feature it should fail with a "No route matches /" error, because you didn't define the root route. However, what I and others are seeing is that it doesn't.
Now I've set a setting in test.rb that will get this exception page to show, but I would rather Rails did a hard-raise of the exception so that it showed up in Cucumber as a failing step, like I'm pretty sure it used to, rather than a passing step.
Does anybody know what could have changed since May-ish of last year for Rails to not do this? I'm pretty confident it's some setting in config/environments/test.rb, but for the life of me I cannot figure it out.
After I investigate the Rails source code, it seems like the ActionDispatch::ShowExceptions middleware that responsible of raising exception ActionController::RoutingError is missing in the test environment. Confirmed by running rake middleware and rake middleware RAILS_ENV=test.
You can see that in https://github.com/josh/rack-mount/blob/master/lib/rack/mount/route_set.rb#L152 it's returning X-Cascade => 'pass' header, and it's ActionDispatch::ShowExceptions's responsibility to pick it up (in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L52)
So the reason you're seeing that your test case is passing because rack-mount is returning "Not Found" text, with status 404.
I'll git blame people and get it fix for you. It's this conditional here: https://github.com/rails/rails/blob/master/railties/lib/rails/application.rb#L159. If the setting is true, the error got translated right but we got error page output. If it's false, then this middleware doesn't get loaded at all. Hold on ...
Update: To clearify the previous block, you're hitting the dead end here. If you're setting action_dispatch.show_exceptions to false, you'll not get that middleware loaded, resulted in the 404 error from rack-mount got rendered. Whereas if you're setting action_dispatch.show_exceptions to true, that middleware will got loaded but it will rescue the error and render a nice "exception" page for you.