How do you write a Rails cron job Whenever gem? - mysql

The whenever gem is installed. I see all kinds of cron and whenever tutorials about scheduling tasks with minutes, hours, days etc. I watched the railscast video on cron/whenever.
But I've yet to find any examples about how to write a job itself, other than rake tasks.
I want to schedule a task that checks the database for changes. The idea is that the database will have tags that tell you which particular row has changed. Whenever should poll the database periodically to check for these changes. Then it hopefully can push, or let the client know it needs to update the page dynamically using ajax.
If I were doing this manually, I'd use commands like:
rails dbconsole
select blah from blah;
Is there a way to write mysql commands in whenever? Is this the correct/best way to poll the database for changes?
I know there are ways to poll a database from mysql itself, but I've been specifically told to do it from the rails side.
I'm a newbie to all of these technologies (Rails, databases, ajax) so that's probably why the answer isn't clear to me.
On the client end, I have buttons that use jquery to add/delete/change row data, just to assure myself I know how to change things in the table once I can get stuff from the database. Those buttons will eventually be removed.
Right now, the page uses ajax to refresh the entire html table. But they would like just a row refresh/update through ajax.

Look at the RailsCast for cron/whenever again. You'll notice an example line of code like this:
runner "MyModel.some_process"
The code in the strings is evaluated and run. So whatever you want whenever to run, just write that code yourself and have a way for it to be called.
So maybe you create a class named DatabaseWatcher and store it in lib which has a class level method named .run, you'd do the following:
runner "DatabaseWatcher.run"
And that's it. In your .run method is where you'd put your logic. As for how to actually write that code, that depends on your requirements. Are you looking for if the updated_at time is within 1 minute of now? Do you store a time when you last checked the DB, and then you can see if the updated_at time is greater than that? Do you have a table that stores every time the model is changed? That all depends on you.

Related

NodeJS Emit when a row is added to database table

I am quite new to NodeJS but I've written a few apps (like a chat, real time page updater etc) And I've used mysql to read from my database and emit() information to my webpage, but how can I have nodeJS watch a database table and emit() whenever a row is added?
I have no idea where to start and google didn't produce much results. However there must be an include I can require() that will watch the database in someway.
The answer really isn't a nodejs question, but a mysql question. If mysql itself can tell you ( via an event or log ) that a row was added, nodejs could read and consume that data. Unfortunately, that doesn't seem to be possible, based on this answer. The answer suggests that the only thing you can do with mysql is to poll for new rows.
IF you're in control of inserting the data rows from nodejs itself, there shouldn't be any problem emitting those events after you get the confirmation it wrote, but I do not have enough information on your project to know what constraints you have.
UPDATE: Nice little npm package https://www.npmjs.com/package/live-sql seems to solve your problem I hope

Is there any event which is called whenever there is a change in the MYSQL database data?

Is there any event which detects changes in the database tables? Im trying to do a chat box. It works fine. Except that it needs to be refreshed at regular seconds of interval to get the data. But, this would eat up lot of server load. SO, i was thinking of a event which triggers any changes in the database data. Suggest me the best solution.
You could use triggers as presented in the comments. Problem with that: a trigger can only be triggered to execute an sql statemnt. So you could us a trigger, and create some kind of "activity" table with proper indexes. Problem: you still need to check this activity table! To reduce load, you should write a server component to your chat program that notifeis all clients. This way, only the server has to check to database regularly and not each client.
Without more information what language you are writing in, we can not help you more I guess.

detecting changes in a mysql database

I have someone else's code and also a db. To understand the code, I want to see that If I run a particular php process, which tables are updated in the db. Something like I start a listner and then carry out the process. After that the listner will show me a list of changes that happened in the database. Is this possible?
Note: As I understand, mysql TRIGGER does something similar but it is for a specific table and also on trigger adds rows to another table. I just want a change log sort of a thing. Thanks in advance.
You could use logging - turn it on, run the code, turn it off, then review the log file.
This article talks you through how to do this:
http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/

I need a Git hook to sync MySql schema

One of the biggest problems i have today is that every time that i make a commit to git i get make the changes on the data base by hand. I don't want that the schema of the data base is always up to date.
I would like to be able have a pre commit hook that check the database schema and include it as part of the commit. Also that every time that I make a pull the data base gets updated.
Anyone has something like this already?
(I have a LAMP server, but I'm willing to install anything that helps me with this)
Like this?
http://www.edmondscommerce.co.uk/git/using-git-to-track-db-schema-changes-with-git-hook/

Data sync solution?

For some security issues I'm in an envorinment where third party apps can't access my DB. For this reason I should have some service/tool/script (dunno what yet... i'm open to the best option, still reading to see what I'm gonna do...)
which enables me to generate on a regular basis(daily, weekly, monthly) some csv file with all new/modified records for a certain application.
I should be able to automate this process and also export at any time a new file.
So it should keep track for each application which records he still needs.
Each application will need some data in some other format (csv/xls/sql), also some fields will be needed for some application and some aren't... It should be fairly flexible...
What is the best option for me? Creating some custom tables for each application? Based on that extracting modified data?
I think you best thing here, assuming you have access to the server to let you set this up is to make a small command line program that can do the relativley simple task you need. Languages like pearl are good for this sort of thing I do believe.
once you have that 'tool' made you can schedule it through the OS of the server to run ever set amount of time. Either schedule task for a windows server or a cronjob for a linux server.
You can also (with out having to set up the scheduled task if you don't / can't want to) enable this small command line application to be called via 'CGI' this is a special way of letting applications on the server be executed at will by a web user. If you do enable this though, I suggest you add some sort of locking system so that it can only be run every so often and to stop it being run five times at once.
EDIT
You might also want to just look into database replication or adding read only users. This saves a hole lot of arseing around. Try to find a solution that dose not split or duplicate data. You can set up users to only be able to access certain parts of the database system in certain ways, such as SELECT data