Avoid Heroku from cleaning some assets - ruby-on-rails

I have a rails app hosted on Heroku. I added a "Audio" folder in Assets. The "Audio" folder is not included into my git pushes. My app generates some audio files that are automatically added to the assets/audio folder. That's why I don't want Heroku to clean the assets inside this particular folder each time I push a modification.
One solution would be to host the generated audio files with AWS S3 but it's quite a lot of work to set up.
I wonder if, instead, there is a way to tell Heroku not to clean the assets in the "Audio" folder?
I searched on the internet but didn't find anything so far...

So far I know Heroku will clean you temporary assets everytime you deploy your application and the build starts.
Check this answer: Store file in directory tmp on heroku Rails
If your application is Rails 5.2 based you can implement your upload to AWS via Active Storage, check: https://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond

Related

Folder contents are automatically deleted every time I deploy my Rails app

I have a directory under /public folder with the use of CarrierWave I store all my PDF files under this dir. But the problem is all the time I deploy new version of my Rails app this directory gets cleaned up and the all files are missing. This directory is was set under my uploader.
I also have a directory named "private" which I created manually in order to not to serve sensitive files globally on WEB. Those files also gone after new deployment process.
How can I prevent files from deleting on deploy process?
Thanks.
I assume you are using some automation for deployment. Because if you are updating your code on server instance manually then you can preserve pre uploaded file, but using manually method to update code is not a good practice.
So in automation deployment we generally follow this kind of flow.
Whenever you deploy that create a new deploy version and set that as current version.
Simply that means it's creating a new directory and placing your rails project in it. Now the files you are storing inside the project directory are there in the previous version those are not gone if you are using any linux instance.(Only if you have setup that way to preserve last few versions to restore incase of new deploy is exploded)
Clear till now?
Not suppose you are not keeping any previous version, your files are gone forever.
So it's not a good idea to store any file under project repository.
Best practice is to use bucket system like AWS bucket or Google cloud bucket, where we store all the uploaded file. If having bucket is not in budget, you can choose a different directory on linux server instance outside of project directory. But you have to setup all those upload paths and directory system to be used as bucket.
This problem I am facing with is happening because of capistrano. Every time I run cap production deploy command on my server, the capistrano deployment tool syncs every file with git repo. And the files added by end-users are not stored under my git repos of course, so capistrano overwriting the empty public folder from my repo to the server. Adding the path to :linked_dirs variable under deploy.rb solved my problem.
Another approach could be using a directory which is somewhere else than your project root path (such as /home/files) to store all your files. By doing this you will be seperating your files from project and also prevent capistrano's overwriting problem.
Hope this information will be useful for someone or future me :) ..
When you deploy with capistrano, a new deploy(folder) is created from the repository.
Any files not in the repository are not carried over.
If you want to persist files in public, you need to create a directory in your server first and then create a symlink with capistrano inside public to that folder.
Then have your carrierwave uploads saved to that location.
During each deployment cap will symlink to that directory and your files will be there

Ruby on Rails app on Heroku file upload not working

I'm new to Rails and Heroku as well.
Following tutorials I succeed to upload my app to Heroku.
My app is enable us to upload csv file to uploads folder in public.
So I can upload file in heroku deployed app and show table as I want.
But after 30 mins or so those files are removed automatically and showing error that no such file in public/uploads folder.
It is properly working on my local which is using sqlite3.
But heroku want to use psql for production mode so I changed production mode to use psql.
So if I upload csv file then it is stored in 'public/uploads' folder.
I think heroku app is automatically update to github repo even I didn't execute 'git push heroku master' command.
In my local repository files are stored but they are run on development mode.
How can I make upload functionality fully working?
Any help would be appreciate.
Ephemeral filesystem is just the way heroku works, as explained in the article - uploaded files will be removed when the instance is restarted.
What you should do is to upload those files for example to S3 (https://devcenter.heroku.com/articles/s3), you can achieve this with CarrierWave gem with Fog gem but there are other possibilities as explained in this thread here: https://www.reddit.com/r/rails/comments/2k9sq4/heroku_any_files_you_upload_will_not_be_saved/

Public assets with Capistrano and Rails not being uploaded

I have several sounds files that are located in public/assets/sounds.
Locally everything works fine, but when I deploy via Capistrano to my ec2 instance, none of those assets make it to the server. I added 'public/assets/sounds' to :linked_dirs in deploy.rb. A directory shows up at 'public/assets/sounds' but none of the mp3s are there. Do I need to manually add all files via :linked_files?
I have it working by just loading the files into the shared/public/assets/sounds directory via ftp, but that doesn't seem like the best use of the Capistrano. I'm also new to Capistrano and could be totally wrong :p
The public/assets directory is reserved for the Rails asset pipeline. You should not place any files there. Here's what I would do:
Remove public/assets/sounds from :linked_dirs.
Choose a different place for the mp3 files, like public/sounds.
Do not add this directory to :linked_dirs.

Rails & Heroku: Can't write into public directory

I write a script to fetch content from other site, and save it to my public/ directory. Due to my network's poor environment, I deployed it to Heroku and wish it do it instead of me doing it locally.
Just something simple like this
movie_file = "#{Rails.root}/public/movie_list#{year}.json"
File.open(movie_file, "w"){|f| f.write(JSON.pretty_generate($movie_list))}
However, when I run it in heroku(just a rake task), it seems like it can't write into the public/ directory, I get no such page error. And I find this answer: Problems with public directory when deploying Node.js app with Heroku
But the original article is unavaible in heroku, and I'm not sure if its still true.
I'm wondering:
If there is any workaround that I can save it to the server (maybe something other than the public/), and then I can download it to my computer?
Or, Instead of wrting the file into public/, maybe I can upload it to other free space?
======================
UPDATE:
Finally, I first save file to tmp, and then save it to Qiniu(An China counterpart as AWS), and you can save it to AWS
The storage on an Heroku dyno should be regarded as ephemeral, as a dyno restart will cause saved files to disappear, and the file would not be visible from other dynos.
You should use the dyno to upload your files to permanent storage, such as AWS, from which you can download it through your browser.
No permanent filesystem for Heroku?

Pushing RoR app to Heroku, files in /public are missing

Let me start by saying I am using Ruby 2.0.0 and Rails 4.1.1
I've worked my way through the Treehouse basic RoR course; ending in a very basic version of Twitter. I have the application running just fine on my local install, but when I pushed it to Heroku it seems to be missing the files in the /public directory; namely the /assets css and javascript.
I've precompiled my assets as instructed, and verified that they area indeed showing up on my GitHub remote that is using the same branch. I was told that Heroku will not compile your assets for you.
All my routes and HTML is displaying just fine, but I cannot pull any of the files that live in the /public directory (404.html, 500.html, etc)
It feels to me like it is a permissions issue or something with the /public directory, but I haven't found a way to actually browse what files are on my Heroku instance. I've tried re-pushing several times while making small changes, and the css/js never seems to appear.
In case that you have already set:
config.serve_static_assets = true
in your config/environments/production.rb
And still not working, you can actually see the logs from your heroku app using heroku logs or heroku logs -n NUMBER_OF_DESIRED_LINES_HERE in your terminal.

Resources