【发布时间】:2014-01-14 00:58:14
【问题描述】:
我正在使用 EventMachine 处理传入的电子邮件,这些电子邮件有时可能非常大。到目前为止,我所拥有的代码绝对适用于间隔至少约 5 秒的电子邮件,但低于此值的某个地方,无论有多少电子邮件到达,都只会处理一封电子邮件。我尝试在几个不同的地方添加 EM.defer 语句,我认为这会有所帮助,但无济于事。我还应该注意,如果有什么不同的话,我也在这个例子中使用了 em-imap gem。
代码的相关部分在这里:
EM.run do
client = EM::IMAP.new('imap.gmail.com', 993, true)
client.connect.bind! do
client.login('me@email.com', 'password123')
end.bind! do
client.select('INBOX')
end.bind! do
client.wait_for_new_emails do |response|
client.fetch(response.data).callback do |fetched|
currentSubjectLine = fetched.first.attr.values[1].subject
desiredCommand = parseSubjectLine(currentSubjectLine)
if desiredCommand == 0
if fetched.first.attr.values[0].parts.length == 2
if fetched.first.attr.values[0].parts[1].subtype.downcase != "pdf"
puts 'Error: Missing attachment, or attachment of the wrong type.'
else
file_name = fetched.first.attr.values[0].parts[1].param.values[0]
client.fetch(response.data, "BODY[2]").callback do |attachments|
attachment = attachments[0].attr["BODY[2]"]
File.new(file_name,'wb+').write(Base64.decode64(attachment))
end
end...
我是否以某种方式阻塞了此代码段中的反应器?我正在使用的某些库是否可能不适合这里? GMail 的 IMAP 服务器可能与它有关吗?在您自信地回答之前,您是否需要更多关于在特定情况下会发生什么的信息?与往常一样,非常感谢任何帮助。谢谢!
使用最小化代码更新
以防万一我的组织中的任何事情与它有关,我将包括我认为可能相关的所有内容。
module Processing
def self.run
EM.run do
client = EM::IMAP.new('imap.gmail.com', 993, true)
client.connect.bind! do
client.login('me@email.com', 'password123')
end.bind! do
client.select('INBOX')
end.bind! do
client.wait_for_new_emails do |response|
client.fetch(response.data).callback do |fetched|
puts fetched[0].attr.values[1].subject
end
end
end.errback do |error|
puts "Something failed: #{error}"
end
end...
Processing.run
【问题讨论】:
-
我根本不了解 ruby,但是如果您一次收到不止一封电子邮件会怎样?你只看
fetched.first?首先是什么? -
啊,谢谢@Max,但不幸的是,这并没有解决它。每封电子邮件在传入时都被转换为“已获取”,“已获取”是一个数组,其中包含存储在不同位置的电子邮件的不同信息位。主题行恰好存储在该数组的第一部分中的某个位置,这就是我在寻找那里的原因。 fetched 的所有部分都指向同一封电子邮件。
标签: ruby concurrency imap eventmachine