我的工作流程是电子邮件--sendgrid--griddler,rails应用程序在heroku上运行。所有入站电子邮件都有附件,有些还相当大。我一直在Heroku上看到H12超时,因为上传到Google Cloud Storage的附件需要超过30秒。
我已经尽可能地使用了延迟作业,但我不认为我可以将来自griddler的附件传递给延迟作业,因为附件是短暂的。我的一个朋友建议我直接从gmail中检索电子邮件,而不是使用sendgrid和griddler,但这更像是一次重写,而不是我目前所能接受的。在理想的情况下,我可以将附件传递给延迟的作业,但我不知道这最终是否可能。
email_processor.rb
if pdfs = attachments.select { |attachment| attachment.content_type == 'application/pdf' }
pdfs.each do |att|
# att is a ActionDispatch::Http::UploadedFile type
# content_type = MIME::Types.type_for(att.to_path).first.content_type
content_type = att.content_type
if content_type == 'application/pdf'
# later if we use multiple attachments in single fax, change num_pages
fax = @email_address.phone_number.faxes.create(sending_email: @email_address.address ,twilio_to: result, twilio_from: @email_address.phone_number.number, twilio_num_pages: 1)
attachment = fax.create_attachment
# next two rows should be with delay
attachment.upload_file!(att)
#moved to attachment model for testing
#fax.send!
end
end
file upload from another model
def upload_file!(file)
# file should be ActionDispatch::Http::UploadedFile type
filename = file.original_filename.gsub(/\s+/, '_')
filename = filename[0..15] if filename.size > 16
path = "fax/#{fax.id}/att-#{id}-#{filename}"
upload_out!(file.open, path)
#self.fax.send!
#make_thumbnail_pdf!(file.open)
end
def upload_out!(file, path)
upload = StorageBucket.files.new key: path, body: file, public: true
upload.save # upload file
update_columns url: upload.public_url
self.fax.update(status: 'outbound-uploaded')
self.fax.process!
end发布于 2019-06-19 14:30:46
如果你不能在30秒内接收并上传附件,那么heroku将无法接收电子邮件。您是对的- web dyno上的临时存储不能从运行延迟作业的worker dyno访问。
即使worker dyno可以从web dyno的临时存储中读取数据,如果附件足够大,也不能保证web dyno能够在30秒内处理来自sendgrid的帖子。
一种选择是配置sendgrid将电子邮件直接转发到您的谷歌应用程序引擎- https://cloud.google.com/appengine/docs/standard/python/mail/receiving-mail-with-mail-api
你的应用程序引擎脚本可以将附件写入google云存储,然后你的应用程序引擎脚本可以向heroku应用程序发送带有附件位置的POST,然后web应用程序可以将延迟的作业排队以下载和处理附件。
发布于 2019-06-20 05:13:50
我最终完全重写了我的电子邮件处理程序。我将gmail设置为邮件目标,然后使用Heroku的计划任务来处理电子邮件(查找未读),然后将附件上传到Google Cloud Storage。使用计划任务可以绕过H12问题。
https://stackoverflow.com/questions/56636797
复制相似问题