Class: ODDB::Util::Server

Inherits:
SBSM::DRbServer
  • Object
show all
Defined in:
lib/oddb/util/server.rb

Constant Summary collapse

ENABLE_ADMIN =
true
SESSION =
Html::Util::Session
VALIDATOR =
Html::Util::Validator

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Server

Returns a new instance of Server.



20
21
22
23
24
25
# File 'lib/oddb/util/server.rb', line 20

def initialize(*args)
  super
  @rss_mutex = Mutex.new
  run_exporter if(ODDB.config.run_exporter)
  run_updater if(ODDB.config.run_updater)
end

Instance Method Details

#_admin(src, result, priority = 0) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/oddb/util/server.rb', line 26

def _admin(src, result, priority=0)
  raise "admin interface disabled" unless(self::class::ENABLE_ADMIN)
  t = Thread.new {
    Thread.current.abort_on_exception = false
    begin
      response = instance_eval(src)
      str = response.to_s
      result << if(str.length > 200)
        response.class
      else
        str
      end.to_s
    rescue StandardError => error
      result << error.message
      require 'pp'
      ODDB.logger.error('admin') { error.class }
      ODDB.logger.error('admin') { error.message }
      ODDB.logger.error('admin') { error.backtrace.pretty_inspect }
      error
    end
  }
  t[:source] = src
  t.priority = priority
  @admin_threads.add(t)
  t
end

#create_product(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/oddb/util/server.rb', line 52

def create_product name
  if prod = ODDB::Drugs::Product.find_by_name(name)
    "The product already exists. You may edit it at http://de.oddb.org/de/drugs/product/uid/#{prod.uid}"
  else
    prod = ODDB::Drugs::Product.new
    prod.name.de = name
    prod.save
    "Success! You may now edit the product at http://de.oddb.org/de/drugs/product/uid/#{prod.uid}"
  end
end

#generate_dictionariesObject



68
69
70
# File 'lib/oddb/util/server.rb', line 68

def generate_dictionaries
  generate_dictionary('german', 'de_DE@euro')
end

#generate_dictionary(language, locale) ⇒ Object



62
63
64
65
66
67
# File 'lib/oddb/util/server.rb', line 62

def generate_dictionary(language, locale)
  ODBA.storage.remove_dictionary(language)
  base = File.join(ODDB.config.data_dir,
                   "fulltext", "data", "dicts", language)
  ODBA.storage.generate_dictionary(language, locale, base)
end

#ipn(notification) ⇒ Object



71
72
73
74
# File 'lib/oddb/util/server.rb', line 71

def ipn(notification)
  Util::Ipn.process notification
  nil # don't return the invoice back across drb - it's not defined in yipn
end

#login(email, pass) ⇒ Object



75
76
77
78
# File 'lib/oddb/util/server.rb', line 75

def (email, pass)
  session = ODDB.auth.(email, pass, ODDB.config.auth_domain)
  Html::Util::KnownUser.new(session)
end

#logout(session) ⇒ Object



79
80
81
82
# File 'lib/oddb/util/server.rb', line 79

def logout(session)
  ODDB.auth.logout(session)
rescue DRb::DRbError, RangeError, NameError
end

#run_at(hour, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/oddb/util/server.rb', line 83

def run_at(hour, &block)
  Thread.new {
    loop {
      now = Time.now 
      run_at = Time.local(now.year, now.month, now.day, hour)
      while(now > run_at)
        run_at += 24*60*60
      end
      sleep(run_at - now)
      block.call
    }
  }
end

#run_exporterObject



96
97
98
# File 'lib/oddb/util/server.rb', line 96

def run_exporter
  @exporter = run_at(ODDB.config.export_hour) { Exporter.run }
end

#run_updaterObject



99
100
101
# File 'lib/oddb/util/server.rb', line 99

def run_updater
  @updater = run_at(ODDB.config.update_hour) { Updater.run }
end

#update_feedback_rss_feedObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/oddb/util/server.rb', line 102

def update_feedback_rss_feed
  async {
    begin
      @rss_mutex.synchronize {
        Export::Rss::Feedback.new.export
      }
    rescue StandardError => error
      ODDB.logger.error('rss_feed') { error.class }
      ODDB.logger.error('rss_feed') { error.message }
      ODDB.logger.error('rss_feed') { error.backtrace.pretty_inspect }
    end
  }
end