activejob error - Unable to autoload constant for gem that exists - ruby-on-rails

On my project we are using Docker, Rails, Sidekiq and the hyrax and bulkrax gems. But after submitting a job we are seeing errors like this in the logs.
[ActiveJob] [Bulkrax::ImportWorkJob] [c51d6569-3b0e-46f0-be44-e84a0224448e] Error performing Bulkrax::ImportWorkJob (Job ID: c51d6569-3b0e-46f0-be44-e84a0224448e) from Sidekiq(import) in 1633.9ms: LoadError (Unable to autoload constant Hyrax::Actors::OptimisticLockValidator, expected /usr/local/bundle/gems/hyrax-3.4.1/app/actors/hyrax/actors/optimistic_lock_validator.rb to define it):
It appears that it cannot find the files to the hyrax gem. I verified they do in fact exist inside the container.
docker-compose.yml
version: '3.4'
# used for local testing
services:
hyrax:
build:
context: ./
dockerfile: Dockerfile
container_name: hyrax
command: bash -c "rm -f tmp/pids/server.pid ; rm -rf tmp/cache ; bundle exec rails s -p 3000 -b 0.0.0.0"
env_file:
- './env/.env.dev.hyrax'
restart: on-failure
depends_on:
- db
- fcrepo
- memcached
- postgres
- redis
- solr
ports:
- 3000:3000
volumes:
- ./hyrax:/home/hyrax
- ./scripts:/home/hyrax/scripts
- ./hyrax-exports/:/home/hyrax/imports
- bundle_data:/usr/local/bundle
- hyrax_tmp:/home/hyrax/tmp
networks:
- hyrax
sidekiq:
container_name: sidekiq
build:
context: ./
dockerfile: Dockerfile
command: bundle exec sidekiq -C config/sidekiq.yml
env_file:
- './env/.env.dev.hyrax'
restart: always
volumes_from:
- hyrax
depends_on:
- redis
networks:
- hyrax
memcached:
image: bitnami/memcached
container_name: memcached
ports:
- "11211"
networks:
- hyrax
redis:
image: redis:alpine
container_name: redis
command: redis-server
ports:
- "6379"
volumes:
- redis:/var/lib/redis/data
networks:
- hyrax
restart: unless-stopped
healthcheck:
test: redis-cli -h redis ping
interval: 30s
timeout: 3s
retries: 3
fcrepo:
image: ghcr.io/samvera/fcrepo4:4.7.5
container_name: fcrepo
restart: on-failure
volumes:
- fcrepo:/data:cached
ports:
- "8080:8080"
networks:
- hyrax
solr:
container_name: solr
image: solr:8.11.1
restart: on-failure
ports:
- "8983"
command:
- sh
- "-c"
- "precreate-core hyrax_test /opt/solr/server/configsets/hyraxconf; solr-precreate hyrax_dev /opt/solr/server/configsets/hyraxconf"
volumes:
- solr:/var/solr/data:cached
- ./hyrax/solr/conf:/opt/solr/server/configsets/hyraxconf
networks:
- hyrax
db:
container_name: db
image: postgres:14-alpine
ports:
- "5432"
env_file:
- './env/.env.dev.db'
volumes:
- postgres:/var/lib/postgresql/data
restart: unless-stopped
healthcheck:
test: pg_isready -U postgres -h postgres
interval: 30s
timeout: 3s
retries: 3
networks:
- hyrax
volumes:
hyrax_tmp:
bundle_data:
postgres:
fcrepo:
redis:
solr:
networks:
hyrax:
driver: bridge
driver_opts:
com.docker.network.bridge.name: br-hyrax
Dockerfile
ARG RUBY_VERSION=2.7.6
FROM ruby:$RUBY_VERSION
ENV LANG C.UTF-8
ENV NODE_VERSION 12
ENV NODE_ENV production
ENV INSTALL_PATH /home/hyrax
RUN curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -qq
RUN apt-get install -y --no-install-recommends nodejs postgresql-client yarn build-essential vim
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY ./hyrax/Gemfile ./hyrax/Gemfile.lock ./
RUN gem install bundler
RUN bundle install
ADD ./hyrax $INSTALL_PATH
RUN rm -rf tmp
RUN useradd -Ms /bin/bash api -u 1001
RUN chown -R api:api /home/hyrax /usr/local/bundle
USER root
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.6'
# Rails Dependencies
# =====================================================
gem 'rails', '~> 5.2.6'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'rubyzip'
# Application Specific
# =================================================================
# background jobs
# =====================================================
gem 'sidekiq'
gem "sidekiq-cron"
gem 'sidekiq-failures'
# Health Check
# =====================================================
gem 'okcomputer'
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# Hydra Dependencies
# =====================================================
gem 'hydra-role-management'
gem 'hydra-editor'
# Hyrax Dependencies
# =====================================================
gem 'hyrax'
gem 'rsolr', '>= 1.0', '< 3'
gem 'bootstrap-sass', '~> 3.0'
gem 'twitter-typeahead-rails', '0.11.1.pre.corejavascript'
gem 'jquery-rails'
gem 'riiif', '~> 2.1'
# Bulk Import
# =====================================================
gem 'bulkrax'
# authentication
# =====================================================
gem 'devise'
gem 'devise-guests', '~> 0.6'
group :development, :test do
gem 'solr_wrapper', '>= 0.3'
gem 'fcrepo_wrapper'
gem 'rspec-rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
gem 'simplecov', group: :test
gem "simplecov-json" # For CodeClimate
gem 'simplecov-console'
gem 'webdrivers', '~> 3.0'
end
Thanks for any suggestions you may have

