Customer Submitted Form for Creating a Cron Job on Rails

Well, I decided to improve on what I ask for help in verses by simply opening a new question. I believe that I can do the following if I find out in what way, when the client submits the form in my Rails application, the cron task starts, in which the time for the cron task is selected by the user from the drop-down menu. Is there a way to do this, I searched for ideas for ideas, but did not find them.

Old question

I am trying to develop a system that will allow the user to upload a movie to my site and then play it at a specific time. Films will be broadcast continuously, so that at a certain time to say the next movie in line in an hour. I am wondering if there is a gem or script that is already doing this? Or that this is the best way to do this, I thought I was doing this with jobs like cron or with a delay, but I don't think this is the most efficient way to do this. Any advice would be appreciated.

ps I think an easier way to explain this: on YouTube you can queue videos for playback so that you can do something similar on rails, this will help me in solving my problem.

+4
source share
3 answers

If the user decides the exact playback time, you can use the rufus-scheduler gem.

Connect your time received from the user:

scheduler = Rufus::Scheduler.start_new scheduler.at @user_defined_time do some_method end 

Your some_method can work in tandem with a socket.io wrapper, such as Juggernaut , which will send a message to the user's browser, in which several JSs will be executed that will extract the video and play it.

What you could do with this is basically in your browser’s open window, and upon reaching the scheduled time, the browser will receive the corresponding video and play it.

If you need to implement a video queuing system, you can simply add users to the queue for the video that they want by ID, and then call the next video in the queue using the function for the callback function that starts when the video ends. The callback will query the database for the next video in the queue, retrieve the video and play it.

+2
source

I do not believe that you even need to do background material just for planning in the foreground. You don’t even have to go after the Juggernaut or with any Cron Job gun.

All of this can be achieved using JavaScript. You can use the javascript functions setTimeout () and clearTimeout () or add sleep () before calling ajax.

Here are the steps:

  • The first movie is loaded or only the page.
  • Make an ajax call inside the setTimeout function. Note. Remember to assign the variable setTimeout () to a variable so that you can execute clearTimeout (the_variable). here is a detailed use: http://www.w3schools.com/js/js_timing.asp
  • setTimeout works like setTimeout (javascript_statement, milliseconds) ... these milliseconds should be the total time of the current movie or the time set by the user subtracted from the current time, which gives you the milliseconds left to play.
  • Send the current movie id to this ajax request so that on the server you can calculate which movie you want to play after the movie you just played ... by retrieving the last movie played using this passed movie-id parameter.
  • I also believe that you will need some features, such as playing only the next movie, when the current movie ends. So basically you can also replace the setTimeout () function to create an Ajax call with the movie player function. Just make an ajax call to the server when the player finishes playing the current movie ... again sending the current movie id in the request.
  • If even after completing the current movie you want to wait for the right time to start the movie, you need to use periodically_call_remote, which sends ajax requests after a specified number of seconds.
  • And once on the server, that is, in your controller, where you process this ajax request, as soon as you make sure you need to show the movie now, just replace the player’s container with a partial one containing the player, and the link to newmovie with autorun - the load is installed To true.
+1
source

I used the hugely popular Whenever for projects that rely heavily on planned tasks, which is great. This gives you a good DSL for defining your planned tasks instead of dealing with the crontab format. From README:

 Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs. 

Example from README:

 every 3.hours do runner "MyModel.some_process" rake "my:rake:task" command "/usr/bin/my_great_command" end every 1.day, :at => '4:30 am' do runner "MyModel.task_to_run_at_four_thirty_in_the_morning" end 

Put your custom options in the above request. It is he.

+1
source

Source: https://habr.com/ru/post/1397947/


All Articles