From 57b0a01104aec1bb9bc507b2182a85103f565bb8 Mon Sep 17 00:00:00 2001
From: Israel Sant'Anna <ibsantanna@inf.ufpr.br>
Date: Wed, 25 Nov 2020 12:01:47 -0300
Subject: [PATCH] Added Sidekiq 6 new service and config files. Updated dalli
 store config for new version

---
 config/environments/production.rb |  5 ++-
 export_env_vars.sh                | 24 ++++++++++
 portalmec-sidekiq.service         | 75 +++++++++++++++++++++++++++++--
 sidekiq_log.conf                  |  4 ++
 4 files changed, 104 insertions(+), 4 deletions(-)
 create mode 100755 export_env_vars.sh
 create mode 100644 sidekiq_log.conf

diff --git a/config/environments/production.rb b/config/environments/production.rb
index b1be05235..68ac04733 100644
--- a/config/environments/production.rb
+++ b/config/environments/production.rb
@@ -95,7 +95,10 @@ Rails.application.configure do
   # Do not dump schema after migrations.
   config.active_record.dump_schema_after_migration = false
 
-  config.cache_store = :dalli_store, nil, { :namespace => 'portalmec', :expires_in => 1.day, :compress => true, :pool_size => 5 }
+  cache_store_servers = nil
+  cache_store_options = { :namespace => 'portalmec', :expires_in => 1.day, :compress => true, :pool_size => 5 }
+  cache_store_args = [cache_store_servers, cache_store_options].compact
+  config.cache_store = :mem_cache_store, *cache_store_args
 
   # Ignore bad email addresses and do not raise email delivery errors.
   # Set this to true and configure the email server for immediate delivery to raise delivery errors.
diff --git a/export_env_vars.sh b/export_env_vars.sh
new file mode 100755
index 000000000..1cb2df65d
--- /dev/null
+++ b/export_env_vars.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
+# Departamento de Informatica - Universidade Federal do Parana
+#
+# This file is part of portalmec.
+#
+# portalmec is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# portalmec is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with portalmec.  If not, see <http://www.gnu.org/licenses/>.
+
+ENV_VARS_PATH=/home/portalmec/portalmec/config/env_vars.sh
+EXPORT_PATH=/home/portalmec/portalmec/env_vars.txt
+
+grep -o '^[^#]*' $ENV_VARS_PATH | cut -d ' ' -f1 --complement > $EXPORT_PATH 
diff --git a/portalmec-sidekiq.service b/portalmec-sidekiq.service
index 7ba676923..cc7f4a872 100644
--- a/portalmec-sidekiq.service
+++ b/portalmec-sidekiq.service
@@ -1,9 +1,78 @@
+#
+# This file tells systemd how to run Sidekiq as a 24/7 long-running daemon.
+#
+# Customize this file based on your bundler location, app directory, etc.
+# Customize and copy this into /usr/lib/systemd/system (CentOS) or /lib/systemd/system or /etc/systemd/system (Debian).
+# Then run:
+#   - systemctl enable sidekiq
+#   - systemctl {start,stop,restart} sidekiq
+#
+# This file corresponds to a single Sidekiq process.  Add multiple copies
+# to run multiple processes (sidekiq-1, sidekiq-2, etc).
+#
+# Use `journalctl -u sidekiq -rn 100` to view the last 100 lines of log output.
+#
 [Unit]
 Description=Control sidekiq for portalmec
+# start us only once the network and logging subsystems are available,
+# consider adding redis-server.service if Redis is local and systemd-managed.
+After=syslog.target network.target
 
+# See these pages for lots of options:
+#
+#   https://www.freedesktop.org/software/systemd/man/systemd.service.html
+#   https://www.freedesktop.org/software/systemd/man/systemd.exec.html
+#
+# THOSE PAGES ARE CRITICAL FOR ANY LINUX DEVOPS WORK; read them multiple
+# times! systemd is a critical tool for all developers to know and understand.
+#
 [Service]
-ExecStart=/portalmec/sidekiq.sh start
-ExecStop=/portalmec/sidekiq.sh stop
+#
+#      !!!!  !!!!  !!!!
+#
+# As of v6.0.6, Sidekiq automatically supports systemd's `Type=notify` and watchdog service
+# monitoring. If you are using an earlier version of Sidekiq, change this to `Type=simple`
+# and remove the `WatchdogSec` line.
+#
+#      !!!!  !!!!  !!!!
+#
+Type=notify
+NotifyAccess=all
+# If your Sidekiq process locks up, systemd's watchdog will restart it within seconds.
+WatchdogSec=10
+
+WorkingDirectory=/home/portalmec/portalmec
+
+# Export the environment variables in a readable format for systemd
+ExecStartPre=/home/portalmec/portalmec/export_env_vars.sh
+
+# If you use rbenv:
+# ExecStart=/bin/bash -lc 'exec /home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
+# If you use the system's ruby:
+# ExecStart=/usr/local/bin/bundle exec sidekiq -e production
+# If you use rvm:
+ExecStart=/home/portalmec/.rvm/gems/default/wrappers/bundle exec sidekiq -e production
+
+# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process
+ExecStop=/bin/kill -s TSTP $MAINPID
+
+# Greatly reduce Ruby memory fragmentation and heap usage
+# https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
+Environment=MALLOC_ARENA_MAX=2
+
+# Load the envinroment variables exported in the ExecStartPre script
+EnvironmentFile=/home/portalmec/portalmec/env_vars.txt
+
+# if we crash, restart
+RestartSec=10
+Restart=on-failure
+
+# output goes to /var/log/syslog (Debian) or /var/log/messages (CentOS)
+StandardOutput=syslog
+StandardError=syslog
+
+# This will default to "bundler" if we don't specify it
+SyslogIdentifier=sidekiq
 
 [Install]
-WantedBy=multi-user.target
+WantedBy=multi-user.target
\ No newline at end of file
diff --git a/sidekiq_log.conf b/sidekiq_log.conf
new file mode 100644
index 000000000..61368d018
--- /dev/null
+++ b/sidekiq_log.conf
@@ -0,0 +1,4 @@
+# Redirects sidekiq's service log from syslog to the sidekiq.log file
+# Copy this file to /etc/rsyslog.d/sidekiq_log.conf
+
+:programname, isequal, "sidekiq" /home/portalmec/portalmec/shared/logs/sidekiq.log
-- 
GitLab