Related

Bundle install is failing on docker when building a ruby on rails application

I'm trying to create an environment for developing some Ruby on Rails applications using Docker.
I'm following the official guide for a ruby on rails application on the docker website
The following are my Gemfile, Dockerfile and docker-compose.yml.
source 'https://rubygems.org'
gem 'rails', '~>6'
# syntax=docker/dockerfile:1
FROM ruby:latest
RUN apt-get update -qq && apt-get install -y nodejs
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
version: "3.9"
services:
db:
image: mysql:8
restart: always
ports:
- 3306:3306
volumes:
- dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
volumes:
dbdata:
The only changes I have made to the guide is to use Rails 6, the latest version of ruby and to use MySQL instead of postgres. When I try to run 'docker-compose build' I get the following error:
ERROR [7/9] RUN bundle install
#17 0.505 /usr/local/lib/ruby/3.0.0/rubygems.rb:281:in `find_spec_for_exe': Could not find 'bundler' (1.17.3) required by your /app/Gemfile.lock. (Gem::GemNotFoundException)
#17 0.505 To update to the latest version installed on your system, run `bundle update --bundler`.
The error message is clear, I don't have the correct version of bundler installed. At the bottom of my Gemfile.lock file I have the following:
RUBY VERSION
ruby 2.5.9p229
BUNDLED WITH
1.17.3
I had assumed that my ruby version would have been the latest version and that my Gemfile would have been bundled with > 2.0. I tried adding 'RUN gem bundle install' in my Dockerfile but that did not fix the issue. Is there a correct way I can specify docker to use the latest version of ruby, rails and bundler that are compatible with each other.
You can add this line before RUN bundle install in your Dockerfile:
ENV BUNDLE_VERSION 1.17.3
RUN gem install bundler --version "$BUNDLE_VERSION"

Using specific version of Ruby on rails in Docker Container

I am trying to build the container for the ruby on rails application. I followed the official guide provided at official docker guide
The problem is while creating the Gemfile I provide gem 'rails', '~> 6.0' but when building the docker image it overrides the rails version and installs the latest version.
I have few libraries which are not compatible with the latest version. How can I stop docker in using the latest version while building the docker container?
Dockerfile:
FROM ruby:3
RUN apt-get update -qq && apt-get install -y nodejs
WORKDIR /backend
COPY Gemfile /backend/Gemfile
COPY Gemfile.lock /backend/Gemfile.lock
RUN bundle install
COPY . /backend
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: "3.9"
services:
mongodb:
image: "mongo"
volumes:
- "mongodb:/var/lib/mongodb/data"
environment:
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "root"
ports:
- 2717:27017
web:
build: .
volumes:
- .:/backend
ports:
- "3000:3000"
depends_on:
- mongodb
volumes:
mongodb:
~> 6.0 is a shortcut for ">= 6.0 and < 7". You need to be more specific if you don't want Rails 6.1: ~> 6.0.0. This is equivalent to ">= 6.0.0 and < 6.1".

Could not locate Gemfile or .bundle/ directory when starting rails container on Windows 10 host

I am having trouble with a rails container in a docker-compose network. I have not touched it in a few months, and when I attempted to start it this week it fails.
When I attempt to start the container with docker-compose up service the startup fails with:
service_1 | Could not locate Gemfile or .bundle/ directory
support_portal_service_1 exited with code 10
Both files are present, the host is a Windows 10 machine.
Bundle install completes succesfully:
Bundle complete! 19 Gemfile dependencies, 83 gems now installed.
Bundled gems are installed into `/usr/local/bundle`
What I have tried:
Added ruby and ruby-all-dev to apt-get install in case missing requirements were the issue
Changed ADD Gemfile /app/Gemfile to COPY Gemfile /app/Gemfile
Tried commenting out Gemfile.lock /app/Gemfile.lock
Ran bundle install on the Windows 10 host
Rebuild the container without cache
Insuring docker has access to the drive/directory
Here is my Dockerfile:
FROM ruby:2.5.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs default-libmysqlclient-dev ruby ruby-all-dev
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
#COPY Gemfile.lock /app/Gemfile.lock
WORKDIR /app
RUN bundle install
ADD . /app
And my docker-compose.yml:
version: '2'
services:
# Structured database
sqldb:
image: mysql:5.7
volumes:
- sql:/var/lib/mysql
env_file:
- .env
environment:
- MYSQL_USER=web
- MYSQL_ROOT_PASSWORD=${PORTAL_DATABASE_PASSWORD}
- MYSQL_PASSWORD=${PORTAL_DATABASE_PASSWORD}
ports:
- "3306:3306"
# Application server
service:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app
env_file:
- .env
expose:
- "3000"
depends_on:
- sqldb
# Front end proxy
web:
image: nginx
build:
context: .
dockerfile: Dockerfile-web
depends_on:
- service
ports:
- "80:80"
- "144:144"
# Persistence
volumes:
sql:

