客户提交表单以在 Rails 上创建 Cron 作业

发布于 2025-01-07 21:30:48 字数 452 浏览 5 评论 0原文

好吧,我决定改进我在诗歌方面寻求帮助的内容,只是就此提出一个新问题。我相信,如果我知道一种方法,当客户端在我的 Rails 应用程序上提交表单时,就会运行 cron 作业,其中 cron 作业的时间由用户从下拉菜单中选择,我相信我可以完成以下任务。有没有办法做到这一点,我一直在谷歌上搜索想法,但还没有找到。

老问题

我正在尝试开发一个系统,允许用户将电影上传到我的网站,然后在特定时间播放。电影将连续流式传输,以便在某个时间(例如一小时后)播放队列中的下一部电影。我想知道是否有 gem 或脚本已经做到了这一点?或者什么是最好的方法来做到这一点,我认为用 cron 或延迟作业之类的工作来做到这一点,但我不认为这是最有效的方法。任何建议将不胜感激。

ps 我认为一种更简单的解释方法是,在 YouTube 上您可以对要播放的视频进行排队,因此可以在 Rails 中做类似的事情,这将有助于解决我的问题。

Okay I decided to improve on what I am asking for help with verse just opening a new question on this. I believe I can accomplish the below if I know of a way that when a client submits a form on my Rails app a cron job is run, where the time for the cron job is selected by the user from a drop down menu. Is there a way to do this, I have been googling around for ideas but haven't found one.

Old Question

I am attempting to develop a system that would allow a user to upload a movie to my site and then have it played back at a certain time. The movies would be continuously streamed, so that at a certain time say after an hour, the next movie in the queue would play. I am wondering is there a gem or script that already does this? Or what is the best way to go about doing it, I thought doing it with jobs like cron or delayed-job, but I don't think that's the most efficient way to do this. Any advice would be appreciated.

p.s. I think a simplier way to explain it is, on YouTube you can queue up videos to be played, so could one do something similiar in rails, this would help towards my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2025-01-14 21:30:48

如果用户决定播放的确切时间,您可以使用 rufus-scheduler gem 。

插入从用户那里获得的时间,如下所示:

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

您的 some_method 可以与 socket.io< 协同工作/a> 包装器,如 Juggernaut,它会向用户的浏览器发送一条消息,浏览器会执行一些 JS会获取视频并播放。

您可以做的基本上就是打开浏览器窗口,当到达预定时间时,浏览器将获取适当的视频并播放它。

如果你需要实现一个视频排队系统,你可以让用户通过ID将他们想要的视频排队,然后通过视频结束时触发的回调函数调用队列中的下一个视频。回调将查询数据库以查找队列中的下一个视频,获取视频并播放它。

If the user gets to decide the exact time of playback, you could use the rufus-scheduler gem.

Plug in your Time obtained from the user like so:

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

Your some_method could work in tandem with a socket.io wrapper like Juggernaut, which would send a message to the user's browser, which would execute some JS that would fetch the video and play it.

What you could do with this is basically sit around with your browser window open, and when the scheduled time is reached, the browser would fetch the appropriate video and play it.

If you need to implement a video queueing system, you could just enable users to queue up the videos they want to by ID, and then call the next video in the queue via means for a callback function which is triggered when the video has ended. The callback would query the database for the next video in the queue, fetch the video, and play it.

画离情绘悲伤 2025-01-14 21:30:48

我在严重依赖计划任务的项目上使用了非常流行的 Whenever,它非常棒。它为您提供了一个很好的 DSL 来定义您的计划任务,而不必处理 crontab 格式。来自自述文件:

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

自述文件中的示例:

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

将用户定义的参数放入上面的查询中。就是这样。

I've used the extremely popular Whenever on projects that rely heavily on scheduled tasks, and it's great. It gives you a nice DSL to define your scheduled tasks instead of having to deal with crontab format. From the README:

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

Example from the 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 user defined parameters in above query. thats it.

甜心小果奶 2025-01-14 21:30:48

我认为您甚至不需要为前端调度做后台工作。
甚至不需要去寻找 Juggernaut 或任何 Cron Job 之类的东西。

这一切都可以简单地通过使用 JavaScript 来实现。
您可以使用 javascript 的 setTimeout() 和clearTimeout() 函数,或者在 ajax 调用之前添加 sleep() 。

步骤如下:

  1. 加载第一部电影或仅加载页面。
  2. 在 setTimeout 函数中进行 ajax 调用。
    注意:不要忘记将 setTimeout() 分配给变量,以便您可以执行clearTimeout(the_variable)。
    这是详细用法:http://www.w3schools.com/js/js_timing.asp
  3. setTimeout 的工作方式为 setTimeout(javascript_statement, milliseconds) ...这些毫秒应该是当前电影的总时间或用户设置的任何时间从当前时间中减去,这给你还剩几毫秒要播放。
  4. 在此 ajax 请求中发送当前电影 id,以便在服务器上您可以通过获取使用此发送的电影 id 参数播放的最后一部电影来计算刚刚播放的电影后要播放哪部电影。
  5. 我还相信您需要一些功能,例如仅在当前电影结束时播放下一部电影。所以基本上你也可以用电影播放器​​函数替换 setTimeout() 函数来进行 Ajax 调用。当电影播放器​​完成播放当前电影时,只需对服务器进行 ajax 调用...再次在请求中发送当前电影 ID。
  6. 如果即使在当前电影完成后,您想要等待适当的时间来开始电影,您需要使用periodic_call_remote,它会在设定的秒数后发送ajax请求。
  7. 一旦在服务器上,即在您处理ajax请求的控制器中,一旦您确定现在需要显示电影,只需将播放器容器替换为包含播放器的部分以及带有autoplay-on-的新电影的链接-负载设置为 true。

I don't believe you even need to do the background stuff for just front-end scheduling.
Need not to even go for Juggernaut or any Cron Job thingy.

This all can simply be achieved by using JavaScript.
You can either use javascript's setTimeout() and clearTimeout() functions or add a sleep() before your ajax call.

here are the steps:

  1. You load the first movie or just the page.
  2. Make an ajax call within the setTimeout function.
    Note: don't forget to assign the setTimeout() to a variable so that you can do clearTimeout(the_variable).
    here is the detailed usage: http://www.w3schools.com/js/js_timing.asp
  3. setTimeout works as setTimeout(javascript_statement, milliseconds) ... these milliseconds should be the total time of the current movie or whatever time that's being set by the user subtracted from the current time which gives you milliseconds left to be played.
  4. Send the current movie id in this ajax request, so that on server you can calculate which movie is to be played after the movie just played... by fetching the last movie played with this sent movie-id parameter.
  5. I also believe you'd require some functionality like, only play the next movie when the current movie ends. So basically you can also replace the setTimeout() function for making the Ajax call with the movie-players function. Just make an ajax call to server when the movie-player completes playing the current movie... again sending the current movie-id in the request.
  6. If even after the completion of current movie, you want to wait for the appropriate time to start the movie, you need to make use of periodically_call_remote which sends a ajax requests after a set number of seconds.
  7. And once on server, i.e. in your controller where you handle that ajax request, once you make sure you need to show the movie now, just replace the player container with the partial containing the player and the link to the newmovie with autoplay-on-load set to true.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文