Skip to content

Commit

Permalink
Dockerize, Postgresize, Redisize, and Sidekiqize Tap
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNaessens committed Jun 8, 2024
1 parent c5845de commit 5273d8e
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 239 deletions.
33 changes: 16 additions & 17 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ on:
jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- run: RAILS_ENV=test bundle exec rake db:create
- run: RAILS_ENV=test bundle exec rake db:schema:load
- run: bundle exec rake
env:
COVERALLS_REPO_TOKEN : ${{ secrets.COVERALLS_REPO_TOKEN }}
- uses: actions/checkout@v4

- name: Build the stack
run: docker-compose up -d db-test

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- run: RAILS_ENV=test bundle exec rake db:create
- run: RAILS_ENV=test bundle exec rake db:schema:load
- run: bundle exec rake

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: Lint with rubocop
env:
RAILS_ENV: "test"
run: |
bundle exec rubocop -c .rubocop.yml
bundle exec rubocop -c .rubocop.yml
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.1
3.3.2
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ruby 3.3.1
ruby 3.3.2
yarn 1.22.19
nodejs 16.15.0
23 changes: 7 additions & 16 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ gem "cancancan", "~> 3.3"
# Default avatar for users
gem "identicon", "0.0.5"

# Run stuff in the background
gem "daemons", "~> 1.4"
gem "delayed_job", "~> 4.1"
gem "delayed_job_active_record"

# Needed for deployment somehow
gem "bcrypt_pbkdf"
gem "ed25519"
Expand All @@ -81,10 +76,11 @@ gem "sentry-rails"
gem "sentry-ruby"
gem "stackprof"

# Production dependencies
group :production do
gem "mysql2", "~> 0.5.3"
end
# Run stuff in background
gem "sidekiq", "~> 7.0"

# Use PostgreSQL as the database for Active Record
gem "pg"

# Test dependencies
group :test do
Expand All @@ -94,20 +90,15 @@ end

# Development dependencies
group :development do
# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"

# Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the database schema.
gem "annotate", "~> 3.1"

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem "spring", "~> 3.0"

# Deployment
gem "capistrano"
gem "capistrano-asdf"
gem "capistrano-passenger"
gem "capistrano-rails"
gem "capistrano", "~> 3.17"
gem "capistrano-docker", github: "TomNaessens/capistrano-docker"

# Linting
gem "rubocop", "~> 1.30"
Expand Down
45 changes: 20 additions & 25 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
GIT
remote: https://github.com/TomNaessens/capistrano-docker.git
revision: 490311def501a5d32a24e52d6952d355a85b6cfe
specs:
capistrano-docker (0.4.0)
capistrano (>= 3.3)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -84,30 +91,15 @@ GEM
i18n
rake (>= 10.0.0)
sshkit (>= 1.9.0)
capistrano-asdf (1.1.1)
capistrano (~> 3.0)
sshkit (~> 1.2)
capistrano-bundler (2.0.1)
capistrano (~> 3.1)
capistrano-passenger (0.2.1)
capistrano (~> 3.0)
capistrano-rails (1.6.2)
capistrano (~> 3.1)
capistrano-bundler (>= 1.1, < 3)
chartkick (4.1.3)
chunky_png (1.4.0)
climate_control (0.2.0)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crack (0.4.5)
rexml
crass (1.0.6)
daemons (1.4.1)
date (3.3.4)
delayed_job (4.1.10)
activesupport (>= 3.0, < 8.0)
delayed_job_active_record (4.1.7)
activerecord (>= 3.0, < 8.0)
delayed_job (>= 3.0, < 5)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -176,7 +168,6 @@ GEM
multi_json (1.15.0)
multi_xml (0.6.0)
mutex_m (0.2.0)
mysql2 (0.5.4)
net-imap (0.4.11)
date
net-protocol
Expand Down Expand Up @@ -221,6 +212,7 @@ GEM
parser (3.3.1.0)
ast (~> 2.4.1)
racc
pg (1.5.6)
psych (5.1.2)
stringio
public_suffix (4.0.7)
Expand Down Expand Up @@ -273,6 +265,8 @@ GEM
ffi (~> 1.0)
rdoc (6.7.0)
psych (>= 4.0.0)
redis-client (0.22.2)
connection_pool
regexp_parser (2.9.2)
responders (3.0.1)
actionpack (>= 5.0)
Expand Down Expand Up @@ -334,6 +328,11 @@ GEM
sentry-ruby (5.17.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
sidekiq (7.2.4)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.19.0)
spring (3.1.1)
sprockets (4.0.3)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -395,14 +394,9 @@ DEPENDENCIES
bulma-rails (~> 0.9.1)
byebug
cancancan (~> 3.3)
capistrano
capistrano-asdf
capistrano-passenger
capistrano-rails
capistrano (~> 3.17)
capistrano-docker!
chartkick (~> 4.0)
daemons (~> 1.4)
delayed_job (~> 4.1)
delayed_job_active_record
devise (~> 4.8)
dotenv-rails
ed25519
Expand All @@ -415,12 +409,12 @@ DEPENDENCIES
identicon (= 0.0.5)
kt-paperclip (~> 7.0)
listen (~> 3.7)
mysql2 (~> 0.5.3)
net-imap
net-pop
net-smtp
omniauth-rails_csrf_protection
omniauth-zeuswpi
pg
puma (~> 5.5.0)
rails (~> 6.1)
rails-controller-testing (~> 1.0)
Expand All @@ -432,6 +426,7 @@ DEPENDENCIES
sdoc (~> 2.2)
sentry-rails
sentry-ruby
sidekiq (~> 7.0)
spring (~> 3.0)
sqlite3 (~> 1.4)
stackprof
Expand Down
3 changes: 3 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: PORT=3000 rails s
sidekiq: bundle exec sidekiq
db: docker-compose up
55 changes: 14 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,17 @@