GitLab CI ..gitlab-ci.yml property to use fors static page

I am using GitLab to host my static page!
every time I am configuring the .gitlab-ci.yml file I am getting the following error: "Could not locate Gemfile"
Here is the output of the cmd
Here id the .gitlab-ci.yml file
image: ruby:2.3
before_script:
- bundle install
job:
script:
- gem install jekyll
- jekyll build
pages:
stage: deploy
script:
- bundle install
- bundle exec jekyll build -d public
artifacts:
paths:
- public
only:
- master
test:
stage: test
script:
- bundle install
- bundle exec jekyll build -d test
artifacts:
paths:
- test
except:
- master
In your configuration I see a mixed of different install instructions:
bundle install (in the before_script)
bundle install (again, in the pages deploy script)
gem install jekyll
The Gemfile is required by the bundle command, and it should specify mainly the dependency on jekyll (so again a duplication)
PROPOSED SOLUTION:
I suggest you to try with the configuration in the gitlab pages sample:
gitlab-ci.yml
image: ruby:2.3
variables:
JEKYLL_ENV: production
before_script:
- bundle install
test:
stage: test
script:
- bundle exec jekyll build -d test
artifacts:
paths:
- test
except:
- master
pages:
stage: deploy
script:
- bundle exec jekyll build -d public
artifacts:
paths:
- public
only:
- master
Gemfile
source "https://rubygems.org"
ruby RUBY_VERSION
# This will help ensure the proper Jekyll version is running.
gem "jekyll", "3.4.0"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gitlab CI setup error - Could not find a JavaScript runtime

I am trying to setup Gitlab CI for my Ruby on Rails project, but ran into an error that I don't know how to fix.
The application is running on Ruby 2.3.1, Rails 5.0 and is using a postgreSQL database.
My current .gitlab.ci.yml file looks like this:
# https://hub.docker.com/r/library/ruby/tags/
image: "ruby:2.3.0"
# Pick zero or more services to be used on all builds.
# Only needed when using a docker container to run your tests in.
# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service
services:
- redis:latest
- postgres:latest
- node:latest
# This is a basic example for a gem or script which doesn't use
# services such as redis or postgres
before_script:
- gem install bundler # Bundler is not installed with the image
- bundle install -j $(nproc) # Install dependencies
rubocop:
script:
- rubocop
rspec:
script:
- rspec spec
rails:
script:
- rails db:migrate
- rspec spec
So it's supposed to be using NodeJS as runtime. However, I get the following error:
$ rails db:migrate
rails aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'uglifier'.
Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
Backtrace for gem load error is:
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
/usr/local/bundle/gems/execjs-2.7.0/lib/execjs.rb:4:in `<top (required)>'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `require'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `block in require'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:259:in `load_dependency'
/usr/local/bundle/gems/activesupport-5.0.0.rc2/lib/active_support/dependencies.rb:293:in `require'
/usr/local/bundle/gems/uglifier-3.0.0/lib/uglifier.rb:5:in `<top (required)>'
You need to have a javascript runtime (e.g. nodejs). You can install it by using the following command:
For Ubuntu Users
sudo apt-get install nodejs
For CentOS / RedHat Users
sudo yum install nodejs
In case you can't install nodejs you can install and then use therubyracer gem.
Description from repository:
Embed the V8 JavaScript interpreter into Ruby.
Add this line to your Gemfile
gem 'therubyracer', platforms: :ruby
Gitlab-CI can be configured with the file .gitlab-ci.yml in this case:
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
can be used to install nodejs on the setup stage (source).
A complete before_script with jekyll can look like the following code (for instance, this is required for jekyll-minifier):
image: ruby:latest
variables:
JEKYLL_ENV: production
LC_ALL: C.UTF-8
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- ruby -v
- which ruby
- gem install bundler
- bundle install
test:
stage: test
script:
- bundle exec jekyll build -d test
artifacts:
paths:
- test
except:
- master
pages:
stage: deploy
script:
- bundle exec jekyll build -d public
artifacts:
paths:
- public
only:
- master

Resources