class RefreshReportDataJob < ApplicationJob
include SuckerPunch::Job
queue_as :default
def perform()
ActiveRecord::Base.connection_pool.with_connection do
user = User.find(id)
# 1. a) Run SQL1
ApplicationRecord.connection.execute("<execute SQL1>")
# 1. b) Run SQL2
ApplicationRecord.connection.execute("<execute SQL2>")
# 2. render erb template
excel_report = ApplicationController.new.render_to_string(
:template => 'forecast/show_xlsx'
)
# 3. Upload Excel generated in step 2 to AWS bucket
s3 = Aws::S3::Resource.new(region: 'us-west-2')
my_bucket = s3.bucket('my-bucket')
my_bucket.create
name = File.basename 'MyExcel'
obj = s3.bucket('my-bucket').object(name)
obj.upload_file('MyExcel')
# 4. Send an email to the user stating the Excel is available in the AWS bucket
sender = 'sender@gmail.com'
recipient = 'receiver@gmail.com'
subject = 'Amazon SES test (AWS SDK for Ruby)'
# The HTML body of the email.
htmlbody = '<h1>Amazon SES test (AWS SDK for Ruby)</h1>'\
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'\
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-ruby/">'\
'AWS SDK for Ruby</a>.'
# The email body for recipients with non-HTML email clients.
textbody = 'This email was sent with Amazon SES using the AWS SDK for Ruby.'
# Specify the text encoding scheme.
encoding = 'UTF-8'
# Create a new SES client in the us-west-2 region. # Replace us-west-2 with the AWS Region you're using for Amazon SES.
ses = Aws::SES::Client.new(region: 'us-west-2')
# Try to send the email.
begin
# Provide the contents of the email.
ses.send_email(
destination: {
to_addresses: [
recipient
]
},
message: {
body: {
html: {
charset: encoding,
data: htmlbody
},
text: {
charset: encoding,
data: textbody
}
},
subject: {
charset: encoding,
data: subject
}
},
source: sender
)
end
end
end
end
У меня есть несколько вопросов здесь:
- Будут ли SQL 1. a) и 1. b) выполняться последовательно?
- В настоящее время SQL работает нормально, но на шаге 2 ничего не работает
- Можно ли отобразить шаблон erb из функции ActiveJob?
- Можно ли загрузить файлы в корзину AWS из функции ActiveJob?
Могу ли я отправить электронное письмо, используя учетную запись AWS SES (используя AWS SDK) из функции ActiveJob?
Теперь я также получаю сообщение о том, что мой шаблон не был найден; Я не уверен, почему я получаю эту ошибку:
ActionView::MissingTemplate Missing template my_controller/my_template with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :axlsx]}. Searched in:
* "/home/ubuntu/workspace/rails/Mailer/cfps_app/app/views"
Мой шаблон присутствует внутри app/views/my_controller
, а в приведенной выше ошибке указано searched in app/views
- это проблема? - означает ли это, что Rails ищет во всех подпапках в app/views
?
Кроме того, мой шаблон генерирует файл .xlsx
.
а. Может ли render_string
вернуть только строку? или он также может вернуть Excel?
б. Как загрузить Excel, возвращенный на предыдущем этапе, в корзину AWS?
Пожалуйста, помогите!