نشر Discourse بدون Docker

@lion ، جرب هذا:

# frozen_string_literal: true

require "fileutils"

discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../")

enable_logstash_logger = ENV["ENABLE_LOGSTASH_LOGGER"] == "1"
puma_stderr_path = "#{discourse_path}/log/puma.stderr.log"
puma_stdout_path = "#{discourse_path}/log/puma.stdout.log"

# تحميل مسجل logstash إذا تم تمكينه
if enable_logstash_logger
  require_relative "../lib/discourse_logstash_logger"
  FileUtils.touch(puma_stderr_path) if !File.exist?(puma_stderr_path)
  # ملاحظة: قد تحتاج إلى تكييف تهيئة المسجل لـ Puma
  log_formatter = proc do |severity, time, progname, msg|
    event = {
      "@timestamp" => Time.now.utc,
      "message" => msg,
      "severity" => severity,
      "type" => "puma"
    }
    "#{event.to_json}\n"
  end
else
  stdout_redirect puma_stdout_path, puma_stderr_path, true
end

# عدد العمال (العمليات)
workers ENV.fetch("PUMA_WORKERS", 8).to_i

# تحديد الدليل
directory discourse_path

# الربط بالعنوان والمنفذ المحددين
bind ENV.fetch("PUMA_BIND", "tcp://#{ENV['PUMA_BIND_ALL'] ? '' : '127.0.0.1:'}3000")

# موقع ملف PID
FileUtils.mkdir_p("#{discourse_path}/tmp/pids")
pidfile ENV.fetch("PUMA_PID_PATH", "#{discourse_path}/tmp/pids/puma.pid")

# ملف الحالة - يستخدم بواسطة pumactl
state_path "#{discourse_path}/tmp/pids/puma.state"

# التكوين الخاص بالبيئة
if ENV["RAILS_ENV"] == "production"
  # مهلة الإنتاج
  worker_timeout 30
else
  # مهلة التطوير
  worker_timeout ENV.fetch("PUMA_TIMEOUT", 60).to_i
end

# تحميل التطبيق مسبقًا
preload_app!

# التعامل مع بدء وإيقاف العامل
before_fork do
  Discourse.preload_rails!
  Discourse.before_fork

  # فحص المشرف
  supervisor_pid = ENV["PUMA_SUPERVISOR_PID"].to_i
  if supervisor_pid > 0
    Thread.new do
      loop do
        unless File.exist?("/proc/#{supervisor_pid}")
          puts "قتل المشرف الذاتي قد اختفى"
          Process.kill "TERM", Process.pid
        end
        sleep 2
      end
    end
  end

  # عمال Sidekiq
  sidekiqs = ENV["PUMA_SIDEKIQS"].to_i
  if sidekiqs > 0
    puts "بدء #{sidekiqs} من عمال Sidekiq الخاضعين للإشراف"

    require "demon/sidekiq"
    Demon::Sidekiq.after_fork { DiscourseEvent.trigger(:sidekiq_fork_started) }
    Demon::Sidekiq.start(sidekiqs)

    if Discourse.enable_sidekiq_logging?
      Signal.trap("USR1") do
        # تأخير إعادة فتح سجلات Sidekiq
        sleep 1
        Demon::Sidekiq.kill("USR2")
      end
    end
  end

  # شيطان مزامنة البريد الإلكتروني
  if ENV["DISCOURSE_ENABLE_EMAIL_SYNC_DEMON"] == "true"
    puts "بدء شيطان مزامنة البريد الإلكتروني"
    Demon::EmailSync.start(1)
  end

  # شياطين الإضافات
  DiscoursePluginRegistry.demon_processes.each do |demon_class|
    puts "بدء شيطان #{demon_class.prefix}"
    demon_class.start(1)
  end

  # خيط مراقبة الشياطين
  Thread.new do
    loop do
      begin
        sleep 60

        if sidekiqs > 0
          Demon::Sidekiq.ensure_running
          Demon::Sidekiq.heartbeat_check
          Demon::Sidekiq.rss_memory_check
        end

        if ENV["DISCOURSE_ENABLE_EMAIL_SYNC_DEMON"] == "true"
          Demon::EmailSync.ensure_running
          Demon::EmailSync.check_email_sync_heartbeat
        end

        DiscoursePluginRegistry.demon_processes.each(&:ensure_running)
      rescue => e
        Rails.logger.warn("خطأ في فحص نبضات الشياطين: #{e}\n#{e.backtrace.join("\n")}")
      end
    end
  end

  # إغلاق اتصال Redis
  Discourse.redis.close
end

on_worker_boot do
  DiscourseEvent.trigger(:web_fork_started)
  Discourse.after_fork
end

# التعامل مع مهلة العامل
worker_timeout 30

# خيارات العامل منخفضة المستوى
threads 8, 32