Search code examples
nginxpassengeractionmailersidekiqengineyard

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


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.


Solution

  • 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.