Rails model global variable - ruby-on-rails

I want to implement a functionality of messages to display on different pages on my site.
I have created a model for this that contains fields, say page, message, active. If active=false the message will not be displayed. This works fine.
Now I want to add a capability to turn all messages on/off for some time. I don't want to update each message and set active=false because 1) there can be a lot of messages, 2) I would have to save which messages were active/inactive at the moment of turning off to restore the initial state when turning back on.
This would be very handy if I could use new "class-wide" variable ##active in my model. However, using multi-thread app (I use unicorn) can cause troubles because ##active will not be shared over all processes.
I was not able to google a good solutions for this, maybe someone can help?

Perhaps create an environment variable called DISABLE_ALL_MESSAGES and then override the message#active method to refer to ENV["DISABLE_ALL_MESSAGES"]:
def active
ENV["DISABLE_ALL_MESSAGES"] && super
end
Another option would be to store the setting in the database and refer to it in a similar manner.

Try setting preload_app=true in your unicorn configuration. This lets unicorn master process to preload the app and all the other workers share the loaded data structure. You might want to move to Ruby Enterprise Edition to share memory across processes.

Related

Preventing Rails from connecting to database during initialization

I am quite new at Ruby/Rails. I am building a service that make an API available to users and ends up with some files created in the local filesystem, without any need to connect to any database. Then, once every few hours, I want to run a piece of ruby code that takes these local files, uploads them to Amazon S3 and registers their location into a Postgres database.
Right now both codes live together in the same project. I am observing that every time a user does something the system connects to the database. I have seen this answer which recommends to eliminate all traces of ActiveRecord in my code, but given that I want to have my background bookkeeping process connect to the database I am stuck on what to do.
Is it possible to define two different profiles (one with database and one without) and specify which profile a certain function call should run on? would this work?
I'm a bit confused by this, the db does not magically connect to the database for kicks on every request, it does so because of a specific request requires it. Generally through ActiveRecord but not exclusively
If your system is connecting every time you make a request, then that implies you have some sort of user metric or authorisation based code in there. Just killing off the database will cause this to fail, and likely you'll have to find it anyways, to then get your system to work. I'd advise locating it.
Things to look for are before_filters in controllers, or database session management, for example, or look for what is in the logs - the query should appear - and that will tell you what is being loaded, modified or whatnot.
It might even work to stop your database, just before doing a user activity, and see where the error leads you. Rinse and repeat until the user activity works, without the database.

UiApplication is not able to maintain its state

I am developing an application to do the following things.
Tracks the Incoming/Outgoing Call.
If user attends to the call, after disconnected the call a Screen will pop-up.(User Have to fill some info and content will save in (Sqlite) database)
Now what I am doing is :-
First Make different entry point (autostartup) & this is the Entry-point of the application. (logic is working fine)
Implement Phone-listener that tracks Incoming/Outgoing Calls (logic is working fine)
When a call is disconnected, detail Screen will pop-up, User can fill up the screen. (logic is working fine)
But the Problem is when user Receives/Do calls and return back to the application, application isn't able to maintain variable states (like File Connections, Global variables that uses in application) they all become reset. This issue I am getting only on the Incoming/Outgoing Call time.
I used Run-time persistence storage but its not working in case of records insertion into the database.
I reinitialize the Database class but it's not working at all.
Please let me know, Why I am getting this issue.
"application can't able to maintain variable states (like File Connections , Global variables that uses in application ) they all become reset"
I presume that you are trying to maintain these 'global' variables as 'static' variables. Remember that static variables are only global to the same Application instance. In this case, the Phone Listener is invoked under a different application - the phone application - and so these variables are different to those that you see in your application.
To resolve this, I recommend doing two things:
a) Use RuntimeStore to provide a place to store shared (global) variables:
http://supportforums.blackberry.com/t5/Java-Development/Create-a-singleton-using-the-RuntimeStore/ta-p/442854
b) Have your listeners do as little as possible, use global events to pass the required information back to your application in your Application's context:
http://supportforums.blackberry.com/t5/Java-Development/Global-Events-and-Global-Event-Listeners/ta-p/444814
But perhaps I have not understood your problem clearly, if not, please clarify.

Tracking custom variable in goal conversion with Piwik + Ruby on Rails

