how to allow users to schedule recurring cron jobs in rails - mysql

I'm building a small reminder application in Rails and I want to add the ability for my users to be able to create a cron job. The cron job would run on a given day each week, each month or even one time depending on the users selection and save.
So the user creates the even, selects if its weekly, daily or one time, then it creates the cron job to run a command line in linux.
Any gems for that to be easier?

The common way would be to have database table with maybe:
status | linux_command | execute_at
and to run every 10 mins a cronjob witch checks the database and executes the command.

Related

Schedule a job based on output of another job

On Server1, i want to schedule a job (JobTest). I want to schedule this once another job(JobLive) is completed.
Job Live is on Another server (Server2)
How this could be done.
Please respond with your valuable information.
Jobs can only be scheduled to run based on a given time of day. They cannot be scheduled to run based on the completion of another job or any other type of condition.
One way to get what you seem to want to is to have the first job add a record to a table that the second job will have access to, and put a step in the second job to check the table, and if the record is not there in the table, don't do anything. Then you would need to schedule the second job to run throughout a range of time, so that it keeps trying until the record is there in the table. Then when the second job completes, it deletes the record from the table, so that it won't run again until the first job runs again.

Automatically delete outdated rows from database every n seconds

I have a database which has a timestamp column and I want outdated data to be deleted.
So my idea is to write a MySQL query to a .php file which deletes every row where timestamp < current_timestamp - const. As there will be a lot of rows where this has to be checked, I am going to set an index to the timestamp column.
So how can I run this script automatically every n seconds? I heard about Linux crontab - can I use this on my webserver (not the db server) to execute the .php file periodically and is this overall a good technique to delete outdated rows from a database?
The database is set on a RDS instance on Amazon Web Services. My webserver is a EC2 instance (also Amazon Web Services).
Doing such a thing requires setting up an event or job. Such efforts keep the database very busy.
I would strongly recommend a different approach. Use a view to access the data you want:
create view v_t as
select t.*
from t
where timestamp > CURRENT_TIMESTAMP - ??;
Then use this view to access the data.
Then, periodically, go in an clean the table to get rid of the rows that you don't don't want. You can do this once a day, once a week, once an hour -- the deletions can occur at times when the database load is lighter, so it doesn't affect users.
I think you should check out lambda service on AWS.
It allows you to run commands against AWS services without another instance running.
Here's an example on how to set it up.
http://docs.aws.amazon.com/lambda/latest/dg/vpc-rds-deployment-pkg.html
Good luck
Eugene
Gordon Linoff's approach is ideal, but if you want to go the route of scheduled jobs, MySQL Event Scheduler is something you can try. The following example, runs daily and delete records older than a week.
CREATE EVENT
clean_my_table
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM my_table
WHERE time_stamp < date_sub(now(), INTERVAL 1 WEEK);
MySQL Event Reference page
https://dev.mysql.com/doc/refman/5.7/en/create-event.html

To update all the records that are growing in number in Rails

I'm using MySQL, and Rails 4.2.3. I've a table called onwers, and it's gonna keep growing for 2 weeks, as there is background job that is feeding it all the time.
Now, I'd like to update all Onwer records in Rails, and the following is the way that I'm accomplishing the job:
Owner.find_each(batch_size: 100) do |owner|
Owner.get_owner_details_from_github owner.login
end
What would be the best way to write code that will keep doing its job? On one hand, it's gonna populate owners table, and on the other, no matter how many records are there in owners, the code will do its job.
One approach would be putting this code in a rake task and having a cron job run that task on some interval.
Alternatively you could use a tool like sidetiq: https://github.com/tobiassvn/sidetiq. It uses a daemon process (sidekiq) that runs perpetually and enqueues jobs on an interval you'd define.

recurrent job runs only once

I have a SQL job schedule to run daily. There are 8 jobs running with the same schedule. 6 jobs run properly. The remaining two run only once when i start the jobs manually, subsequent recurring runs never happen. There is no information about these two in job history.
It looks like the scheduler does not invoke the mentioned two jobs at all.
Workaround: set the schedule to run once in 24 hours, added start time and end times with 30 minutes of interval.

MySql - Missed event schedule

I am trying to use mysql event schedule in my application, I have not use it before so i have some confusions.
I want to know if my computer is off on the schedule date, then schedule will continue on next day, after starting my computer?
Like:
my schduled is for beginning at every month (no predefined time set)
if in the above date my computer/Server is off,
will mysql continue scheduled event in next day after turning on my computer/server?
If no, then please suggest a solution.
Hmmmm, have you looked at something like this?
MySQL: Using the Event Scheduler
... or:
How to create MySQL Events
... or even: [MySQL :: MySQL 5.1 Reference Manual: 19.4.1. Event Scheduler Overview](19.4.1. Event Scheduler Overview)?
Also please keep in mind that SQL DBMS servers are written with the rather strong presumption that they will be kept up and operating 24 hours per day with only brief periods of downtime for maintenance or repairs. There is generally very little consideration for operation on machines which are shutdown at night and while not in use.
If you simply store a table of dates and events then your can simply query that table for events which have passed or are upcoming within any range you like ... and you can run the program(s) containing those queries (and performing any appropriate activities based on the results) whenever you start you computer and periodically while it's up and running.
These links refer to a feature of MySQL which is designed to have the server internally execute certain commands (MySQL internal commands, such as re-indexing, creating/updating views, cleaning tables of data which "expires" and so on. I don't know if a MySQL server would attempt to execute all events which have passed during downtime, though it should only be a little bit of work to follow the tutorial, schedule some event for some time (say 15 minutes after the time you expect to hit [Enter]) ... then shutdown your computer (or even just the MySQL server) and go off to lunch. Then come back, start it up and see what happens.
The scheduled event could be something absurdly simple, like inserting the "current" time into some table you set up.