Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Add Dockerized scheduled tasks
Browse files Browse the repository at this point in the history
Added container for scheduled tasks
  • Loading branch information
dokurnon authored and Neil Dokurno committed Jul 14, 2020
1 parent 0a8bb7a commit cda2672
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
15 changes: 15 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ build:rails:
- docker push "$CI_REGISTRY_IMAGE:$IMAGE_TAG"
only:
- tags

build:cron:
image: "docker:latest"
variables:
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
stage: build
script:
- export IMAGE_TAG=$(echo -en $CI_BUILD_REF_NAME | tr -c '[:alnum:]_.-' '-')
- docker login -u "gitlab-ci-token" -p "$CI_BUILD_TOKEN" $CI_REGISTRY
- docker build -f Dockerfile.cron --pull -t "${CI_REGISTRY_IMAGE}-cron:$IMAGE_TAG" .
- docker push "${CI_REGISTRY_IMAGE}-cron:$IMAGE_TAG"
only:
- tags
49 changes: 49 additions & 0 deletions Dockerfile.cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM ruby:2.6.5
ARG MASTER_KEY=$MASTER_KEY

# Add NodeJS repo
RUN curl -sL https://deb.nodesource.com/setup_12.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

# Install dependencies and remove unneeded packages/files
RUN apt-get update -qq
RUN apt-get install -y --no-install-recommends cron \
nodejs \
git \
unzip \
xvfb \
libxi6 \
libgconf-2-4 \
apt-transport-https \
yarn
# Install packages
WORKDIR /usr/src/app
COPY Gemfile* /usr/src/app/
COPY Gemfile.lock* /usr/src/app/
RUN gem install bundler --no-document
RUN bundle install --without development test

# Setup app files
COPY . /usr/src/app/
WORKDIR /usr/src/app
COPY config/database.yml.example.production config/database.yml

# Create log file and link to stdout
RUN touch /var/log/cron.log

# Add entrypoint
COPY entrypoint-cron.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint-cron.sh
ENTRYPOINT ["entrypoint-cron.sh"]

# Update and cleanup container
RUN apt-get upgrade -y && apt-get autoclean \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt \
/var/lib/dpkg \
/var/lib/cache \
/var/lib/log

# App startup
CMD ["cron", "-f"]
23 changes: 15 additions & 8 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@

# Learn more: http://github.com/javan/whenever

# stagger jobs by offsetting with current time
time = Time.new # rubocop:disable TimeZone
# Add all set environment variables to Crontab
ENV.each_key do |key|
env key.to_sym, ENV[key]
end

# Log to stdout
set :output, "/var/log/cron.log"

# Overwrite rake job_type to not include --silent flag
job_type :rake, "cd :path && :environment_variable=:environment bundle exec rake :task :output"

# define cron strings
nightly_cron_str = time.min.to_s + ' 5 * * *'
hourly_cron_str = time.min.to_s + ' * * * *'
# Set environment
set :environment, ENV["RAILS_ENV"]

# every night around 5 AM
every nightly_cron_str do
# every night around 5 AM EST
every :day, at: '12:00am' do
rake 'run_daily_tasks'
end

# every hour (except five AM)
every hourly_cron_str do
every :hour do
rake 'run_hourly_tasks'
end
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
container_name: reservations-rails
build:
context: ./
dockerfile: Dockerfile.dev
dockerfile: Dockerfile.cron
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ${APPLICATION_PATH}:/app
Expand Down
11 changes: 11 additions & 0 deletions entrypoint-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e

# Update crontab file using whenever command
bundle exec whenever --update-crontab

# Follow the log file in the background
tail -f /var/log/cron.log &

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

0 comments on commit cda2672

Please sign in to comment.