I am testing Piwik if it would be suitable for analytics and tracking for my Ruby on Rails application. For testing purposes, I am running piwik on localhost, tracking the rails application running also on localhost. Piwik tracking is integrated by the piwik_analytics gem, and to track goal conversions I am using the piwik-tracker gem. All my goals are manually converted (by calling the goal conversion method from a controller).
My problem is that I wish to record goal conversions by a custom variable (scope: visit), but this does not seem to work. The custom variable is set correctly, and the goals conversions are recorded correctly, but the goal conversions are not sortable by the value of the custom variable.
In my application, there are three different types of users, and I wish to track their actions by user type. To this end, I have defined a custom variable which contains the user type. I have included setting this variable in the piwik_tracking_tag in application.haml.html, because I cannot know the entry page beforehand. I see that the variable is set correctly, because visits are recorded by this variable. The goals, too, are converted correctly.
However, in the goals panel, when I click on the 'By custom variable' option, I see 0 conversions for each different user type.
I am wondering if this is a Piwik issue, and not caused by me. I have also tried (re)setting the custom variable when calling the goal tracking method
piwik.request.custom_variable(1,"Role","user_role").track_goal(1)
but this makes no difference.
#marflar: We switched to client-side tracking (and updated Piwik to a version they released after I posted this quesiton), and then viewing by custom variable started working. However, Piwik randomly didn't manage to track some goals, so in the end we ditched Piwik and went for Mixpanel, which we've been content with.

Server Context Variable

I need to set up a context variable for rails in a way that I can store it and use on all controllers and actions in my app.
Basically it's a server that have several games. The user chooses 1 game and that goes on current_game. Depending on the value of current_game the app loads different stuff from the data base.
Is there a way I can make it?
May be you can using session variables, read the documentation !

Create a "playable demo" version of a Rails site?

It's quite common in sites- you have a "demo" version with a guest account full of data/posts/comments that you can play with, and all the data is reset every few hours so users wont spam the demo site.
I thought to have another rails environment, "mysite_demo" and use a cron job to call rake to reset it's database every X hours, and populate the seed data.
Then it hit me that all over my app I'll have to check if I'm running in "demo-mode":
For example, if the demo site has a login/register page too, a user might register, insert some data and wonder why his account is deleted after he logged in again.. so demosite shouldn't have a register option at all.
So I thought I'll make a "demo" branch of the code.. with the difference and just merge changes as I go... sounds like an overkill.
ideas?
In my application I started with a fixed demo user with an account that resets every hour. Something about that model didn't quite sit right - if there were multiple users hitting the demo at the same time you could get into some weird concurrency issues. And what if a user is in the middle of a demo and your reset the demo account? What happens?
I don't know if this model works for you but I ended up creating a brand new user account with a demo flag set in the database - I also automatically log the user in. This way the user gets to play around for as long as they like and I don't have to worry about data getting deleted/changed while a user demos my app. I run a cron job every night that deletes users with the demo flag set that are older than 24 hours.
If the demo version is running from its own database, how is it any different from the real thing? The demo site is just an instance of your product.
Just clean up the DB and redeploy the demo as needed. Is it just this simple or am I missing something?
Then it hit me that all over my app I'll have to check if I'm running in "demo-mode" (e.g, you cant register a new user in the demo) and make the site behave accordingly.
If the site is in demo, why does it matter what the users do? Anything they do will be wiped in a few hours, so they won't be able to actually do work with it.
It sounds like you are trying to handicap the site so they will pay. I don't know what your site does, but if its a host based service(web page that stores & display information) then the limited life span of the data should deter squatters.
If you website does something that can be used elsewhere, then I can see limiting it. An example might be a service that transforms media formats, or writes resumes. If the user can do something useful in the 2 hour window and walk away with it, then you might consider branching.
Why not allow the user to make an account even if it is deleted in an hour?
That allows them to see how the registration process of the script works for at least an hour, maybe give a message on the signup page that the account is only valid for an hour.
Just my thoughts
Is there any other functionality that is different in the demo version than the production environment? If it is just an issue of making the user register, you could just create a registered demo account in production, and give out the user name/password for people. Although this may not be an option depending on other business requirements.
If you are willing to use Authlogic you can take a look at this, then every X hours you can look through the database for users that start with anonymous_ and delete records that are associated with them.
Just make a separate demo site that works exactly like the production one, but the DB gets reset once an hour to clean example data. The only change you need to make is a banner across the top of every page that says its a demo. There are several ways to do it, (modify your site theme, or maybe use frames) but basically you should only have to change the code in one place, instead of throughout the site.
You could setup a new environment demo on your database.yml, with read-only privileges for the User table, and an additional demo_database. Then place some checks on your code to see if your RAILS_ENV is on DEMO.
That way, you only need to work with the same codebase and just show whatever you feel like it.
You can deploy it as a separate app with its own database to a separate domain or subdomain and then check the domain to decide what options should be available. For instance if you put it on demo.example.com you would use:
if request.domain =~ /demo/
If you use Capistrano you can set it up to update both apps when you deploy so they are in sync.

Resources