Elasticbeanstalk + rails + crono
railsでcron的なものを使いたかったけど、crontabを触りたくなかったので、cronoを使うことにした.その設定ファイル(結構作るのに時間かかったので。。。)
Cronoはこちら
I would like to use something like cron with rails, but didn't want to use crontab, so I try to use crono(I know it may not be maintained, but...), here is the config which I used.
環境
# rails -v
Rails 6.0.2.1
# uname -a
Linux ip-172-31-27-188 4.14.165-103.209.amzn1.x86_64 #1 SMP Sun Feb 9 00:23:26 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
設定ファイル
# cat .ebextensions/05_crono.config
commands:
create_post_dir:
command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_crono.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
EB_APP_LOG_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_log_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
PID=$EB_APP_PID_DIR/crono.pid
cd $EB_APP_DEPLOY_DIR
if [ -f $PID ]
then
su -s /bin/bash -c "kill -TERM `cat $PID`" $EB_APP_USER
su -s /bin/bash -c "rm -rf $PID" $EB_APP_USER
fi
. $EB_SUPPORT_DIR/envvars.d/sysenv
sleep 10
su -s /bin/bash -c "bundle exec crono start -P $PID " $EB_APP_USER
sleep 10
ln -sf $EB_APP_DEPLOY_DIR/log/crono.log $EB_APP_LOG_DIR
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_mute_crono.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
PID=$EB_APP_PID_DIR/crono.pid
if [ -f $PID ]
then
su -s /bin/bash -c "kill -USR1 `cat $PID`" $EB_APP_USER
fi
テストに使ったjob
class CronoTestJob < ApplicationJob
queue_as :default
def logger
Crono.logger.nil? ? Rails.logger : Crono.logger
end
def perform(*args)
logger.info '*** start crono test job ***'
end
end