diff --git a/README.md b/README.md index 8ffeab6..ded9406 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,6 @@ To run locally with Docker, type docker-compose up -d ``` -Then seed data with - -```bash -docker-compose run web rake db:migrate rates:reload -``` - Now you can access the API at ``` diff --git a/app/Dockerfile b/app/Dockerfile index 31c9450..cafa3a6 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -4,6 +4,6 @@ RUN mkdir /app WORKDIR /app ADD Gemfile /app/Gemfile ADD Gemfile.lock /app/Gemfile.lock -RUN bundle install --without development test +RUN bundle install --jobs=8 --without development ADD . /app CMD ["unicorn", "-c", "./config/unicorn.rb"] diff --git a/app/bin/schedule b/app/bin/schedule new file mode 100755 index 0000000..394945a --- /dev/null +++ b/app/bin/schedule @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +system 'rake db:setup' + +require 'rufus-scheduler' + +scheduler = Rufus::Scheduler.new +scheduler.cron '*/15 15,16,17 * * 1-5', timeout: '1m' do + system 'rake rates:update' +end +scheduler.join diff --git a/app/wait-for-it.sh b/app/bin/wait-for-it.sh similarity index 100% rename from app/wait-for-it.sh rename to app/bin/wait-for-it.sh diff --git a/app/config/unicorn.rb b/app/config/unicorn.rb index 4010477..bb3101f 100644 --- a/app/config/unicorn.rb +++ b/app/config/unicorn.rb @@ -9,8 +9,3 @@ timeout 10 before_fork do |_, _| Sequel::DATABASES.each(&:disconnect) end - -fork do - require_relative 'environment' - require 'schedule' -end diff --git a/app/lib/schedule.rb b/app/lib/schedule.rb deleted file mode 100644 index 6745176..0000000 --- a/app/lib/schedule.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -require 'bank' -require 'rufus-scheduler' - -schedule = Rufus::Scheduler.new - -schedule.cron '*/15 15,16,17 * * 1-5' do - Bank.fetch_current_rates! -end - -schedule.join diff --git a/app/lib/tasks/db.rake b/app/lib/tasks/db.rake index 20be55c..4484e12 100644 --- a/app/lib/tasks/db.rake +++ b/app/lib/tasks/db.rake @@ -1,11 +1,6 @@ # frozen_string_literal: true namespace :db do - desc 'Create db' - task :create do - `createdb fixer` - end - desc 'Run database migrations' task migrate: :environment do Sequel.extension(:migration) @@ -16,4 +11,6 @@ namespace :db do Sequel::IntegerMigrator.new(db, dir, opts).run end + + task setup: %w[db:migrate rates:reload] end diff --git a/app/lib/tasks/test.rake b/app/lib/tasks/test.rake index 0a1c5ba..283bf19 100644 --- a/app/lib/tasks/test.rake +++ b/app/lib/tasks/test.rake @@ -1,6 +1,6 @@ # frozen_string_literal: true -return if ENV['RACK_ENV'] == 'production' +return unless defined?(Rubocop) require 'rake/testtask' require 'rubocop/rake_task' @@ -13,4 +13,4 @@ end RuboCop::RakeTask.new -task default: %w[db:migrate rates:reload test rubocop] +task default: %w[db:setup test rubocop] diff --git a/docker-compose.override.yml b/docker-compose.override.yml index c3e164a..d114461 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -6,6 +6,10 @@ services: environment: RACK_ENV: development VIRTUAL_HOST: localhost - nginx-proxy: ports: - - '8080:80' + - "8080:8080" + scheduler: + build: + context: ./app + environment: + RACK_ENV: development diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 755af93..8a7136a 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -10,7 +10,16 @@ services: max-size: "50m" max-file: "10" restart: unless-stopped + scheduler: + env_file: .env + image: hakanensari/fixer + logging: + options: + max-size: "50m" + max-file: "10" + restart: unless-stopped nginx-proxy: + image: jwilder/nginx-proxy labels: - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true" logging: @@ -23,6 +32,7 @@ services: restart: unless-stopped volumes: - /etc/nginx/conf.d + - /var/run/docker.sock:/tmp/docker.sock:ro - ./limit_req.conf:/etc/nginx/conf.d/limit_req.conf:ro - certs:/etc/nginx/certs:ro - html:/usr/share/nginx/html diff --git a/docker-compose.yml b/docker-compose.yml index 09993f7..1d994b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,16 +6,19 @@ services: - data:/var/lib/postgresql/data web: command: unicorn -c config/unicorn.rb + entrypoint: bin/wait-for-it.sh db:5432 -s -- environment: DATABASE_URL: postgres://postgres@db/postgres - entrypoint: ./wait-for-it.sh db:5432 -s -- expose: - '8080' links: - db - nginx-proxy: - image: jwilder/nginx-proxy - volumes: - - /var/run/docker.sock:/tmp/docker.sock:ro + scheduler: + command: bin/schedule + entrypoint: bin/wait-for-it.sh db:5432 -s -- + environment: + DATABASE_URL: postgres://postgres@db/postgres + links: + - db volumes: data: