April 3, 2019
This blog is part of our Rails 6 series.
We use the db:seed
task to seed the database in Rails apps. Recently an
issue was reported on Rails issue
tracker where the db:seed
task was not finishing.
In development environment, Rails uses async adapter as the default Active Job adapter. The Async adapter runs jobs with an in-process thread pool.
This specific issue was happening because the seed task was trying to attach a file using Active Storage. Active Storage adds a job in the background during the attachment process. This task was not getting executed properly using the async adapter and it was causing the seed task to hang without exiting.
It was found out that by using the inline adapter in development environment, this issue goes away. But making a wholesale change of making the default adapter in development environment as inline adapter defeats the purpose of having the async adapter as default in the first place.
Instead a change is made to execute all the code related to seeding using inline adapter. The inline adapter makes sure that all the code will be executed immediately.
As the inline adapter does not allow queuing up the jobs in future, this can result into an error if the seeding code somehow triggers such jobs. This issue is already reported on Github.
Active Job is optional framework and we can skip it completely. Now that seeding
depends on presence of Active Job, it was throwing an error when Active Job was
not part of the application. Also, executing the jobs inline automatically, when
users has set the Active Job queue adapter to something of their choice was
surprising for the users. So a
change has been made to load the
seeds inline only when Active Job is included in the application and the queue
adapter is async
. This makes it backward compatible as well it does not change
user's choice of queue adapter automatically.
If this blog was helpful, check out our full blog archive.