耙任务不起作用的自毁功能

Self-destructing feature with rake task not working

本文关键字:功能 任务 不起作用      更新时间:2023-09-26

我正在尝试在我的待办事项列表应用程序中创建一个功能,在该功能中,待办事项将在7天后自毁。然而,在我现在的代码中,从创建todo的日期开始,天数是递增的,而不是递减然后被销毁。任何关于如何解决这个问题的想法都将不胜感激。

以下是我要做的部分视图:

 <tr id="<%= dom_id(todo)%>">
    <td><%= todo.description %></td>
    <td><%= distance_of_time_in_words_to_now(todo.created_at + 7.days) %></td>
    <td><%= link_to "Completed", todo, method: :delete, remote: true, class: "btn btn-success" %></td>
  </tr>

这是我的rake任务代码:

desc 'Removes items over a week old'
task delete_items: :environment do
  Todo.where("created_at <= ?", Time.now - 7.days).destroy_all
end

这是我的destroy.js.erb文件:

<% if @todo.destroyed? %>
   $('#todo_' +<%= @todo.id %>).hide();
   $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:notice] %>");
   $('.alert').addClass('alert-success');
   $('.alert').removeClass('remove');
<% else %>
   $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:error] %>");
   $('.alert').addClass('alert-danger');
   $('.alert').removeClass('remove');
<% end %>

这是我的create.js.erb文件:

 <% if @todo.valid? %>
   $('.js-todos').prepend("<%= escape_javascript(render(@todo)) %>");
   $('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>");
   $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:notice] %>");
   $('.alert').addClass('alert-success');
   $('.alert').removeClass('remove');
 <% else %>
   $('.alert').html("<button type='button' class='close' data-dismiss='alert'>&times;</button><%= flash[:error] %>");
   $('.alert').addClass('alert-danger');
   $('.alert').removeClass('remove');
   $('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>")
 <% end %>

如果您需要查看其他文件,请告诉我。再次感谢!

编辑:添加的文件:

这是我的日程安排。rb代码:

set :output, "#{path}/log/cron.log"
every 1.day do
  rake "delete_items"
end

这是我的新deploy.rb文件:

require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'my_app_name'
set :repo_url, 'git@example.com:me/my_repo.git'

我是否需要在set:application/set:repo_url中填充deploy.rb文件才能工作?谢谢

您的表将始终递增。忘记distance_of_time_in_words方法的实现复杂性,只需考虑它的含义:如果你今天创建一个项,那么它明天的时间距离将是1天。然后后天两天。然后3天。将7天添加到创建的时间中只会使其为-6天,然后为-5天,依此类推(忽略d_o_t_i_w_f_n如何格式化它)。当你点击0时,todo应该被删除,并且不会显示在表上,但表只是显示一些东西——它与项目是否被删除没有实际关系;它只是通知用户。删除由您编写的rake任务负责,当运行时,该任务似乎正在运行。

听起来问题是你的任务根本没有运行。根据您的报告,当您手动运行它时,它是工作的,但您的调度程序无法定期运行它。你安排得怎么样?调度程序是否不同时在本地和生产环境中工作?你用什么工具来安排日程,你是如何使用它们的?

因为你的表和调度程序是单独的问题,我建议你问一个关于rake调度失败的新问题,在这个问题中,你发布了你的调度代码(如果你使用了什么宝石?你是如何使用它们的?在本地和生产上显式运行任务(在调度程序之外)的行为是什么?),并且尝试确定调度rake任务的哪个部分失败。

你的表格看起来基本正确。如果它无法显示所需的单词/数字,那只是distance_of_time_in_words方法的一个函数,以及你如何使用它

include ActionView::Helpers::DateHelper # Module of which d_o_t_i_w_f_n is a method
distance_of_time_in_words_from_now(5.days.ago)
distance_of_time_in_words_from_now(24.hours.from_now)

事实上,您想要删除的项目显示在表中,这可能与日程安排有关,而与表逻辑无关,所以在这个问题上,我只关注日程安排。