Problem: attemping to develop a cron job or delayed job that delivers autoresponder email messages. Turns out it's a trickier problem that I thought. Here's my model:
问题:试图开发一个cron作业或延迟作业,以交付自动回复的电子邮件消息。原来这是一个更棘手的问题。这是我的模型:
class List < ActiveRecord::Base
has_many :subscriptions
has_many :contacts, :through => :subscriptions
has_many :messages
class Subscription < ActiveRecord::Base
belongs_to :contact
belongs_to :list
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :list
-----------Lists------------
| |
| |
Subscriptions (join) Messages
|
|
Contacts
My goal is to loop through all subscriptions, and for each subscription check to see if the "days since subscription" matches "days after signup" attribute on the message object. If true, deliver the message to recipient, then continue looping through the others.
我的目标是对所有订阅进行循环,并对每个订阅检查,以查看在消息对象上的“订阅后的天数”是否与“注册后的天数”相匹配。如果为真,则将消息传递给收件人,然后继续在其他消息之间循环。
class SubscriptionsController < ApplicationController
def deliver
@subscriptions = Subscription.all
@subscriptions.each do |subscription|
subscription.list.messages.each do |message|
if message.days_since == subscription.days_after
# deliver message through mailer in a scheduled delayed_job
else
end
end
end
end
If someone subscribed to a list 2 days ago, and the list owner wanted a message to be delivered 2 days after signup, then the message should be delivered because it's a match.
如果有人在2天前订阅了一个列表,而列表所有者希望在注册后2天发送一条消息,那么该消息应该被发送,因为它是匹配的。
The deliver method above is my attempt to loop through all subscriptions, and compare the subscription.days_since with message.days_after_signiup.
上面的交付方法是我尝试遍历所有订阅并比较订阅。days_since message.days_after_signiup。
Any advice or ideas would be very much appreciated. Really been struggling with this one, and it would feel great to get through this roadblock.
如有任何建议或想法,我们将不胜感激。真的一直在和这个问题做斗争,如果能通过这个障碍,感觉会很好。
D.
D。
2 个解决方案
#1
1
Couldn't you just do something with a where or join statement?
你就不能用where或join语句做点什么吗?
messages = Message.joins(:subscriptions).where("messages.days_since > subscriptions.days_after")
messages.each do |message|
message.deliver
end
#2
1
I'd just add a send_by
to Subscriptions and use the use the range between last_cron_run and Time.now to see if any Subscription.where(:send_by => last..now)
fall into that range.
我只需向订阅添加send_by并使用last_cron_run和Time之间的范围。现在看看是否有订阅。在哪里(:send_by => .now)落在那个范围内。
I'd also calculate the send_by data at time of subscription.
我还将在订阅时计算send_by数据。
You can also calculate how to send the next ones this way and your cron job just runs based on 14 minute blocks(00 - 14) and check the minutes of the send_by
. That's if you set it for 15 minutes. If not, use whatever you decide to set your timing by, just chop off the last minute of that range (e.g., if an hour, use 59 mins instead).
您还可以计算如何以这种方式发送下一个命令,您的cron作业仅基于14分钟块(00 - 14)运行,并检查send_by的分钟数。如果你设置15分钟。如果没有,那就用你决定的时间来决定你的时间,只要把这段时间的最后一分钟砍掉(比如,如果一个小时,用59分钟代替)。
#1
1
Couldn't you just do something with a where or join statement?
你就不能用where或join语句做点什么吗?
messages = Message.joins(:subscriptions).where("messages.days_since > subscriptions.days_after")
messages.each do |message|
message.deliver
end
#2
1
I'd just add a send_by
to Subscriptions and use the use the range between last_cron_run and Time.now to see if any Subscription.where(:send_by => last..now)
fall into that range.
我只需向订阅添加send_by并使用last_cron_run和Time之间的范围。现在看看是否有订阅。在哪里(:send_by => .now)落在那个范围内。
I'd also calculate the send_by data at time of subscription.
我还将在订阅时计算send_by数据。
You can also calculate how to send the next ones this way and your cron job just runs based on 14 minute blocks(00 - 14) and check the minutes of the send_by
. That's if you set it for 15 minutes. If not, use whatever you decide to set your timing by, just chop off the last minute of that range (e.g., if an hour, use 59 mins instead).
您还可以计算如何以这种方式发送下一个命令,您的cron作业仅基于14分钟块(00 - 14)运行,并检查send_by的分钟数。如果你设置15分钟。如果没有,那就用你决定的时间来决定你的时间,只要把这段时间的最后一分钟砍掉(比如,如果一个小时,用59分钟代替)。