Update temp_file

This commit is contained in:
BoHung Chiu 2022-05-29 12:59:54 +08:00
parent e4c0ee53ed
commit 85c1cc62b2
3 changed files with 272 additions and 36 deletions

View File

@ -9,6 +9,25 @@ require "rails/test_unit/railtie"
require "mongoid"
require "mongoid/railtie"
require "bson"
require 'rails'
if Rails.version.to_i>4
module AbstractController
module Callbacks
module ClassMethods
alias_method :prepend_before_filter, :prepend_before_action
alias_method :before_filter, :before_action
end
end
end
end
if RUBY_VERSION.to_f > 2.1
class Time
def new_offset(offset)
DateTime.new(self.to_i).new_offset(offset)
end
end
end
if Mongoid::VERSION.to_i < 5
require "moped"
Moped::BSON = BSON
@ -23,43 +42,52 @@ else
end
end
#monkey the behavier as before(mongoid 4)
module Mongoid
module Contextual
class Mongo
def add_default_sort_options
if !criteria.options.key?(:sort)
criteria.options[:sort] = {:_id => 1}
apply_option(:sort)
end
end
def first
add_default_sort_options
return documents.first if cached? && cache_loaded?
try_cache(:first) do
if raw_doc = view.limit(-1).first
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
eager_load([doc]).first
if Mongoid::VERSION.to_i==5
module Mongoid
module Contextual
class Mongo
def add_default_sort_options
if !criteria.options.key?(:sort)
criteria.options[:sort] = {:_id => 1}
apply_option(:sort)
end
end
end
def last
add_default_sort_options
try_cache(:last) do
with_inverse_sorting do
def first
add_default_sort_options
return documents.first if cached? && cache_loaded?
try_cache(:first) do
if raw_doc = view.limit(-1).first
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
eager_load([doc]).first
end
end
end
def last
add_default_sort_options
try_cache(:last) do
with_inverse_sorting do
if raw_doc = view.limit(-1).first
doc = Factory.from_db(klass, raw_doc, criteria.options[:fields])
eager_load([doc]).first
end
end
end
end
def documents_for_iteration
add_default_sort_options
return documents if cached? && !documents.empty?
return view unless eager_loadable?
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
eager_load(docs)
end
end
def documents_for_iteration
add_default_sort_options
return documents if cached? && !documents.empty?
return view unless eager_loadable?
docs = view.map{ |doc| Factory.from_db(klass, doc, criteria.options[:fields]) }
eager_load(docs)
end
end
end
else
Boolean = Mongoid::Boolean
class Mongoid::Criteria::Queryable::Key
def to_bson_key(validating_keys = Config.validating_keys?)
self.to_s
end
end
end
@ -101,10 +129,29 @@ end
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
dependency_gems = Gem::Specification.find_all.to_a.flat_map{|spec| spec.dependent_specs.map{|s| [s.name,s.version.to_s]}}.uniq.to_h
dependency_gems = dependency_gems.select{|gem,v| v=='0.0.1'}.keys
dependency_gems.each{|gem_name| Bundler.require.push(Bundler::Dependency.new(gem_name,:runtime))}
already_required = Bundler.require.map{|b| b.name}
if Thread.current[:dependency_gems].nil?
require File.expand_path('../set_global_variable.rb', __FILE__)
end
Thread.current[:dependency_gems].each{|gem_name| Bundler.require.push(Bundler::Dependency.new(gem_name,:runtime)) unless already_required.include?(gem_name)}
Thread.current[:dependency_gems] = nil
Bundler.require(:default, Rails.env)
class CssPostProcessor
def call(input)
d = input[:data]
[[':hover','.hover-class'],[':hover',':focus-within'],['\.ad-overlay','.banner-overlay']].each do |s,t|
d = d.gsub(/((?:(?!{|}).)*#{s}(?:(?!{|}).)*){/) do |v|
v1 = $1
v1 = v1.gsub(/((?:(?!,|\t).)*#{s}(?:(?!,|\t).)*)/) do |c|
c1 = c.gsub(/#{s}/,t)
"#{c}, #{c1}"
end
"#{v1}{"
end
end
return {data: d}
end
end
module Orbit
class Application < Rails::Application
@ -113,6 +160,9 @@ module Orbit
# -- all .rb files in that directory are automatically loaded.
# tell the I18n library where to find your translations
if Rails.version.to_i>=6
config.hosts.clear
end
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}')]
I18n.load_path += Gem::Specification.select{|v| v.source.class!=Bundler::Source::Rubygems}.map{|v| Dir[File.expand_path('../config/locales/*.yml',v.loaded_from)]}.flatten
@ -143,7 +193,14 @@ module Orbit
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
if Rails.version.to_i>=5
config.eager_load_paths << Rails.root.join('lib')
config.mongoid.belongs_to_required_by_default = false
end
# config.mongoid.observers = :orbit_observer
config.assets.configure do |env|
env.cache = ActiveSupport::Cache.lookup_store(:memory_store,{ size: 128.megabytes })
env.register_postprocessor 'text/css', CssPostProcessor.new
end
end
end

View File

@ -0,0 +1,172 @@
$:.push File.expand_path("../..", __FILE__)
require 'bundler'
require 'config/boot'
require 'lib/orbit_category/categorizable'
require 'lib/orbit_tag/taggable'
require 'lib/orbit_model/impression'
require 'lib/orbit_model/status'
require 'rails'
require "mongoid"
require "kaminari/mongoid"
require "mongoid/railtie"
require 'carrierwave/mongoid'
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
require "mongoid"
require "mongoid/railtie"
require "bson"
require 'app/models/concerns/slug'
require 'app/uploaders/image_uploader'
require 'app/models/tag'
require 'app/models/category'
Mongoid.load!("config/mongoid.yml")
require 'impressionist'
require 'app/models/site'
require 'app/models/page_set' #need for site save
if Site.count == 0
site = Site.new
extra_trans = {}
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 []
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = 'orbit'
end
site.title_translations = {:en=>"Orbit",:zh_tw=>"Orbit"}.merge(extra_trans)
site.valid_locales = [:en, :zh_tw] + extra_langs
site.in_use_locales = site.valid_locales
site.site_type = "orbit"
site.template = (Dir.glob("#{Rails.root}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first
site.save
end
built_in_ext_lines = File.readlines('built_in_extensions.rb').map{|s| s.split("#")[0].to_s} rescue []
downloaded_ext_lines = File.readlines('downloaded_extensions.rb').map{|s| s.split("#")[0].to_s} rescue []
extras_ext_lines = File.readlines('extra_gems.rb').map{|s| s.split("#")[0].to_s} rescue []
orbit_ext_lines = (built_in_ext_lines + downloaded_ext_lines + extras_ext_lines)
orbit_ext_gems = orbit_ext_lines.map{|l| l.match(/gem\s+['"](\w+)['"]/)[1] rescue nil}.select{|s| s.present?}.uniq
orbit_gem_specs = Gem::Specification.find_all.select{|g| orbit_ext_gems.include?(g.name) & (g.source.class != Bundler::Source::Rubygems)}
Thread.current[:dependency_gems] = orbit_gem_specs.flat_map{|spec| spec.dependent_specs.map{|s| s.name}}.uniq
orbit_gems_paths = orbit_gem_specs.map{|gem| gem.full_gem_path}
orbit_ext_gems_models_paths = orbit_gems_paths.map{|d| Dir.glob("#{d}/app/models/*")}
orbit_models_categorized = orbit_ext_gems_models_paths.map.with_index do |models_paths,i|
gem_name = orbit_gem_specs[i].name
categorized = models_paths.select{|fn| File.read(fn).include?("OrbitCategory::Categorizable") rescue false}.map{|fn| File.basename(fn).split(".")[0].classify}
[gem_name, categorized]
end.to_h
orbit_ext_gems_models = orbit_ext_gems_models_paths.flatten.map{|fn| File.basename(fn).split(".")[0].classify}
File.open("orbit_ext_gems_tmp.txt","w+"){|f| f.write(orbit_ext_gems.to_json)}
File.open("orbit_ext_gems_models_tmp.txt","w+"){|f| f.write(orbit_ext_gems_models.to_json)}
File.open("orbit_categorized_tmp.txt","w+"){|f| f.write(orbit_models_categorized.to_json)}
begin
require 'process_shared'
class ProcesssShareWraper
attr_accessor :data
attr_accessor :need_process
def parse_value(v)
if v[0] == String
v[1].read_string.force_encoding('utf-8')
elsif v[0] == Fixnum
v[1].read_int64
elsif v[0] == Float
v[1].read_float
else
v[1].read_object
end
end
def save_value(h,v)
if h[0] == String
h[1].write_string(v)
elsif h[0] == Fixnum
h[1].write_int64(v)
elsif h[0] == Float
h[1].write_float(v)
else
h[1].write_object(v)
end
end
def initialize(data)
self.need_process = true
def process_value(v)
case v
when String
str_len = v.bytesize * 2
str_len = 250 if str_len < 250
tmp = ProcessShared::SharedMemory.new(:uint8, str_len)
tmp.write_string(v)
[String,tmp]
when Fixnum
tmp = ProcessShared::SharedMemory.new(:int64)
tmp.write_int64(v)
[Fixnum,tmp]
when Float
tmp = ProcessShared::SharedMemory.new(:float)
tmp.write_float(v)
[Float,tmp]
else
tmp_obj = Marshal.dump(v)
tmp = ProcessShared::SharedMemory.new(:uint8, tmp_obj.bytesize*2)
tmp.write_string(tmp_obj)
[Object,tmp]
end
end
case data
when Hash
self.data = data.map do |k,v|
[k,process_value(v)]
end.to_h
when Array
self.data = data.map{|v| process_value(v)}
else
self.need_process = false
self.data = process_value(data)
end
end
def value
if self.need_process
case self.data
when Hash
self.data.map{|k,v| [k,parse_value(v)]}.to_h
when Array
self.data.map{|v| parse_value(v)}
end
else
parse_value(self.data)
end
end
def value=(v)
if self.need_process
raise 'You cannot change value.'
else
save_value(self.data,v)
end
end
def [](k)
parse_value(self.data[k])
end
def []=(k,v)
save_value(self.data[k],v)
end
end
@mutex = ProcessShared::Mutex.new
@shared_hash = {}
orbit_gem_specs.each do |v|
v.metadata['_require'].to_s.split(',').each{|m| require(m)}
tmp = v.metadata['global_hash']
tmp = tmp ? eval(tmp) : nil
gem_name = v.name
if tmp
@shared_hash[gem_name] = ProcesssShareWraper.new(tmp)
end
end
Thread.current[:shared_hash] = @shared_hash
Thread.current[:shared_mutex] = @mutex
rescue LoadError => e
puts ['error in unicorn',e,e.backtrace]
end

View File

@ -4,15 +4,21 @@ rails_root = `pwd`.gsub("\n", "")
rails_env = ENV['RAILS_ENV'] || 'production'
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i rescue 2
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)
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
default_cpu_cores = cpu_cores
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
cpu_cores = 1 if (cpu_cores < 1)
worker_processes (rails_env == 'production' ? cpu_cores : 1)
# preload_app true
timeout 30
listen "#{rails_root}/tmp/unicorn.sock", :backlog => 64
listen "#{rails_root}/tmp/unicorn.sock", :backlog => 4000
stderr_path "#{rails_root}/log/unicorn.log"
stdout_path "#{rails_root}/log/unicorn.log"
@ -25,4 +31,5 @@ before_fork do |server, worker|
rescue Errno::ENOENT, Errno::ESRCH
end
end
end
end
require File.expand_path("../set_global_variable",__FILE__)