Gregory Frerot
Gregory Frerot

Reputation: 1360

Execute "assets:precompile" by code/client

I have a site that has a lot of images (+ 10k) with several hundreds displayed by pages, so I use the principle of ruby on rails assets to compile these images. The problem is that my client has the possibility to change these images via an FTP and it forces me to connect each time in ssh to the server to launch the command "assets: precompile". Would there be a way to create a link button my client will click to launch the command from the code.

I tried to create a cron job with the Whenever gem (https://github.com/javan/whenever) that runs the "assets: precompile" command every hour but it does not work.

every :hour do
  rake "assets:precompile"
end

I tried to create this method linked to a link but without success also

def compile_image
  system('rvm use 2.2.0@project && cd /var/www/project-folder/ && rake assets:precompile')
  redirect_to imports_index_path, notice: 'Images compiled'
end

I'm on ruby 2.2 and rails 4.2.

Thanks for your help

Upvotes: 0

Views: 165

Answers (1)

Richard-Degenne
Richard-Degenne

Reputation: 2959

Asset compilation should only be used for static images used in your app (icons, backgrounds...), not for the actual data your site uses.

This is especially true if it is a dynamic and large collection of images.


This isn't really a programming problem anymore, it's more of a general software engineering problem, but here are my two cents anyways.

I'd suggest you use a CDN to make the photos available to browsers. Your Rails app should store (or build) the URLs to the CDN, so that the client browser can fetch and display them.

For instance, set up a HTTP server such as Nginx on the same machine that hosts the FTP server, then build a consistent URL schema so that your FTP files can be served through HTTP, without copy.

New files added to the server via FTP are automatically available through HTTP, and maybe a cron job or an asynchronous worker can check for new files and register them to your Rails app.

Of course, that's just an example of what can be done. There are tons of other solutions, but without further details, it's hard to tell.

Upvotes: 1

Related Questions