To provide a consistent experience on every system, docker and docker-compose is used during development and production.

### Using Docker and Make *(recommended)*
1. Install the prerequisites: ruby `$(cat .ruby-version)`, preferably using [asdf](https://asdf-vm.com/), and some system libraries depending on your OS (e.g. imagemagick)
2. Install the ruby dependencies: `bin/bundle`
3. Start up the database, sidekiq and rails server by running `bin/dev`
4. Set up some database data using `rails db:setup`
5. Browse to http://localhost:3000

#### Linux/Unix
In case you want to start the webserver in your IDE, just run `docker-compose up -d` and start Sidekiq manually (`bundle exec sidekiq`)

1. Install [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/)
2. Start the development server
```sh
make dev
```
## Deploying

> This will start a development server on http://localhost:3000
3. Seed the database.
```sh
make dev-seed
```

> The development setup uses an SQLite 3 database, which can be found under `/db/development.sqlite3`

#### Windows

1. Install [WSL (Windows Subsystem for Linux)](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
2. Follow the instructions for Linux/Unix above

### Directly on your system

1. Install [asdf](http://asdf-vm.com/guide/getting-started.html#getting-started)
2. Install dependencies: `asdf install`
3. Install gems: `bundle install`
4. Migrate the db using `bundle exec rails db:migrate`
5. Seed the db using `bundle exec rails db:seed`
6. Start Tap by running `bundle exec rails s`

## Production

You can generate a production docker image using:

```sh
make build
```

> The image will be tagged under `tap:latest`
_Locally_, run `bundle exec cap production deploy`

## FAQ

Expand All @@ -73,9 +43,12 @@ make build

<details>
<summary>There are no transactions going from Tap to Tab</summary>
The delay job may not be running. You can start it using:
Sidekiq might not be running. Check the dashboard on https://tap.zeus.gent/sidekiq.

You can start by redeploying the application, or by turning the deployment on the server off & on (inside the `/home/tap/production/current` directory):

```sh
sudo -u tap RAILS_ENV=production /home/tap/production/current/bin/delayed_job start
```bash
podman-compose -f docker-compose.prod.yml -p tap down
podman-compose -f docker-compose.prod.yml -p tap up -d
```
</details>
2 changes: 1 addition & 1 deletion app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def create
# Create an order
# POST(method: DELETE) /users/{username}/orders/{id}
def destroy
@order.destroy
@order.destroy!
respond_to do |format|
format.json { head :no_content }
format.html do
Expand Down
10 changes: 4 additions & 6 deletions app/jobs/tab_api_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

TabApiJob = Struct.new(:order_id) do
def perform(*_args)
class TabApiJob < ApplicationJob
def perform(order_id)
order = Order.find_by(id: order_id)
return unless order && !order.transaction_id

Expand All @@ -22,13 +22,11 @@ def perform(*_args)
order.update(transaction_id: JSON.parse(result.body)["id"].to_i)
end

private

def headers
{
"Authorization" => "Token token=#{Rails.application.secrets.tab_api_key}"
}
end

def error(_job, exception)
Airbrake.notify(exception)
end
end
6 changes: 1 addition & 5 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ def calculate_price
def create_api_job
return if Rails.env.test?

priority = 0
run_at = Rails.application.config.call_api_after.from_now
job = TabApiJob.new(id)

Delayed::Job.enqueue job, priority: priority, run_at: run_at
TabApiJob.set(wait: Rails.application.config.call_api_after).perform_later(id)
end

def product_presence
Expand Down
5 changes: 0 additions & 5 deletions bin/delayed_job

This file was deleted.

10 changes: 10 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
docker-compose up -d

if ! command -v foreman &> /dev/null
then
echo "Installing foreman…"
gem install foreman
fi

foreman start -f Procfile.dev "$@"
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Application < Rails::Application
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Brussels'

# Setup delayed jobs
config.active_job.queue_adapter = :delayed_job
# Setup Sidekiq
config.active_job.queue_adapter = :sidekiq
config.call_api_after = 5.minutes
config.frecency_num_orders = 25

Expand Down
Loading

0 comments on commit 5273d8e

Please sign in to comment.