Ruby On Rails : Mailer : Sidekiq - asset path not valid in email : Engineyard : Nginx : Passenger

684 views Asked by At

I have used Sidekiq for processing emails asynchronously. It works well in development env however it does not render a valid asset URL in email published.

Expected Asset URL:
http://myapp.com/assets/logo-277121cb27cd5798ea5786fa2996c82f.png

Got:
http://myapp.com/images/logo.png

I am using Engineyard as VPC. In production/staging servers I have a dedicated utility instance named redis which comprises of redis server and sidekiq instances running. They are processing email properly so I guess they are in good condition.

nginx configuration in /data/nginx/servers/my_app.conf
cat /data/nginx/servers/my_app.conf

server {

  listen 81;

  server_name _;


  client_max_body_size 100M;

  root /data/my_app/current/public;

  access_log /var/log/engineyard/nginx/my_app.access.log main;
  error_log /var/log/engineyard/nginx/my_app.error.log notice;

  location ~ ^/(images|assets|javascripts|stylesheets)/ {
    expires 10y;
    try_files  $uri $uri/index.html /last_assets/$uri /last_assets/$uri.html @app_my_app;
  }

  error_page 404 /404.html;
  error_page 500 502 504 /500.html;
  error_page 503 @503;
  recursive_error_pages on;
  location @503 {

    error_page 405 = /system/maintenance.html;

    if (-f $request_filename) {
      break;
    }

    rewrite ^(.*)$ /system/maintenance.html break;
  }

  location @app_my_app {

    passenger_enabled on;

    passenger_set_cgi_param HTTP_X_FORWARDED_FOR   $proxy_add_x_forwarded_for;
    passenger_set_cgi_param HTTP_X_REAL_IP         $remote_addr;
    passenger_set_cgi_param HTTP_HOST              $http_host;
    passenger_set_cgi_param HTTP_X_FORWARDED_PROTO $scheme;
    passenger_set_cgi_param HTTP_X_REQUEST_START   't=$start_time';
    passenger_set_cgi_param HTTP_X_QUEUE_START     't=$start_time';

    passenger_set_cgi_param SERVER_PORT            80;

    rack_env staging;

    passenger_min_instances 2;
  }

  location / {
  if (-f $document_root/system/maintenance.html) { return 503; }
    try_files  $uri $uri/index.html $uri.html @app_my_app;
  }
include /etc/nginx/servers/my_app/custom.conf;
}

and I have used a helper method to get Organization's logo url

  # Gets the logo URL of the Organization set by Controller/Mailer
  #   in instance variable @organization
  # @param [Organization] organization default value is nil
  # @return [String] URL for logo image
  def get_logo_url(organization = nil)
    if organization && organization.logo.present?
      # This case works as AWS S3 is used
      organization.logo_url(:logo)
    else
      # This case not working
      asset_url('logo.png')
    end
  end

Latest Finding:
I tried to send email from main app without using sidekiq and It works in that case. The asset helpers like asset_url, asset_path, image_path are not able to get the diegsted file name from the manifest file.

4

There are 4 answers

0
Shiva On BEST ANSWER

Note: As a temp fix / Best solution still seeking

Since sidekiq had been facing issues with serving static assets stored as assets/images/logo.png.

Expected Asset URL:

http://myapp.com/assets/logo-277121cb27cd5798ea5786fa2996c82f.png

Got:
http://myapp.com/images/logo.png

so I actually moved the static asset files that I may need in email to public/images directory. Now instead of Rails app, Nginx serves the assets. Now its working

My latest finding (doubt) : I doubt that my Chef cookbooks are unable to start the sidekiq and corresponding Rails instance for sidekiq in production / staging environment.

0
Ralph On

I am a Team Lead for Engine Yard. If you could open a ticket we could investigate a bit more on your behalf.

Thanks,

Ralph

0
komaldhanwani On

If someone is using capistrano 3 then he needs to include sidekiq role(or role of server where your sidekiq is deployed) .... otherwise capistrano is compiling assets only for web role

server 'testanywebsite.com', user: 'deploy-user', roles: %w(sidekiq)
set :assets_roles, [:web, :sidekiq]
2
iHiD On

I was facing the same issue. The problem was that I wasn't compiling assets on the sidekiq server, so there wasn't a manifest there.

A change in Capistrano from: task :compile_assets, roles: :app, primary: true do

to: task :compile_assets, roles: [:app, :processor], primary: true do

fixed it in my personal case.