Prepare to support ruby 3.3.

This commit is contained in:
邱博亞 2024-02-24 14:54:42 +08:00
parent 8cf6638840
commit 378fb30015
9 changed files with 912 additions and 1029 deletions

View File

@ -2,6 +2,7 @@ require "yaml"
module Announcement module Announcement
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
initializer "announcement" do initializer "announcement" do
Rails.application.config.to_prepare do
begin begin
translate_data = Dir["#{Announcement::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))} translate_data = Dir["#{Announcement::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))}
data = {} data = {}
@ -73,7 +74,7 @@ module Announcement
data_item[key_item2] = [value2_item1,value2_item2] data_item[key_item2] = [value2_item1,value2_item2]
data_item[key1] = key1_attr data_item[key1] = key1_attr
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
require File.expand_path('../../../app/models/anns_cache', __FILE__) require File.join(Announcement::Engine.root, 'app/models/anns_cache')
if defined?(AnnsCache) if defined?(AnnsCache)
AnnsCache.all.delete AnnsCache.all.delete
end end
@ -158,7 +159,6 @@ module Announcement
:active_for_action=>{'admin/announcements'=>'settings'}, :active_for_action=>{'admin/announcements'=>'settings'},
:available_for => 'managers' :available_for => 'managers'
end end
end end
# temp = YAML.load_file(File.join(Rails.root,"config","mongoid.yml")) # temp = YAML.load_file(File.join(Rails.root,"config","mongoid.yml"))
# dbsettings = temp["production"]["sessions"]["default"] # dbsettings = temp["production"]["sessions"]["default"]
@ -168,3 +168,4 @@ module Announcement
end end
end end
end end
end

View File

@ -41,6 +41,7 @@ else
#store api #store api
gem 'httparty' gem 'httparty'
end end
gem 'proxifier', git: 'http://gitlab.tp.rulingcom.com/core/ruby-proxifier.git'
gem 'net-smtp-proxy' gem 'net-smtp-proxy'
#built in modules #built in modules
eval(File.read(File.dirname(__FILE__) + '/built_in_extensions.rb')) eval(File.read(File.dirname(__FILE__) + '/built_in_extensions.rb'))
@ -136,8 +137,6 @@ if RUBY_VERSION.to_f == 2.1
#form helpers #form helpers
gem 'dynamic_form' gem 'dynamic_form'
gem 'fugit', '1.9.0' #fix sytax error dependency for rufus
#development related gems #development related gems
group :development do group :development do
gem 'better_errors', '>= 2.8.1', '< 2.8.2' gem 'better_errors', '>= 2.8.1', '< 2.8.2'
@ -195,14 +194,19 @@ else
gem 'railties' gem 'railties'
gem 'rss' gem 'rss'
#parser #parser
if RUBY_VERSION.to_f >= 3.0
gem 'nokogiri', '~> 1.16', '>= 1.16.2'
gem 'json'
else
gem 'nokogiri', '~> 1.8.5' gem 'nokogiri', '~> 1.8.5'
gem 'json', '2.6.1'
end
gem 'actionpack-page_caching' gem 'actionpack-page_caching'
#database #database
# gem 'mongoid', github: "mongoid/mongoid" # gem 'mongoid', github: "mongoid/mongoid"
gem 'bson'#, '~> 4.5.0' gem 'bson'#, '~> 4.5.0'
gem 'mongo', '2.15.1' #last version support for mongodb 2.6 gem 'mongo', '2.15.1' #last version support for mongodb 2.6
gem 'json', '2.6.1'
gem 'mongoid' gem 'mongoid'
# sockets # sockets
@ -235,6 +239,8 @@ else
#form helpers #form helpers
gem 'dynamic_form' gem 'dynamic_form'
gem 'fugit', '1.9.0' #fix sytax error dependency for rufus
#development related gems #development related gems
group :development do group :development do
#gem 'better_errors' #gem 'better_errors'

View File

@ -11,6 +11,11 @@ require "mongoid/config"
require "mongoid/railtie" require "mongoid/railtie"
require "bson" require "bson"
require 'rails' require 'rails'
if defined?(Fixnum).nil?
Fixnum = Integer
puts "Use Integer instead!"
puts "Fixnum is deprecated in Ruby trunk!"
end
if Rails.version.to_i>4 if Rails.version.to_i>4
module AbstractController module AbstractController
module Callbacks module Callbacks
@ -61,8 +66,13 @@ if Rails.version.to_i>4
self.to_s self.to_s
end end
end end
elsif Rails.version.to_f >= 4.2
class ActionController::Parameters
def to_h #Migrate from rails 4.1 to rails 4.2, avoid unpermitted parameters
to_unsafe_hash
end
end
end end
if RUBY_VERSION.to_f > 2.1 if RUBY_VERSION.to_f > 2.1
if RUBY_VERSION.to_f>2.4 if RUBY_VERSION.to_f>2.4
module Psych module Psych
@ -83,10 +93,10 @@ if RUBY_VERSION.to_f > 2.1
end end
module URI module URI
def self.escape(uri) def self.escape(uri)
self::DEFAULT_PARSER.escape(uri) self::DEFAULT_PARSER.escape(uri.to_s)
end end
def self.unescape(uri) def self.unescape(uri)
self::DEFAULT_PARSER.unescape(uri) self::DEFAULT_PARSER.unescape(uri.to_s)
end end
def self.parse(uri) def self.parse(uri)
uri = uri.strip uri = uri.strip
@ -188,15 +198,14 @@ end
module Mongoid module Mongoid
module Findable module Findable
def and_any_of(conditions) def and_any_of(conditions)
self.or(conditions) self.any_of(*conditions)
end end
end end
end end
module Mongoid module CustomCriteria
class Criteria
def and_any_of(conditions) def and_any_of(conditions)
if self.selector.count==0 if self.selector.count==0
tmp = self.or(conditions) tmp = self.any_of(*conditions)
else else
all_selector = self.selector all_selector = self.selector
self.selector = {} self.selector = {}
@ -205,15 +214,38 @@ module Mongoid
tmp tmp
end end
end end
module Mongoid
class Criteria
include CustomCriteria
end end
end
module ActionView::Helpers::AssetTagHelper require 'action_dispatch/http/request'
self.included do class ActionDispatch::Request
alias_method :org_remote_ip, :remote_ip
def remote_ip
if @remote_ip.nil?
@remote_ip = org_remote_ip
if @remote_ip == '127.0.0.1'
tmp = env['HTTP_X_FORWARDED_FOR']
@remote_ip = tmp if tmp.present?
end
@remote_ip
else
@remote_ip
end
end
end
module AssetTagHelperFix
def stylesheet_link_tag(*sources) def stylesheet_link_tag(*sources)
options = sources.extract_options!.stringify_keys options = sources.extract_options!.stringify_keys
super(*sources,{media: :all}.merge(options)) super(*sources,{media: :all}.merge(options))
end end
end end
module ActionView::Helpers::AssetTagHelper
def self.included(base)
base.include ::AssetTagHelperFix
end
end end
# Require the gems listed in Gemfile, including any gems # Require the gems listed in Gemfile, including any gems
@ -237,7 +269,7 @@ Bundler.require(:default, Rails.env)
class CssPostProcessor class CssPostProcessor
def call(input) def call(input)
d = input[:data] d = input[:data]
[[':hover','.hover-class'],[':hover',':focus-within'],['\.ad-overlay','.banner-overlay']].each do |s,t| [[':hover','.hover-class'],['\.ad-overlay','.banner-overlay']].each do |s,t|
d = d.gsub(/((?:(?!{|}).)*#{s}(?:(?!{|}).)*){/) do |v| d = d.gsub(/((?:(?!{|}).)*#{s}(?:(?!{|}).)*){/) do |v|
v1 = $1 v1 = $1
v1 = v1.gsub(/((?:(?!,|\t).)*#{s}(?:(?!,|\t).)*)/) do |c| v1 = v1.gsub(/((?:(?!,|\t).)*#{s}(?:(?!,|\t).)*)/) do |c|
@ -252,13 +284,17 @@ class CssPostProcessor
end end
module Orbit module Orbit
class Application < Rails::Application
if Rails.version.to_i<=5
class ExceptionController < ActionDispatch::PublicExceptions class ExceptionController < ActionDispatch::PublicExceptions
require 'action_dispatch/http/request' require 'action_dispatch/http/request'
require 'action_dispatch/middleware/exception_wrapper' require 'action_dispatch/middleware/exception_wrapper'
require 'action_dispatch/routing/inspector' require 'action_dispatch/routing/inspector'
require 'action_dispatch/middleware/debug_exceptions' require 'action_dispatch/middleware/debug_exceptions'
RESCUES_TEMPLATE_PATH = ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH RESCUES_TEMPLATE_PATH = ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH
Is_Rails5 = (Rails.version.to_i>4) Rails_Ver = Rails.version.to_f
Is_Rails4_2 = (Rails_Ver >= 4.2)
Is_Rails5 = (Rails_Ver >= 5)
alias_method :org_call, :call alias_method :org_call, :call
def call(env) def call(env)
request = ActionDispatch::Request.new(env) request = ActionDispatch::Request.new(env)
@ -270,9 +306,13 @@ module Orbit
if (Site::DEBUG rescue false) || (Rails.env != "production" && request_path.start_with?('/admin') && (@current_user = (session[:user_id] ? User.find(session[:user_id]) : nil) rescue nil)) if (Site::DEBUG rescue false) || (Rails.env != "production" && request_path.start_with?('/admin') && (@current_user = (session[:user_id] ? User.find(session[:user_id]) : nil) rescue nil))
exception = env['action_dispatch.exception'] exception = env['action_dispatch.exception']
@routes_app = env["action_dispatch.routes"] @routes_app = env["action_dispatch.routes"]
if Is_Rails4_2
if Is_Rails5 if Is_Rails5
backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner") backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
wrapper = ActionDispatch::ExceptionWrapper.new(backtrace_cleaner, exception) wrapper = ActionDispatch::ExceptionWrapper.new(backtrace_cleaner, exception)
else
wrapper = ActionDispatch::ExceptionWrapper.new(env, exception)
end
traces = wrapper.traces traces = wrapper.traces
trace_to_show = "Application Trace" trace_to_show = "Application Trace"
if traces[trace_to_show].empty? && wrapper.rescue_template != "routing_error" if traces[trace_to_show].empty? && wrapper.rescue_template != "routing_error"
@ -282,7 +322,7 @@ module Orbit
if source_to_show = traces[trace_to_show].first if source_to_show = traces[trace_to_show].first
source_to_show_id = source_to_show[:id] source_to_show_id = source_to_show[:id]
end end
template = ActionDispatch::DebugExceptions::DebugView.new([RESCUES_TEMPLATE_PATH], template_variables = {
request: request, request: request,
exception: wrapper.exception, exception: wrapper.exception,
traces: traces, traces: traces,
@ -292,10 +332,11 @@ module Orbit
source_extracts: wrapper.source_extracts, source_extracts: wrapper.source_extracts,
line_number: wrapper.line_number, line_number: wrapper.line_number,
file: wrapper.file file: wrapper.file
) }
else else
wrapper = ActionDispatch::ExceptionWrapper.new(env, exception) wrapper = ActionDispatch::ExceptionWrapper.new(env, exception)
template = ActionView::Base.new([RESCUES_TEMPLATE_PATH], template_variables = {
request: request, request: request,
exception: wrapper.exception, exception: wrapper.exception,
application_trace: wrapper.application_trace, application_trace: wrapper.application_trace,
@ -305,7 +346,12 @@ module Orbit
source_extract: wrapper.source_extract, source_extract: wrapper.source_extract,
line_number: wrapper.line_number, line_number: wrapper.line_number,
file: wrapper.file file: wrapper.file
) }
end
if Is_Rails5
template = ActionDispatch::DebugExceptions::DebugView.new([RESCUES_TEMPLATE_PATH], template_variables)
else
template = ActionView::Base.new([RESCUES_TEMPLATE_PATH], template_variables)
end end
file = "rescues/#{wrapper.rescue_template}" file = "rescues/#{wrapper.rescue_template}"
@ -341,7 +387,10 @@ module Orbit
end end
end end
end end
class Application < Rails::Application config.exceptions_app = ExceptionController.new("#{Rails.root}/app/views/errors")
else
config.action_dispatch.show_detailed_exceptions = false
end
# Settings in config/environments/* take precedence over those specified here. # Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.
@ -385,10 +434,16 @@ module Orbit
config.mongoid.belongs_to_required_by_default = false config.mongoid.belongs_to_required_by_default = false
end end
# config.mongoid.observers = :orbit_observer # config.mongoid.observers = :orbit_observer
require 'fileutils'
asset_cache_path = 'public/assets'
FileUtils.rm_r asset_cache_path, :force => true
FileUtils.mkdir_p asset_cache_path
config.assets.configure do |env| config.assets.configure do |env|
env.cache = ActiveSupport::Cache.lookup_store(:memory_store,{ size: 128.megabytes }) env.js_compressor = :uglifier # or :closure, :yui
env.css_compressor = :sass # or :yui
env.cache = ActiveSupport::Cache.lookup_store(:file_store, asset_cache_path)
env.register_postprocessor 'text/css', CssPostProcessor.new env.register_postprocessor 'text/css', CssPostProcessor.new
end end
config.exceptions_app = ExceptionController.new("#{Rails.root}/app/views/errors")
end end
end end

View File

@ -2,4 +2,4 @@
puts 'boot' puts 'boot'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

View File

@ -4,13 +4,12 @@ begin
rescue Exception => e rescue Exception => e
end end
include BundlerHelper include BundlerHelper
current_locale = I18n.locale
extra_langs = File.read('config/extra_lang.txt').scan(/.*extra_.*/).select{|v| v.exclude? '#'}[0].to_s.split(/extra_language.*:|,| /).select{|v| !v.empty?}.map{|v| v.to_sym} rescue []
site = Site.first site = Site.first
if (site.password_failed_lock_num.nil? rescue false) if (site.password_failed_lock_num.nil? rescue false)
site.update({:password_failed_lock_num=> site.fields['password_failed_lock_num'].options[:default]}) site.update({:password_failed_lock_num=> site.fields['password_failed_lock_num'].options[:default]})
end end
site.update(:valid_locales=>I18n.available_locales)
site.reset_cache site.reset_cache
if ModuleApp.where(:key=>'member_sign_up').count > 1 if ModuleApp.where(:key=>'member_sign_up').count > 1
ModuleApp.where(:key=>'member_sign_up')[0...1].each{|m| m.destroy} ModuleApp.where(:key=>'member_sign_up')[0...1].each{|m| m.destroy}
@ -21,41 +20,16 @@ if (site.page_sets.count==0 rescue false)
elsif site.page_sets.count > 1 elsif site.page_sets.count > 1
site.page_sets.skip(1).destroy site.page_sets.skip(1).destroy
end end
$member_extra_url = site.member_extra_url begin
if File.basename($0) != 'rake' require File.expand_path('../do_on_startup_helper', __FILE__)
crontab_email=%x[crontab -l|grep 'bundle exec rake email:deliver_all'|grep `pwd`] load_error = false
crontab_email_all = crontab_email.split("\n") rescue LoadError
if crontab_email_all.count >= 1 load_error = true
crontab_jobs=%x[crontab -l|grep -v "cd `pwd`.*bundle exec rake email:deliver_all"].gsub(/^\n$/,'').gsub(/'/,"'\"'\"'")
system("echo '#{crontab_jobs}' | crontab -")
end
CronWorker.cron_every_minute(Email.method(:deliver_all))
nginx_exe = %x[ps -o args -C nginx| grep -E 'daemon|master'| awk '{print $4}'].split("\n")[0]
if !nginx_exe.nil?
nginx_config = %x[#{nginx_exe} -V 2>&1 | grep -o '\\-\\-conf-path=\\(.*conf\\)' | cut -d '=' -f2].sub("\n",'')
orbit_sites_config = %x[grep orbit_sites #{nginx_config}].scan(/^[\t ]*include(.*);/)[0][0].gsub(/ /,'')
current_site_config = %x[grep -H `pwd` #{orbit_sites_config}|grep -E ':\s\+root'].split(':')[0]
current_site_config_content = current_site_config.present? ? %x[cat #{current_site_config}] : ""
config_domain = site.site_settings['domain'] rescue nil
server_names = current_site_config_content.scan(/^[\t ]*server_name[\t ]*(.*);/).flatten.collect{|v| v.to_s.split(/[\t ]+/)}.flatten.sort_by{|v| v==config_domain ? 0 : v.split(".").count}.reverse
ip = UDPSocket.open {|s| s.connect("8.8.8.8", 1); s.addr.last} rescue nil
new_server_names = server_names.uniq.select{|server_name| ip == (IPSocket.getaddress(server_name) rescue nil)}
server_name = new_server_names.select{|server_name| ip != server_name}.first
if server_name.nil?
if new_server_names.count != 0
server_name = new_server_names.first
else
server_name = server_names.first
if server_name.nil?
server_name = ip || (`ip route get 8.8.8.8|xargs|awk '{print $7}'`.strip)
end
end
end
ports = current_site_config_content.scan(/^[\t ]*listen[\t ]*(.*);/).flatten.collect{|v| v.split(/[\t ]+/)}.flatten.select{|v| v.to_i.to_s == v}
port = ports.blank? ? '80' : (ports.select{|v| v=='443'}.blank? ? ports.sort_by{|v| v.to_i==80 ? -1 : v.to_i}[0] : '443')
root_url = (port=='443' ? "https://#{server_name}" : (port=='80' ? "http://#{server_name}" : "http://#{server_name}:#{port}"))
site.update_attributes(root_url: root_url)
end end
if File.basename($0) != 'rake' && !load_error
fix_email_job
fix_site_root_url(site)
if ShowForAdminMessage.first.nil? if ShowForAdminMessage.first.nil?
ShowForAdminMessage.create() ShowForAdminMessage.create()
end end
@ -64,409 +38,16 @@ if File.basename($0) != 'rake'
site.template = (Dir.glob("#{Rails.root}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first site.template = (Dir.glob("#{Rails.root}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first
site.save site.save
end end
require File.expand_path('../initial_site_data', __FILE__)
if Page.count == 0
home = Page.new
extra_trans = {}
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = 'Home'
end
home.name_translations = {:en=>"Home",:zh_tw=>"首頁"}.merge(extra_trans)
home.url = "/"
home.save
end
if Page.where(:module=>"sitemap").blank?
sitemap = Page.new
sitemap.module = "sitemap"
sitemap.page_id = "sitemap"
extra_trans = {}
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = 'Sitemap'
end
sitemap.name_translations = {:en=>"Sitemap",:zh_tw=>"網站導覽"}.merge(extra_trans)
sitemap.enabled_for = ["en", "zh_tw"]
sitemap.menu_enabled_for = []
sitemap.url = "/sitemap"
sitemap.parent_page_id = Page.root.id
sitemap.save
end
termofuse = Page.where(:page_id=>'termofuse').first
page = termofuse.page_contexts.order(:version=>-1).first rescue nil
default_content_translations = {}
ac = ActionController::Base.new()
site.in_use_locales.each do |locale|
I18n.with_locale(locale) do
content = ac.render_to_string(:partial=>"pages/default_term_of_use",locals: {:current_site=>site})
default_content_translations[locale] = content
end
end
if page.nil?
page = PageContext.new
extra_trans = {}
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = ''
end
page.content_translations = default_content_translations
page.version = 1
page.save
else
change_flag = false
new_content_translations = {}
site.in_use_locales.each do |locale|
if page.content_translations[locale].blank?
change_flag = true
new_content_translations[locale] = default_content_translations[locale]
end
end
if change_flag
page.content_translations = page.content_translations.merge(new_content_translations)
page.save
end
end
if termofuse.nil?
termofuse = Page.new
termofuse.module = "page_content"
termofuse.page_id = "termofuse"
termofuse.page_contexts << page
extra_trans = {}
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = 'Term of use'
end
termofuse.name_translations = {:en=>"Term of use",:zh_tw=>"使用規則"}.merge(extra_trans)
termofuse.enabled_for = ["en", "zh_tw"]
termofuse.menu_enabled_for = []
termofuse.url = "/termofuse"
termofuse.parent_page_id = Page.root.id
termofuse.save
site.terms_of_use_link = "/termofuse"
site.save
end
if Workgroup.count == 0
groups = [{"title"=>"Administrator", "key"=>"admin"},
{"title"=>"Sub Managers", "key"=>"sub_managers"},
{"title"=>"Managers", "key"=>"managers"}]
groups.each do |group|
workgroup = Workgroup.new
workgroup.title = group["title"]
workgroup.key = group["key"]
workgroup.save
end
end
if Role.count==0
#teacher
role = Role.new
role.key = 'teacher'
role.title_translations = {:en=>'Teacher', :zh_tw=>'老師'}
role.save
role_status = RoleStatus.new
role_status.key = 'Full-Time'
role_status.role_id = role.id
role_status.title_translations = {:en=>'Full-Time', :zh_tw=>'專任'}
role_status.save
role_status = RoleStatus.new
role_status.key = 'Adjunct'
role_status.role_id = role.id
role_status.title_translations = {:en=>'Adjunct', :zh_tw=>'兼任'}
role_status.save
role_status = RoleStatus.new
role_status.key = 'appointment'
role_status.role_id = role.id
role_status.title_translations = {:en=>'appointment', :zh_tw=>'合聘'}
role_status.save
attribute_field = AttributeField.new
attribute_field.key = 'job_title'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Job Title', :zh_tw=>'職稱'}
attribute_field.markup = 'select'
attribute_field.option_list = {
"0" => { "zh_tw" => "教授兼系主任",
"en" => "Professor and Chairman" },
"1" => { "zh_tw" => "教授",
"en" => "Professor" },
"2" => { "zh_tw" => "副教授",
"en" => "Associate Professor" },
"3" => { "zh_tw" => "助理教授",
"en" => "Assistant Professor" },
"4" => { "zh_tw" => "講師",
"en" => "Lecturer" }
}
attribute_field.typeB = {
"initial" => { "zh_tw" => "",
"en" => "" },
"option_list" => {
"0" => { "zh_tw" => "教授兼系主任",
"en" => "Professor and Chairman" },
"1" => { "zh_tw" => "教授",
"en" => "Professor" },
"2" => { "zh_tw" => "副教授",
"en" => "Associate Professor" },
"3" => { "zh_tw" => "助理教授",
"en" => "Assistant Professor" },
"4" => { "zh_tw" => "講師",
"en" => "Lecturer" }
} }
attribute_field.save
attribute_field = AttributeField.new
attribute_field.key = 'Research_Expertise'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Research Expertise', :zh_tw=>'研究專長'}
attribute_field.markup = 'text_area'
attribute_field.save
attribute_field = AttributeField.new
attribute_field.key = 'Teaching_Field'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Teaching Field', :zh_tw=>'授課領域'}
attribute_field.markup = 'text_area'
attribute_field.save
#satff
role = Role.new
role.key = 'staff'
role.title_translations = {:en=>'Staff', :zh_tw=>'行政人員'}
role.save
role_status = RoleStatus.new
role_status.key = 'Full-Time'
role_status.role_id = role.id
role_status.title_translations = {:en=>'Full-Time', :zh_tw=>'全職'}
role_status.save
role_status = RoleStatus.new
role_status.key = 'Part-time'
role_status.role_id = role.id
role_status.title_translations = {:en=>'Part-time', :zh_tw=>'兼職'}
role_status.save
role_status = RoleStatus.new
role_status.key = 'Contracted'
role_status.role_id = role.id
role_status.title_translations = {:en=>'Contracted', :zh_tw=>'約聘'}
role_status.save
attribute_field = AttributeField.new
attribute_field.key = 'job_title'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Job Title', :zh_tw=>'職稱'}
attribute_field.markup = 'select'
attribute_field.option_list = {
"0" => { "zh_tw" => "組長",
"en" => "Group Leader" },
"1" => { "zh_tw" => "編審",
"en" => "Editor" },
"2" => { "zh_tw" => "技士",
"en" => "Technician" },
"3" => { "zh_tw" => "幹事",
"en" => "Assistant" },
"4" => { "zh_tw" => "組員",
"en" => "Clerk" } ,
"5" => { "zh_tw" => "工讀",
"en" => "Work-study" }
}
attribute_field.typeB = {
"initial" => { "zh_tw" => "",
"en" => "" },
"option_list" => {
"0" => { "zh_tw" => "組長",
"en" => "Group Leader" },
"1" => { "zh_tw" => "編審",
"en" => "Editor" },
"2" => { "zh_tw" => "技士",
"en" => "Technician" },
"3" => { "zh_tw" => "幹事",
"en" => "Assistant" },
"4" => { "zh_tw" => "組員",
"en" => "Clerk" } ,
"5" => { "zh_tw" => "工讀",
"en" => "Work-study" }
} }
attribute_field.save
attribute_field = AttributeField.new
attribute_field.key = 'Duties'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Duties', :zh_tw=>'承辦業務'}
attribute_field.markup = 'text_area'
attribute_field.save
attribute_field = AttributeField.new
attribute_field.key = 'Deputy'
attribute_field.role_id = role.id
attribute_field.title_translations = {:en=>'Deputy', :zh_tw=>'代理人'}
attribute_field.markup = 'text_area'
attribute_field.save
end
if User.all.count==0
group = Workgroup.where(:key=>'admin').first
profile = MemberProfile.new
profile.first_name_translations = {:en=>'Digital', :zh_tw=>'Digital'}
profile.last_name_translations = {:en=>'Ruling', :zh_tw=>'Ruling'}
profile.email = "orbit@rulingcom.com"
profile.is_hidden = true
profile.save
user = User.new
user.workgroup = group
user.member_profile = profile
user.user_name = "rulingcom"
user.password_digest = "$2a$12$ODoNQJc22aU.U6HpWhUJIeKRYIf/3xcfzym8V.4tqt/V7pM2c9alq"
user.email = "orbit@rulingcom.com"
user.approved = true
user.beta_tester = true
user.save(:validate=>false)
profile = MemberProfile.new
profile.first_name_translations = {:en=>'Admin', :zh_tw=>'Admin'}
profile.last_name_translations = {:en=>'Admin', :zh_tw=>'Admin'}
profile.email = "service@rulingcom.com"
profile.save
user = User.new
user.workgroup = group
user.member_profile = profile
user.user_name = "admin"
user.password_digest = "$2a$12$zCT/qE6TdMUcYzA8NHhM/.G150JlazF1PRThjOuuxYt3Oi7DBrHrK"
user.email = "service@rulingcom.com"
user.approved = true
user.save(:validate=>false)
end
MemberProfileDefaultField.get_field_to_set MemberProfileDefaultField.get_field_to_set
MemberIndexCache.destroy_all MemberIndexCache.all.delete_all
env_pwd = ENV['PWD'] env_pwd = ENV['PWD']
dir_name = env_pwd.split('/')[-1] dir_name = env_pwd.split('/')[-1]
fix_css_file
fix_template_header_file
fix_datatable_width_issue
watch_dog = %x[screen -ls auto_reopen_#{dir_name}] watch_dog = %x[screen -ls auto_reopen_#{dir_name}]
all_css_files = Dir["app/templates/**/*.css","app/templates/**/*.scss"]
contents = all_css_files.collect do |css_file|
[css_file,File.open(css_file, "r") { |f| f.read }]
end
s=contents.collect do |f,c|
c_tp = c
tmp = []
flag = false
c_tp = c_tp.gsub(/(^[ \t]*background(?:(?!\R).)*:[ \t]*linear-gradient(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*-pie-background/)
get_indent = var1.scan(/([ \t]*)background/).flatten[0].to_s
t = var1.sub(/background.*:/,'-pie-background:')
m = "#{var1}\n#{t}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
tmp << m
flag = true
end
m
end
c_tp = c_tp.gsub(/^([ \t]*border-radius:[ \t]*\d+(?:em|px|%|cm|pt|rem|vw|vh)(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*behavior: url\(\"\/assets\/ie_support\/PIE2\/PIE.htc\"\);/)
get_indent = var1.scan(/([ \t]*)border-radius/).flatten[0].to_s
m = "#{var1}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
tmp << m
flag = true
end
m
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
flag ? [f,tmp] : nil
end.compact
def gsub_block(c,rg,block)
return c.scan(rg) if !block
result = c.gsub(rg) do |str|
block.call(str)
end
end
def get_js_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<script|<%=[ \t]*javascript_include_tag)(?:(?!<script|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<script|<%=|<%#|<\/script>).)*(?:<\/script>|%>)[ \t\r]*(?:\n)*/m,block)
end
def get_css_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<link|<%=[ \t]*stylesheet_link_tag)(?:(?!<link|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<link|<%=|<%#).)*(?:>|%>)[ \t\r]*(?:\n)*/m,block)
end
all_html_files = Dir['app/templates/**/*.html','app/templates/**/*.erb']
all_html_contents = all_html_files.collect{|file| [file,File.open(file,'r'){|f| f.read}]}
s = all_html_contents.collect do |f,c|
flag = false
c_tp = c.clone
tmp = []
c_tp = get_js_include_name(c_tp,"(?:html5shiv|jquery|jquery\_prefix|response|ie9|cssParser|polycalc|jquery\.cycle2|jquery\.cycle2\.carousel|jquery\.cycle2\.scrollVert|jquery\.cycle2\.tile|jquery\.dataTables|dataTables\.responsive)") do |m|
flag = true
tmp << m
m = ''
end
c_tp = get_css_include_name(c_tp,"(?:responsive\.dataTables)") do |m|
flag = true
tmp << m
m = ''
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
tmp.length>0 ? [f,tmp] : nil
end.compact
all_head_files = Dir['app/templates/*/partial/_head.html.erb','app/templates/mobile/orbit_mobile/partial/_head.html.erb']
all_head_files.each do |f|
c = File.open(f, "r") { |f| f.read }
flag = false
if c.scan(/<%= render 'shared\/ie_html5_fix' %>/).length == 0
c = "<%= render 'shared/ie_html5_fix' %>\n#{c}"
flag = true
end
c = get_css_include_name(c,/\/\/(?:(?!\n).)*bootstrap/) do |m|
flag = true
v_match = m.scan(/"(.*)"/).flatten[0]
v = v_match.start_with?('http') ? v_match : ("http:"+v_match)
save_path = File.expand_path('../../assets/stylesheets/bootstrap/bootstrap.min.css',f)
system("wget #{v} -O #{save_path}")
m.sub(v_match,"bootstrap/bootstrap.min")
end
if flag
File.open(f, "w") { |f| f.write c }
end
end
#autofix DataTable width 0 issue
bs = "[ \t\r\n]*"
Dir['app/templates/**/*.erb'].each do |v|
c = File.open(v,'r'){|f| f.read}
flag = false
c = c.gsub(/(<table(?:(?!>).)*class=\"(?:(?!>).)*) nowrap((?:(?!>).)*\"(?:(?!>).)*>)/m) do |m|
flag = true
"#{$1}#{$2}"
end
c = c.gsub(/\$\('\.i-member span a'\)/) do |m|
flag = true
"$('.i-member span.member-data-value-name a')"
end
c = c.gsub(/^([ \t]*)(\$\((?:(?!\n).)*\))\.(DataTable\({#{bs}searching:#{bs}false,#{bs}paging:#{bs}false,#{bs}ordering:#{bs}false,#{bs}info:#{bs}false(?:(?!,|\n).)*(?:(?!autoWidth|}).)*)[\r\n]+^[ \t]*}\)/m) do |m|
flag = true
indent = $1
new_text = "#{indent}#{$2}.each(function(){\n#{indent} if($(this).find('thead').length!=0 && $(this).find('td').length!=0 && !$(this).hasClass('dataTable')){\n#{indent} $(this).#{$3.gsub(/^ /,' ')},\n#{indent} autoWidth: false\n#{indent} });\n#{indent} }\n#{indent}});"
new_text
end
if flag
File.open(v,'w'){|f| f.write c}
end
end
if watch_dog.scan("auto_reopen_#{dir_name}").count != 0 if watch_dog.scan("auto_reopen_#{dir_name}").count != 0
bundler_with_clean_env{%x[screen -ls | grep auto_reopen_#{dir_name} | cut -d. -f1 | awk '{print $1}' | xargs kill]} bundler_with_clean_env{%x[screen -ls | grep auto_reopen_#{dir_name} | cut -d. -f1 | awk '{print $1}' | xargs kill]}
end end
@ -474,312 +55,28 @@ if File.basename($0) != 'rake'
if !Rails.const_defined?('Console') if !Rails.const_defined?('Console')
Thread.new do Thread.new do
begin begin
admin_message = ShowForAdminMessage.first fix_script_permission
save_flag = false fix_page_part
message_tmp = '' fix_broken_page
PagePart.all.group_by{|v| [v.part_id,v.page_id]}.each do |k,v|
next if k[1].nil? #For popup window
if v.count > 1
page = Page.find(k[1])
all_sub_parts = []
new_v = []
v.each do |v1|
tmp_sub_parts = v1.sub_parts
if tmp_sub_parts.count == 0
v1.destroy
else
all_sub_parts += tmp_sub_parts
new_v << v1
end
end
if new_v.count > 1
if message_tmp.exclude? ("<a href=\"#{page.url}\">#{page.name}</a>")
message_tmp += "<a href=\"#{page.url}\">#{page.name}</a><br>"
end
save_flag = true
new_v.each_with_index do |p1,i1|
if i1 == 0
p1.sub_parts = all_sub_parts
p1.save!
else
p1.sub_parts = []
p1.destroy
end
end
end
end
end
if save_flag
admin_message_message_tp = admin_message.message
admin_message_message_tp << [I18n.t('please_check_for_repeate_data'),message_tmp,false]
admin_message.update_attributes(message: admin_message_message_tp)
end
broken_pages = Page.all.select{|v| v.child_page_ids.include?(v.id)}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.child_page_ids -= [p.id]
p.save
end
end
broken_pages = Page.all.select{|v| v.id==v.parent_page_id}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.parent_page_id = page_root.id
p.save
end
end
# Site.make_cache # Site.make_cache
if (!(site.tmp_flags.include?("uf1")) rescue false) fix_empty_user
MemberProfile.any_of([{:is_approved => nil},{:tmp_name=>nil}]).each do |member| fix_old_content_unit(site)
if (member.user.approved rescue true) fix_member_info_cache(site)
member.is_approved = true fix_db_index
else fix_asset_data(site)
member.is_approved = false fix_user_password_notify(site)
end fix_site_map(site)
begin fix_login_page(site)
member.save! fix_sort_number(site)
rescue => e fix_thumb(site)
puts "member save failed" fix_selection_option(site)
puts e.to_s fix_member_profile_field(site)
end fix_member_page_cache(site)
end fix_module_app(site)
User.where(:member_name=>nil).each do |user| fix_impression_count(site)
user.fix_member_name
end
Site.update_all("$push"=>{"tmp_flags"=>'uf1'})
end
MemberProfile.where(:is_approved => nil).each do |member|
if (member.user.approved rescue true)
member.is_approved = true
else
member.is_approved = false
end
member.save
end
MemberProfile.where({:tmp_name=>nil}).each do |member|
update_key = {}
member.name_translations.each do |k, v|
update_key["tmp_name.#{k}"] = v
end
MemberProfile.where(id: member.id).update_all(update_key)
end
ModuleApp.create_indexes
Tag.create_indexes
Category.create_indexes
Page.create_indexes
RoleStatus.create_indexes
MemberProfile.create_indexes
Role.create_indexes
User.create_indexes
AttributeValue.create_indexes
Impression.index(impressionable_type: -1,impressionable_id: -1)
Impression.index(created_at: -1)
Impression.create_indexes
PageCacheModel.create_indexes
['bulletin', 'page_context', 'web_link'].each do |app|
app_class = app.classify.constantize rescue nil
if !app_class.nil?
[{updated_at: -1}, {view_count: -1}].map{|v| app_class.index(v)}
app_class.create_indexes
end
end
image_regex = Asset::ImageRegex
#remove duplicated index
AssetTag.collection.indexes.drop_one({asset_wrap_id: 1}) rescue nil
AssetTag.collection.indexes.drop_one({asset_tag_id: -1, created_at: -1}) rescue nil
AssetTag.collection.indexes.drop_one({is_public: 1, is_admin: -1}) rescue nil
Asset.collection.indexes.drop_one({_fts: 'text', _ftsx: 1}) rescue nil
Asset.where("data" => image_regex).update_all(is_image: true)
Asset.where(:data.not => image_regex).update_all(is_image: false)
Asset.where(size: nil).each{|asset| asset.save}
AssetTag.create_indexes
SubPart.create_indexes
PagePart.create_indexes
Asset.create_indexes
AssetTag.all.update_all(assets_count: 0)
Asset.collection.aggregate([{"$match"=>{"user_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>"$user_id","count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
User.where(id: gp['_id']).update_all(assets_count: gp['count'])
end
Asset.collection.aggregate([{"$match"=>{"asset_tag_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>{"tag_id" => "$asset_tag_id","parent_ids" => "$root_asset_tag_ids"},"count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
AssetTag.where(:id => gp['_id']['tag_id']).update_all(assets_count: gp['count'])
end
# Email.where(template: 'reset_password_mailer/reset_user_password',"template_data.period_flag" => true).count
if (!(site.tmp_flags.include?("uf2")) rescue false)
flag = !User::PasswordValidTime.nil?
User.all.each do |v|
m = MemberProfile.where(id: v.member_profile_id).first
if m.nil?
v.destroy
end
if flag
send_date = v.password_updated_at.nil? ? Time.now : (v.password_updated_at+User::PasswordValidTime)
v.send_password_reset_email(send_date: send_date,period_flag: true,email: m.email)
end
end
Site.update_all("$push"=>{"tmp_flags"=>'uf2'})
end
if (!(site.tmp_flags.include?("uf3")) rescue false)
MemberProfile.all.each do |m|
m.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf3'})
end
if (!(site.tmp_flags.include?("asf1")) rescue false)
Asset.where(:title=>nil).each do |a|
a.title_translations = I18n.available_locales.map{|l| [l.to_s, a[:data]]}.to_h
a.description_translations = I18n.available_locales.map{|l| [l.to_s, ""]}.to_h
a.save
end
Site.update_all("$push"=>{"tmp_flags"=>'asf1'})
end
if (!(site.tmp_flags.include?("sm1")) rescue false)
Page.where(:page_id=>'sitemap').update_all("name.zh_tw"=>"網站導覽")
Site.update_all("$push"=>{"tmp_flags"=>'sm1'})
end
if (!(site.tmp_flags.include?("sm2")) rescue false)
Page.where(:module => "login_page").each do |p|
p.instance_variable_set(:@skip_callback, true)
p.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sm2'})
end
if (!(site.tmp_flags.include?("sort1")) rescue false)
Category.where(:sort_number=>nil).each do |c|
c.sort_number = 0
c.save
end
Tag.where(:sort_number=>nil).each do |t|
t.sort_number = 0
t.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sort1'})
end
if File.exist?('start_site.sh') && File.stat('start_site.sh').mode < 0100755
tmp = File.read('start_site.sh')
File.open('start_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'start_site.sh')
end
if File.exist?('close_site.sh') && File.stat('close_site.sh').mode < 0100755
tmp = File.read('close_site.sh')
File.open('close_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'close_site.sh')
end
force_update_thumb = !(site.tmp_flags.include?('uf4'))
if `which fc-list`.present? && `fc-list |grep 'MingLiU'`.blank?
puts "Installing fonts for system to fix fetching template bug ..."
system("wget -q http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/fonts.tgz?inline=false -O fonts.tgz")
system("mkdir ~/.local/share/fonts -p")
system("tar -zxf fonts.tgz -C ~/.local/share && fc-cache -r")
puts "Finish installing fonts!"
force_update_thumb = true
end
Multithread.where(:key=>"fetch_design_thumbs").destroy
require File.expand_path('../fetch_design_thumb', __FILE__)
puts 'fetch_design_thumb start'
fetch_design_thumb(site,current_locale,nil,force_update_thumb)
puts 'fetch_design_thumb finish'
Site.update_all("$push"=>{"tmp_flags"=>'uf4'}) if force_update_thumb
if (!(site.tmp_flags.include?("uf5")) rescue false)
SelectOption.all.to_a.group_by{|o| [o.field_name, o.sub_part_id]}.each do |k, select_options|
select_options[1..-1].each{|o| o.destroy}
end
Site.update_all("$push"=>{"tmp_flags"=>'uf5'})
end
if (!(site.tmp_flags.include?("uf6")) rescue false)
MemberProfileField.where(:option_list.in=>[{},nil]).each do |m|
m.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf6'})
end
if (!(site.tmp_flags.include?("uf7")) rescue false)
MemberProfile.where(:is_approved=>true).each do |member|
if (member.user && !(member.user.approved))
member.user.update(:approved=>true)
end
end
user = User.where(:user_name=>'rulingcom').first
user.member_profile.update(:is_hidden=>true) if user && user.member_profile
Site.update_all("$push"=>{"tmp_flags"=>'uf7'})
end
if (!(site.tmp_flags.include?("uf8")) rescue false)
users = User.where(beta_tester: true)
users.each do |user|
user.member_profile.update(:is_hidden=>true) if user.member_profile
end
Site.update_all("$push"=>{"tmp_flags"=>'uf8'})
end
if (!(site.tmp_flags.include?("uf10")) rescue false)
Page.where(:module=>'member').each do |page|
page.custom_array_field = page.custom_array_field.to_a.sort_by{|a| a['sort_order'].to_i}
page.custom_array_field_for_show = page.custom_array_field_for_show.to_a.sort_by{|a| a['sort_order'].to_i}
page.save
end
MemberProfile.cache_member_page
Site.update_all("$push"=>{"tmp_flags"=>'uf10'})
end
if (!(site.tmp_flags.include?("uf11")) rescue false)
ModuleApp.all.each do |module_app|
module_app.update_tmp_sort_number
end
Site.update_all("$push"=>{"tmp_flags"=>'uf11'})
end
if (!(site.tmp_flags.include?("uf12")) rescue false)
Tag.where(:module_app_ids.in=>[[], nil]).each do |tag|
tag.module_app_ids = ModuleApp.where(:tag_ids=>tag.id).pluck(:id)
tag.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf12'})
end
ImpressionCount.create_indexes
if (!(site.tmp_flags.include?("imc1")) rescue false)
ImpressionCount.migrate_all
site.tmp_flags << "imc1"
site.tmp_flags << "imc2"
Site.update_all("$push"=>{"tmp_flags"=>{"$each"=>['imc1', 'imc2']}})
elsif (!(site.tmp_flags.include?("imc2")) rescue false)
if Rails.env == 'production'
rails_root = Rails.root
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
default_cpu_cores = cpu_cores
if File.exists?("#{rails_root}/cpu_cores.txt")
cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i rescue default_cpu_cores
else
begin
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i if (File.exists?("#{rails_root}/../cpu_cores.txt") rescue false)
rescue => e
cpu_cores = default_cpu_cores
end
end
cpu_cores = 1 if (cpu_cores < 1)
else
cpu_cores = 1
end
last_date = ImpressionCount.order_by(:date=>-1)[cpu_cores*2].date rescue nil
if last_date.nil?
ImpressionCount.migrate_all
else
mongoid_version = Mongoid.default_client.command(buildInfo: 1).first[:version].to_f rescue 2.6
if mongoid_version >= 3.6
last_date = DateTime.parse(last_date.to_s+" 00:00:00"+Time.zone.formatted_offset)
else #mongoid < 3.6 only support UTC
last_date = DateTime.parse(last_date.to_s+" 00:00:00+00:00")
end
ImpressionCount.migrate_all({"$match"=>{"created_at"=>{"$gte"=>last_date}}})
end
Site.update_all("$push"=>{"tmp_flags"=>'imc2'})
end
site.tmp_flags = Site.pluck(:tmp_flags).flatten.uniq
site.save
if ChecklistMainSetting.count == 0 if ChecklistMainSetting.count == 0
ChecklistMainSetting.create ChecklistMainSetting.create
end end
ChecklistSubSetting.create_indexes
ChecklistField.create_indexes
ChecklistRecord.create_indexes
ChecklistValue.create_indexes
rescue => e rescue => e
puts [e,e.backtrace] puts [e,e.backtrace]
end end

View File

@ -0,0 +1,507 @@
def fix_email_job
crontab_email=%x[crontab -l|grep 'bundle exec rake email:deliver_all'|grep `pwd`]
crontab_email_all = crontab_email.split("\n")
if crontab_email_all.count >= 1
crontab_jobs=%x[crontab -l|grep -v "cd `pwd`.*bundle exec rake email:deliver_all"].gsub(/^\n$/,'').gsub(/'/,"'\"'\"'")
system("echo '#{crontab_jobs}' | crontab -")
end
CronWorker.cron_every_minute(Email.method(:deliver_all))
end
def fix_site_root_url(site)
nginx_exe = %x[ps -o args -C nginx| grep -E 'daemon|master'| awk '{print $4}'].split("\n")[0]
if !nginx_exe.nil?
nginx_config = %x[#{nginx_exe} -V 2>&1 | grep -o '\\-\\-conf-path=\\(.*conf\\)' | cut -d '=' -f2].sub("\n",'')
orbit_sites_config = %x[grep orbit_sites #{nginx_config}].scan(/^[\t ]*include(.*);/)[0][0].gsub(/ /,'')
current_site_config = %x[grep -H `pwd` #{orbit_sites_config}|grep -E ':\s\+root'].split(':')[0]
current_site_config_content = current_site_config.present? ? %x[cat #{current_site_config}] : ""
config_domain = site.site_settings['domain'] rescue nil
server_names = current_site_config_content.scan(/^[\t ]*server_name[\t ]*(.*);/).flatten.collect{|v| v.to_s.split(/[\t ]+/)}.flatten.sort_by{|server_name| server_name.include?('orbit') ? 100 : server_name.starts_with?('www') ? 0 : server_name==config_domain ? 1 : server_name.split('.').count}
ip = UDPSocket.open {|s| s.connect("8.8.8.8", 1); s.addr.last} rescue nil
new_server_names = server_names.uniq.select{|server_name| ip == (IPSocket.getaddress(server_name) rescue nil)}
server_name = new_server_names.select{|server_name| ip != server_name}.first
if server_name.nil?
if new_server_names.count != 0
server_name = new_server_names.first
else
server_name = server_names.first
if server_name.nil?
server_name = ip || (`ip route get 8.8.8.8|xargs|awk '{print $7}'`.strip)
end
end
end
ports = current_site_config_content.scan(/^[\t ]*listen[\t ]*(.*);/).flatten.collect{|v| v.split(/[\t ]+/)}.flatten.select{|v| v.to_i.to_s == v}
port = ports.blank? ? '80' : (ports.select{|v| v=='443'}.blank? ? ports.sort_by{|v| v.to_i==80 ? -1 : v.to_i}[0] : '443')
root_url = (port=='443' ? "https://#{server_name}" : (port=='80' ? "http://#{server_name}" : "http://#{server_name}:#{port}"))
site.update_attributes(root_url: root_url)
end
end
def gsub_block(c,rg,block)
return c.scan(rg) if !block
result = c.gsub(rg) do |str|
block.call(str)
end
end
def get_js_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<script|<%=[ \t]*javascript_include_tag)(?:(?!<script|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<script|<%=|<%#|<\/script>).)*(?:<\/script>|%>)[ \t\r]*(?:\n)*/m,block)
end
def get_css_include_name(c,name,&block)
gsub_block(c,/^[ \t]*(?:<link|<%=[ \t]*stylesheet_link_tag)(?:(?!<link|<%=|<%#).)*[\"\/\']+#{name}[-\d\.]*(?:\.js|\.min|\")(?:(?!<link|<%=|<%#).)*(?:>|%>)[ \t\r]*(?:\n)*/m,block)
end
def fix_css_file
all_css_files = Dir["app/templates/**/*.css","app/templates/**/*.scss"]
contents = all_css_files.collect do |css_file|
[css_file,File.open(css_file, "r") { |f| f.read }]
end
contents.each do |f,c|
c_tp = c
flag = false
c_tp = c_tp.gsub(/(^[ \t]*background(?:(?!\R).)*:[ \t]*linear-gradient(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*-pie-background/)
get_indent = var1.scan(/([ \t]*)background/).flatten[0].to_s
t = var1.sub(/background.*:/,'-pie-background:')
m = "#{var1}\n#{t}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
flag = true
end
m
end
c_tp = c_tp.gsub(/^([ \t]*border-radius:[ \t]*\d+(?:em|px|%|cm|pt|rem|vw|vh)(?:(?!\R|;).)*;[ \r]*(?:\/\/(?:(?!\R|;).)*)*)[ \r\t]*[\n]*([\t ]*(?:(?!\R|;).)*(?:$|;))/m) do |m|
var1 = $1
var2 = $2
if !var2.to_s.match(/^[ \t]*behavior: url\(\"\/assets\/ie_support\/PIE2\/PIE.htc\"\);/)
get_indent = var1.scan(/([ \t]*)border-radius/).flatten[0].to_s
m = "#{var1}\n#{get_indent}behavior: url(\"/assets/ie_support/PIE2/PIE.htc\");\n#{var2}"
flag = true
end
m
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
end
end
def fix_template_header_file
all_html_files = Dir['app/templates/**/*.html','app/templates/**/*.erb']
all_html_contents = all_html_files.collect{|file| [file,File.open(file,'r'){|f| f.read}]}
all_html_contents.each do |f,c|
flag = false
c_tp = c.clone
c_tp = get_js_include_name(c_tp,"(?:html5shiv|jquery|jquery\_prefix|response|ie9|cssParser|polycalc|jquery\.cycle2|jquery\.cycle2\.carousel|jquery\.cycle2\.scrollVert|jquery\.cycle2\.tile|jquery\.dataTables|dataTables\.responsive)") do |m|
flag = true
m = ''
end
c_tp = get_css_include_name(c_tp,"(?:responsive\.dataTables)") do |m|
flag = true
m = ''
end
if flag
File.open(f, "w") { |f| f.write c_tp }
end
end
all_head_files = Dir['app/templates/*/partial/_head.html.erb','app/templates/mobile/orbit_mobile/partial/_head.html.erb']
all_head_files.each do |f|
c = File.open(f, "r") { |f| f.read }
flag = false
if c.scan(/<%= render 'shared\/ie_html5_fix' %>/).length == 0
c = "<%= render 'shared/ie_html5_fix' %>\n#{c}"
flag = true
end
c = get_css_include_name(c,/\/\/(?:(?!\n).)*bootstrap/) do |m|
flag = true
v_match = m.scan(/"(.*)"/).flatten[0]
v = v_match.start_with?('http') ? v_match : ("http:"+v_match)
save_path = File.expand_path('../../assets/stylesheets/bootstrap/bootstrap.min.css',f)
system("wget #{v} -O #{save_path}")
m.sub(v_match,"bootstrap/bootstrap.min")
end
if flag
File.open(f, "w") { |f| f.write c }
end
end
end
def fix_datatable_width_issue
#autofix DataTable width 0 issue
bs = "[ \t\r\n]*"
Dir['app/templates/**/*.erb'].each do |v|
c = File.open(v,'r'){|f| f.read}
flag = false
c = c.gsub(/(<table(?:(?!>).)*class=\"(?:(?!>).)*) nowrap((?:(?!>).)*\"(?:(?!>).)*>)/m) do |m|
flag = true
"#{$1}#{$2}"
end
c = c.gsub(/\$\('\.i-member span a'\)/) do |m|
flag = true
"$('.i-member span.member-data-value-name a')"
end
c = c.gsub(/^([ \t]*)(\$\((?:(?!\n).)*\))\.(DataTable\({#{bs}searching:#{bs}false,#{bs}paging:#{bs}false,#{bs}ordering:#{bs}false,#{bs}info:#{bs}false(?:(?!,|\n).)*(?:(?!autoWidth|}).)*)[\r\n]+^[ \t]*}\)/m) do |m|
flag = true
indent = $1
new_text = "#{indent}#{$2}.each(function(){\n#{indent} if($(this).find('thead').length!=0 && $(this).find('td').length!=0 && !$(this).hasClass('dataTable')){\n#{indent} $(this).#{$3.gsub(/^ /,' ')},\n#{indent} autoWidth: false\n#{indent} });\n#{indent} }\n#{indent}});"
new_text
end
if flag
File.open(v,'w'){|f| f.write c}
end
end
end
def fix_member_info_cache(site)
if ((site.tmp_flags.include?("uf1_v2")) rescue true)
return
end
MemberProfile.where({:tmp_name=>nil}).each do |member|
member_update_key = {}
user_update_key = {}
member.name_translations.each do |k, v|
member_update_key["tmp_name.#{k}"] = v
user_update_key["member_name.#{k}"] = v
end
MemberProfile.where(id: member.id).update_all(member_update_key)
User.where(member_profile_id: member.id).update_all(user_update_key)
#user.fix_member_name
end
approved_member_profile_ids = MemberProfile.where(:is_approved=>true).pluck(:id)
User.where(:member_profile_id.in => approved_member_profile_ids).update_all(approved: true)
approved_member_ids = User.where(approved: true).pluck(:member_profile_id)
MemberProfile.where(:id.in => approved_member_ids).update_all(is_approved: true)
MemberProfile.where(:id.nin => approved_member_ids).update_all(is_approved: false)
User.where(:user_name=>'rulingcom').update_all(beta_tester: true)
users = User.where(beta_tester: true)
users.each do |user|
user.member_profile.update(:is_hidden=>true) if user.member_profile
end
Site.update_all("$push"=>{"tmp_flags"=>'uf1_v2'})
end
def fix_member_page_cache(site)
if ((site.tmp_flags.include?("uf10")) rescue true)
return
end
Page.where(:module=>'member').each do |page|
page.custom_array_field = page.custom_array_field.to_a.sort_by{|a| a['sort_order'].to_i}
page.custom_array_field_for_show = page.custom_array_field_for_show.to_a.sort_by{|a| a['sort_order'].to_i}
page.save
end
MemberProfile.cache_member_page
Site.update_all("$push"=>{"tmp_flags"=>'uf10'})
end
def fix_module_app(site)
if (!(site.tmp_flags.include?("uf11")) rescue false)
ModuleApp.all.each do |module_app|
module_app.update_tmp_sort_number
end
Site.update_all("$push"=>{"tmp_flags"=>'uf11'})
end
if (!(site.tmp_flags.include?("uf12")) rescue false)
Tag.where(:module_app_ids.in=>[[], nil]).each do |tag|
tag.module_app_ids = ModuleApp.where(:tag_ids=>tag.id).pluck(:id)
tag.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf12'})
end
end
def fix_page_part
admin_message = ShowForAdminMessage.first
save_flag = false
message_tmp = ''
PagePart.all.group_by{|v| [v.part_id,v.page_id]}.each do |k,v|
next if k[1].nil? #For popup window
if v.count > 1
page = Page.find(k[1])
all_sub_parts = []
new_v = []
v.each do |v1|
tmp_sub_parts = v1.sub_parts
if tmp_sub_parts.count == 0
v1.destroy
else
all_sub_parts += tmp_sub_parts
new_v << v1
end
end
if new_v.count > 1
if message_tmp.exclude? ("<a href=\"#{page.url}\">#{page.name}</a>")
message_tmp += "<a href=\"#{page.url}\">#{page.name}</a><br>"
end
save_flag = true
new_v.each_with_index do |p1,i1|
if i1 == 0
p1.sub_parts = all_sub_parts
p1.save!
else
p1.sub_parts = []
p1.destroy
end
end
end
end
end
if save_flag
admin_message_message_tp = admin_message.message
admin_message_message_tp << [I18n.t('please_check_for_repeate_data'),message_tmp,false]
admin_message.update_attributes(message: admin_message_message_tp)
end
end
def fix_broken_page
broken_pages = Page.all.select{|v| v.child_page_ids.include?(v.id)}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.child_page_ids -= [p.id]
p.save
end
end
broken_pages = Page.all.select{|v| v.id==v.parent_page_id}
if broken_pages.length>0
page_root = Page.root
broken_pages.each do |p|
p.parent_page_id = page_root.id
p.save
end
end
end
def fix_db_index
ModuleApp.create_indexes
Tag.create_indexes
Category.create_indexes
Page.create_indexes
RoleStatus.create_indexes
MemberProfile.create_indexes
Role.create_indexes
User.create_indexes
AttributeValue.create_indexes
Impression.index(impressionable_type: -1,impressionable_id: -1)
Impression.index(created_at: -1)
Impression.create_indexes
PageCacheModel.create_indexes
['bulletin', 'page_context', 'web_link'].each do |app|
app_class = app.classify.constantize rescue nil
if !app_class.nil?
[{updated_at: -1}, {view_count: -1}].map{|v| app_class.index(v)}
app_class.create_indexes
end
end
#remove duplicated index
AssetTag.collection.indexes.drop_one({asset_wrap_id: 1}) rescue nil
AssetTag.collection.indexes.drop_one({asset_tag_id: -1, created_at: -1}) rescue nil
AssetTag.collection.indexes.drop_one({is_public: 1, is_admin: -1}) rescue nil
Asset.collection.indexes.drop_one({_fts: 'text', _ftsx: 1}) rescue nil
AssetTag.create_indexes
SubPart.create_indexes
PagePart.create_indexes
Asset.create_indexes
ImpressionCount.create_indexes
ChecklistSubSetting.create_indexes
ChecklistField.create_indexes
ChecklistRecord.create_indexes
ChecklistValue.create_indexes
end
def fix_asset_data(site)
if ((site.tmp_flags.include?("asf1")) rescue true)
return
end
image_regex = Asset::ImageRegex
Asset.where("data" => image_regex).update_all(is_image: true)
Asset.where(:data.not => image_regex).update_all(is_image: false)
Asset.where(size: nil).each{|asset| asset.save}
AssetTag.all.update_all(assets_count: 0)
Asset.collection.aggregate([{"$match"=>{"user_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>"$user_id","count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
User.where(id: gp['_id']).update_all(assets_count: gp['count'])
end
Asset.collection.aggregate([{"$match"=>{"asset_tag_id"=>{"$ne"=>nil}}},{"$group"=>{"_id"=>{"tag_id" => "$asset_tag_id","parent_ids" => "$root_asset_tag_ids"},"count"=>{"$sum"=> 1}}}]).to_a.each do |gp|
AssetTag.where(:id => gp['_id']['tag_id']).update_all(assets_count: gp['count'])
end
Asset.where(:title=>nil).each do |a|
a.title_translations = I18n.available_locales.map{|l| [l.to_s, a[:data]]}.to_h
a.description_translations = I18n.available_locales.map{|l| [l.to_s, ""]}.to_h
a.save
end
Site.update_all("$push"=>{"tmp_flags"=>'asf1'})
end
def fix_empty_user
member_profile_ids = MemberProfile.all.pluck(:id).to_set
empty_user_ids = []
User.all.pluck(:id, :member_profile_id).each do |u|
if !member_profile_ids.include?(u[1])
empty_user_ids << u[0]
end
end
if !empty_user_ids.empty?
User.where(:id.in => empty_user_ids).delete_all
end
end
def fix_user_password_notify(site)
if ((site.tmp_flags.include?("uf2")) rescue true)
return
end
flag = !User::PasswordValidTime.nil?
User.all.each do |v|
if flag
send_date = v.password_updated_at.nil? ? Time.now : (v.password_updated_at+User::PasswordValidTime)
v.send_password_reset_email(send_date: send_date,period_flag: true,email: m.email)
end
end
Site.update_all("$push"=>{"tmp_flags"=>'uf2'})
end
def fix_site_map(site)
if ((site.tmp_flags.include?("sm1")) rescue true)
return
end
Page.where(:page_id=>'sitemap').update_all("name.zh_tw"=>"網站導覽")
Site.update_all("$push"=>{"tmp_flags"=>'sm1'})
end
def fix_login_page(site)
if ((site.tmp_flags.include?("sm2")) rescue true)
return
end
Page.where(:module => "login_page").each do |p|
p.instance_variable_set(:@skip_callback, true)
p.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sm2'})
end
def fix_sort_number(site)
if ((site.tmp_flags.include?("sort1")) rescue true)
return
end
Category.where(:sort_number=>nil).each do |c|
c.sort_number = 0
c.save
end
Tag.where(:sort_number=>nil).each do |t|
t.sort_number = 0
t.save
end
Site.update_all("$push"=>{"tmp_flags"=>'sort1'})
end
def fix_thumb(site)
force_update_thumb = !(site.tmp_flags.include?('uf4'))
if `which fc-list`.present? && `fc-list |grep 'MingLiU'`.blank?
puts "Installing fonts for system to fix fetching template bug ..."
system("wget -q http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/fonts.tgz?inline=false -O fonts.tgz")
system("mkdir ~/.local/share/fonts -p")
system("tar -zxf fonts.tgz -C ~/.local/share && fc-cache -r")
puts "Finish installing fonts!"
force_update_thumb = true
end
Multithread.where(:key=>"fetch_design_thumbs").destroy
require File.expand_path('../fetch_design_thumb', __FILE__)
puts 'fetch_design_thumb start'
current_locale = I18n.locale
fetch_design_thumb(site,current_locale,nil,force_update_thumb)
puts 'fetch_design_thumb finish'
Site.update_all("$push"=>{"tmp_flags"=>'uf4'}) if force_update_thumb
end
def fix_selection_option(site)
if ((site.tmp_flags.include?("uf5")) rescue true)
return
end
SelectOption.all.to_a.group_by{|o| [o.field_name, o.sub_part_id]}.each do |k, select_options|
select_options[1..-1].each{|o| o.destroy}
end
Site.update_all("$push"=>{"tmp_flags"=>'uf5'})
end
def fix_member_profile_field(site)
if ((site.tmp_flags.include?("uf6")) rescue true)
return
end
MemberProfileField.where(:option_list.in=>[{},nil]).each do |m|
m.check_option_list
m.save
end
Site.update_all("$push"=>{"tmp_flags"=>'uf6'})
end
def fix_script_permission
if File.exist?('start_site.sh') && File.stat('start_site.sh').mode < 0100755
tmp = File.read('start_site.sh')
File.open('start_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'start_site.sh')
end
if File.exist?('close_site.sh') && File.stat('close_site.sh').mode < 0100755
tmp = File.read('close_site.sh')
File.open('close_site.sh', 'w+'){|f| f.write(tmp.gsub("\r", ""))}
File.chmod(0755, 'close_site.sh')
end
end
def fix_impression_count(site)
if (!(site.tmp_flags.include?("imc1")) rescue false)
ImpressionCount.migrate_all
site.tmp_flags << "imc1"
site.tmp_flags << "imc2"
Site.update_all("$push"=>{"tmp_flags"=>{"$each"=>['imc1', 'imc2']}})
elsif (!(site.tmp_flags.include?("imc2")) rescue false)
if Rails.env == 'production'
rails_root = Rails.root
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
default_cpu_cores = cpu_cores
if File.exist?("#{rails_root}/cpu_cores.txt")
cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i rescue default_cpu_cores
else
begin
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i if File.exist?("#{rails_root}/../cpu_cores.txt")
rescue => e
cpu_cores = default_cpu_cores
end
end
cpu_cores = 1 if (cpu_cores < 1)
else
cpu_cores = 1
end
last_date = ImpressionCount.order_by(:date=>-1)[cpu_cores*2].date rescue nil
if last_date.nil?
ImpressionCount.migrate_all
else
mongoid_version = Mongoid.default_client.command(buildInfo: 1).first[:version].to_f rescue 2.6
if mongoid_version >= 3.6
last_date = DateTime.parse(last_date.to_s+" 00:00:00"+Time.zone.formatted_offset)
else #mongoid < 3.6 only support UTC
last_date = DateTime.parse(last_date.to_s+" 00:00:00+00:00")
end
ImpressionCount.migrate_all({"$match"=>{"created_at"=>{"$gte"=>last_date}}})
end
Site.update_all("$push"=>{"tmp_flags"=>'imc2'})
end
end
def fix_old_content_unit(site)
if !AutoConvertUnitHelper::AutoConvertFlag || ((site.tmp_flags.include?("poc1")) rescue true)
return
end
AutoConvertUnitHelper.process_old_content
Site.update_all("$push"=>{"tmp_flags"=>'poc1'})
end

View File

@ -16,7 +16,7 @@ if !default_locale.nil?
I18n.locale = default_locale I18n.locale = default_locale
end end
[MemberProfile,Page,Role,RoleStatus,AttributeValue,AttributeField,MemberProfileField,MemberProfileFieldValue,MemberProfileDefaultField,MemberRelation].each do |m| [MemberProfile,Role,RoleStatus,AttributeValue,AttributeField,MemberProfileField,MemberProfileFieldValue,MemberProfileDefaultField,MemberRelation].each do |m|
m.class_eval{include MemberCleanCache} m.class_eval{include MemberCleanCache}
end end
require File.expand_path('../../app/models/page_cache_model', __FILE__) rescue nil require File.expand_path('../../app/models/page_cache_model', __FILE__) rescue nil
@ -26,7 +26,7 @@ if (site.enable_cache rescue false)
require 'active_support/concern' require 'active_support/concern'
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
excluded_classes = [Asset,AssetTag,Email,EmailLog,Multithread,Site,Impression,ModuleApp,OrbitMemberModule,OrbitWidget,PageCacheModel,UserAction] excluded_classes = [Asset,AssetTag,Email,EmailFile,EmailLog,Multithread,Site,Impression,ModuleApp,OrbitMemberModule,OrbitWidget,PageCacheModel,UserAction,UserLoginLog,TicketStatus,Workgroup,MemberIndexCache,MemberEditTemp]
excluded_classes += OrbitApp::Module::Registration.avoid_page_cache excluded_classes += OrbitApp::Module::Registration.avoid_page_cache
if excluded_classes.exclude?(self) if excluded_classes.exclude?(self)
custom_flag = false custom_flag = false
@ -42,27 +42,21 @@ if (site.enable_cache rescue false)
if drop_flag if drop_flag
PageCacheModel.delete_all PageCacheModel.delete_all
else else
PageCacheModel.where(page_id: self.id).delete caches = PageCacheModel.where(page_id: self.id).only(:page_id)
PageCacheModel.delete_roda_caches(caches)
caches.delete
end end
end end
true #return true to save ok true #return true to save ok
end end
elsif self == SubPart elsif [SubPart, PagePart].include?(self)
def delete_page_cache
if self.changed? || self.new_record?
page = self.page_part.page rescue nil
if page
PageCacheModel.where(page_id: self.id).delete
end
end
true #return true to save ok
end
elsif self == PagePart
def delete_page_cache def delete_page_cache
if self.changed? || self.new_record? if self.changed? || self.new_record?
page = self.page rescue nil page = self.page rescue nil
if page if page
PageCacheModel.where(page_id: self.id).delete caches = PageCacheModel.where(page_id: self.id).only(:page_id)
PageCacheModel.delete_roda_caches(caches)
caches.delete
end end
end end
true #return true to save ok true #return true to save ok
@ -79,8 +73,10 @@ if (site.enable_cache rescue false)
end end
if custom_flag if custom_flag
self.after_save :delete_page_cache_custom self.after_save :delete_page_cache_custom
self.before_destroy :delete_page_cache_custom
else else
self.after_save :delete_page_cache self.after_save :delete_page_cache
self.before_destroy :delete_page_cache
end end
end end
end end

View File

@ -44,6 +44,7 @@ require 'app/models/category'
Mongoid.load!("config/mongoid.yml") Mongoid.load!("config/mongoid.yml")
# require 'impressionist' # require 'impressionist'
orm = 'mongoid' orm = 'mongoid'
require "impressionist/../../app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb" require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb" require "impressionist/models/#{orm}/impressionist/impressionable.rb"
@ -62,6 +63,7 @@ if Site.count == 0
site.in_use_locales = site.valid_locales site.in_use_locales = site.valid_locales
site.site_type = "orbit" site.site_type = "orbit"
site.template = (Dir.glob("#{ENV['PWD']}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first site.template = (Dir.glob("#{ENV['PWD']}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first
site.update_flag = false
site.save site.save
end end
begin begin

View File

@ -7,10 +7,14 @@ rails_env = ENV['RAILS_ENV'] || 'production'
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2 cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
default_cpu_cores = cpu_cores default_cpu_cores = cpu_cores
if File.exist?("#{rails_root}/cpu_cores.txt") if File.exist?("#{rails_root}/cpu_cores.txt")
cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i rescue default_cpu_cores cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').strip.to_i rescue default_cpu_cores
else else
begin begin
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').sub("\n",'').to_i if (File.exist?("#{rails_root}/../cpu_cores.txt") rescue false) if (File.exist?("#{rails_root}/../cpu_cores.txt") rescue false)
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').strip.to_i
elsif (File.exist?("#{ENV['HOME']}/cpu_cores.txt") rescue false)
cpu_cores = File.read("#{ENV['HOME']}/cpu_cores.txt").force_encoding('utf-8').strip.to_i
end
rescue => e rescue => e
cpu_cores = default_cpu_cores cpu_cores = default_cpu_cores
end end
@ -21,19 +25,34 @@ worker_processes (rails_env == 'production' ? cpu_cores : 1)
# preload_app true # preload_app true
timeout 30 timeout 30
require File.expand_path("../set_global_variable",__FILE__)
site = Site.first
if (site.use_advanced_cache rescue false)
listen "#{rails_root}/tmp/unicorn_rails.sock", :backlog => 4000
pid "#{rails_root}/tmp/pids/unicorn_rails.pid"
roda_pid_file = "#{rails_root}/tmp/pids/unicorn.pid"
roda_pid = File.open(roda_pid_file, 'r').read().gsub("\n",'').to_i rescue nil
if !roda_pid || !system("kill -0 #{roda_pid}")
`cd #{rails_root}/config/roda && ulimit -n 100000 && bundle exec unicorn -c unicorn.rb -D -E production`
end
else
listen "#{rails_root}/tmp/unicorn.sock", :backlog => 4000 listen "#{rails_root}/tmp/unicorn.sock", :backlog => 4000
pid "#{rails_root}/tmp/pids/unicorn.pid"
end
if Unicorn::Configurator::RACKUP[:daemonized]
stderr_path "#{rails_root}/log/unicorn.log" stderr_path "#{rails_root}/log/unicorn.log"
stdout_path "#{rails_root}/log/unicorn.log" stdout_path "#{rails_root}/log/unicorn.log"
end
ENV['worker_num'] = '0'
before_fork do |server, worker| before_fork do |server, worker|
ENV['worker_num'] = "#{worker.nr}" ENV['worker_num'] = "#{worker.nr}"
old_pid = "#{rails_root}/tmp/pids/unicorn.pid.oldbin" old_pid = "#{server.config[:pid]}.oldbin"
if File.exist?(old_pid) && server.pid != old_pid if File.exist?(old_pid)
begin begin
Process.kill("QUIT", File.read(old_pid).to_i) sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH rescue Errno::ENOENT, Errno::ESRCH
end end
end end
end end
require File.expand_path("../set_global_variable",__FILE__)