简体   繁体   English

使用rails 3中的任何时候gem实现计划任务

[英]Implementing a schedular task using whenever gem in rails 3

I'm trying to implement a scheduler task that deletes a user in user table who got abused more than 5 times.我正在尝试实现一个调度程序任务,该任务删除用户表中被滥用超过 5 次的用户。 To achieve this in the user.rb file I have return a method report_abuse_delete method which performs the functionality of finding the user who got abuses more than 5 times and delete his records from the database.为了在 user.rb 文件中实现这一点,我返回了一个方法 report_abuse_delete 方法,该方法执行查找滥用超过 5 次的用户并从数据库中删除他的记录的功能。

Here is my method in User model:这是我在用户 model 中的方法:

def report_abuse_delete

@delete_abused_user= Abuse.find(:all, :conditions=>['count>=?',5])

 @delete_abused_user.each do |d|

    @abused_user= User.find(d.abuse_id)

      if  @abused_user.delete

         render :text=> "User account has been deleted. Reason: This user has been reported spam for more than 5 times"

        UserMailer.user_delete_five_spam_report(@user).deliver

      end

 end
end

And this is what I have written in the Scheduler.rb file这就是我在 Scheduler.rb 文件中所写的

every 2.minutes do
   rake "log:clear", :environment => "development"

   runner "User.report_abuse_delete", :environment => "development"
end

As you can see in the scheduler.rb file I'm trying to perform a 2 functions one is clearing my log for every 2minutes and trying to run a method report_abuse_delete that I wrote in my model.正如您在 scheduler.rb 文件中看到的那样,我正在尝试执行 2 个功能,其中一个是每 2 分钟清除一次日志并尝试运行我在 model 中编写的方法 report_abuse_delete。

I'm facing a issue as follows for every 2 minutes my log is getting cleared but the method which I wrote in the model in not getting invoked I guess the functionality is not getting triggered.我每 2 分钟就会遇到如下问题,我的日志被清除,但是我在 model 中编写的方法没有被调用,我猜这个功能没有被触发。 I have searched all the web and checked every possible way.我已经搜索了所有 web 并检查了所有可能的方式。 I'm unable to figure out what was the problem is.我无法弄清楚问题出在哪里。

Help me out please.请帮帮我。 Any kind of help is welcome and appreciable.任何形式的帮助都是值得欢迎和赞赏的。

You've defined report_abuse_delete as a normal - that is instance - method, but you're calling it as a class method.您已将report_abuse_delete定义为普通(即实例)方法,但您将其称为class方法。 Try defining the method as def self.report_abuse_delete .尝试将方法定义为def self.report_abuse_delete

Also, I don't know if the render call will work: I haven't used this gem, but since you don't have any kind of user agent to see the text, I'm not sure what you'd expect it to do.另外,我不知道render调用是否会起作用:我没有使用过这个 gem,但是由于你没有任何类型的用户代理来查看文本,我不确定你会期待什么去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM