Class: DrbDb

Inherits:
Object
  • Object
show all
Includes:
Cron, DRbUndumped, Eprint, EventCache, GtkAttributes, Help, Messaging, SQL
Defined in:
lib/DrbDB.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Constant Summary collapse

INIT =
0
LOADING =
1
SERVING =
2

Constants included from Eprint

Eprint::TERM_COLOUR

Instance Attribute Summary collapse

Attributes included from Cron

#scheduler

Instance Method Summary collapse

Methods included from Messaging

#delete_message, #messages, #send_check_messages, #send_message, #sendmail_to_nick, #set_message_read, #unread_messages

Methods included from EventCache

#events, #load_all_events

Methods included from GtkAttributes

#gtk_attribute, #gtk_attribute_of_object, #load_all_attributes

Methods included from Cron

#add_cron, #remove_cron, #reset_cron, #start_cron, #stop_cron

Methods included from Help

#load_all_help, #remove_help_format, #remove_help_item, #save_help_format, #save_help_item

Methods included from SQL

#add_where, #escape_string, #fields, #guess_base, #guess_table, #init_sql, #jruby?, #prepare_sql, #qrow, #query, #rows, #select_last, #top_level, #update_manqod_db

Methods included from Eprint

#ecode, #edebug, #eerror, #eeval, #eexception, #efatal, #einfo, #eprint, #ewarn, #getBinding

Constructor Details

#initialize(connection, main_server_uri) ⇒ DrbDb

Returns a new instance of DrbDb.



19
20
21
22
23
24
25
26
27
# File 'lib/DrbDB.rb', line 19

def initialize(connection,main_server_uri)
	@state=INIT
	@connection=connection
	@main_server_uri=main_server_uri
	@moditems=Hash.new #[moditem_id][id][fields]
	@server=nil
#		@relations=Hash.new
#		@server=DRb::DRbServer.new(@connection['uri'],self)
end

Instance Attribute Details

#adminObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def admin
  @admin
end

#cacheObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def cache
  @cache
end

#clientObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def client
  @client
end

#connectionObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def connection
  @connection
end

#imagesObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def images
  @images
end

#main_serverObject

Returns the value of attribute main_server.



29
30
31
# File 'lib/DrbDB.rb', line 29

def main_server
  @main_server
end

#moditemsObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def moditems
  @moditems
end

#relation_builderObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def relation_builder
  @relation_builder
end

#serverObject

Returns the value of attribute server.



29
30
31
# File 'lib/DrbDB.rb', line 29

def server
  @server
end

#stateObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def state
  @state
end

#uriObject (readonly)



28
29
30
# File 'lib/DrbDB.rb', line 28

def uri
  @uri
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


291
292
293
294
# File 'lib/DrbDB.rb', line 291

def alive?
	true
#		server.alive?
end

#auth?(nick, password) ⇒ Boolean

Returns:

  • (Boolean)


234
235
236
237
238
# File 'lib/DrbDB.rb', line 234

def auth?(nick,password)
	r=@users.auth?(nick,password)
	edebug("auth?(#{nick}):#{r}")
	r
end

#changed_ids_of_base(base, ids, nick = nil) ⇒ Object



255
256
257
258
259
260
261
262
263
# File 'lib/DrbDB.rb', line 255

def changed_ids_of_base(base,ids,nick=nil)
	base_moditem=nil
	moditems_with_base(base){|b|
		base_moditem=b
	}
	unless base_moditem.nil?
		base_moditem.rows_changed(ids,nick)
	end
end

#each_moditem(modtype = "list") ⇒ Object



275
276
277
278
279
# File 'lib/DrbDB.rb', line 275

def each_moditem(modtype = "list")
	@moditems.each_value{|m|
		yield m if m.mod_type == modtype
	}
end

#exitObject



304
305
306
307
# File 'lib/DrbDB.rb', line 304

def exit
	@server.stop_service
	super
end

#initObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/DrbDB.rb', line 31

def init
	einfo("Memcached #{Memcached::VERSION}")
  @cache = Memcached.new([@connection['cache_host']],{:prefix_key=>key_name, :default_ttl => 0, :timeout => 30})
	DRb.start_service(@connection["uri"],self)
	@server=DRb.current_server()
	@main_server=DRb::DRbObject.new_with_uri(@main_server_uri)
	@uri=server.uri()
	unless init_sql(@connection['sql_host'],@connection['sql_user'],@connection['sql_password'],@connection['sql_db'])
		eerror("connection to sql server failed: #{@connection['sql_user']}@#{@connection['sql_host']}/#{@connection['sql_db']}")
		return nil
	end
	update_manqod_db
	begin
		begin
			@admin=DRb::DRbObject.new(nil,@connection['admin_uri'])
			we_have_admin=@admin.alive?
		rescue
			ewarn("no admin?")
			sleep 1
		end
	end until we_have_admin
	begin
		@state=LOADING
		load_all_attributes

		admin.rows("select moditems.id,modules.modname,moditems.display from moditems left join modules on modules.id = moditems.modid").each{|moditem|
			begin
				case moditem["modname"]
					when "listing"
						@moditems[moditem["id"].to_i]=DrbListModel.new(self,moditem["id"].to_i).create_skeleton unless @moditems.has_key?(moditem["id"])
						@moditems[moditem["id"].to_i].update(self)
					when "form"
						@moditems[moditem["id"].to_i]=DrbForm.new(self,moditem["id"].to_i).create_skeleton unless @moditems.has_key?(moditem["id"])
					when "relation_builder"
						@relation_builder=@moditems[moditem["id"].to_i]=DrbRelationBuilder.new(self,moditem["id"].to_i) unless @moditems.has_key?(moditem["id"])
					else
						ewarn("not caching #{moditem['display']}[#{moditem['modname']}]")
				end
			rescue =>err
				ewarn("error loading moditem: #{moditem.inspect}")
				eexception(err,:moditem => moditem)
			end
		}
		@images=DrbImages.new(self).load_all
		@users=Users.new(self).load_all
		load_all_events
		load_all_help
		start_cron
		@state=SERVING
	rescue => err
		eexception(err)
		retry
	end

	DRb.thread.join
	einfo("EXITED")
	exit
end

#key_nameObject



116
117
118
# File 'lib/DrbDB.rb', line 116

def key_name
	@connection['key_name']
end

#lock_id_of_base(base, lid) ⇒ Object



265
266
267
268
# File 'lib/DrbDB.rb', line 265

def lock_id_of_base(base,lid)
	ids=if lid.class == Array then lid else [lid] end
	moditems_with_base(base){|b| ids.each{|iid| b.lock_iter(iid)}}
end

#moditem(mod_id) ⇒ Object



121
122
123
# File 'lib/DrbDB.rb', line 121

def moditem(mod_id)
	@moditems[mod_id.to_i]
end

#moditems_with_base(base) ⇒ Object



249
250
251
252
253
# File 'lib/DrbDB.rb', line 249

def moditems_with_base(base)
	@moditems.each_value{|drblist|
		yield drblist if drblist && drblist.mod_type == "list" && drblist.base == base
	}
end

#mygroups(nick) ⇒ Object



245
246
247
# File 'lib/DrbDB.rb', line 245

def mygroups(nick)
	@users.mygroups(nick)
end

#nameObject



113
114
115
# File 'lib/DrbDB.rb', line 113

def name
	@connection['name']
end

#reload_client_attributesObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/DrbDB.rb', line 193

def reload_client_attributes
	client.load_all_attributes
	dead=Array.new
	main_server.connected_clients.each_pair{|client_id,cdb|
		if cdb[:db] == client.name
			begin
				einfo("sending reload attribute to #{client_id}")
				cdb[:object].rpc("GtkAttributeStorage.instance.load_all")
			rescue => err
				ewarn("cannot send reload attributes to #{client_id}")
				dead.push(client_id)
			end
		end
	}
	dead.each{|client_id|
		ewarn("killing dead client: #{client_id}")
		remove_client(client_id)
	}
end

#reload_client_events(goid) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/DrbDB.rb', line 213

def reload_client_events(goid)
	einfo("changed client event: #{goid}")
	client.events(goid,true)
	dead=Array.new
	main_server.connected_clients.each_pair{|client_id,cdb|
		if cdb[:db] == client.name
			begin
				einfo("sending reload event to #{client_id}")
				cdb[:object].rpc("EventCache.instance.reload_events(\"#{goid}\")")
			rescue => err
				ewarn("cannot send reload events to #{client_id}")
				dead.push(client_id)
			end
		end
	}
	dead.each{|client_id|
		ewarn("killing dead client: #{client_id}")
		remove_client(client_id)
	}
end

#reload_client_imagesObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/DrbDB.rb', line 174

def reload_client_images
	dead=Array.new
	main_server.connected_clients.each_pair{|client_id,cdb|
		if cdb[:db] == client.name
			begin
				einfo("sending reload images to #{client_id}")
				sdb[:object].rpc("DrbImages.instance.load_images")
			rescue => err
				ewarn("cannot send reload images to #{client_id}")
				dead.push(client_id)
			end
		end
	}
	dead.each{|client_id|
		ewarn("killing dead client: #{client_id}")
		remove_client(client_id)
	}
end

#reload_moditem(moditem_id) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/DrbDB.rb', line 125

def reload_moditem(moditem_id)
	@state=LOADING
	if moditem(moditem_id).nil?
		einfo("creating moditem ##{moditem_id}")
		#moditem added
		admin.rows("select moditems.id,modules.modname,moditems.display
		from moditems 
		left join modules on modules.id = moditems.modid
		where moditems.id='#{moditem_id}'").each{|moditem|
		begin
			case moditem["modname"]
				when "listing"
					@moditems[moditem["id"].to_i]=DrbListModel.new(self,moditem["id"].to_i).create_skeleton unless @moditems.has_key?(moditem["id"])
					@moditems[moditem["id"].to_i].update(self)
				when "form"
					@moditems[moditem["id"].to_i]=DrbForm.new(self,moditem["id"].to_i).create_skeleton unless @moditems.has_key?(moditem["id"])
				else
					eerror("not caching #{moditem['display']}[#{moditem['modname']}]")
			end
		rescue =>err
			ewarn("error loading moditem: #{moditem.inspect}")
			eexception(err,:moditem => moditem)
		end
		}
		
	else
		einfo("reloading moditem ##{moditem_id}")
		case moditem(moditem_id).mod_type
			when "list"
				new_model=DrbListModel.new(self,moditem_id).create_skeleton
				edebug("#{new_model} created")
				new_model.update(self)
				unless @moditems[moditem_id.to_i].nil?
					@moditems[moditem_id.to_i].clients.each_pair{|client_id,client| new_model.subscribe(client_id,client)}
					edebug("#{@moditems[moditem_id.to_i]} abandoned")
				end
				@moditems[moditem_id.to_i]=new_model
				@moditems[moditem_id.to_i].notify_clients(nil,"structure")
			when "form"
				moditem(moditem_id).create_skeleton
		end
	end
	@state=SERVING
end

#reload_usersObject



170
171
172
# File 'lib/DrbDB.rb', line 170

def reload_users
	@users.load_all
end

#remove_client(client_id) ⇒ Object



287
288
289
# File 'lib/DrbDB.rb', line 287

def remove_client(client_id)
	@moditems.each_value{|m| m.unsubscribe(client_id) if m.mod_type == "list"}
end

#remove_dead_clientsObject



281
282
283
284
285
# File 'lib/DrbDB.rb', line 281

def remove_dead_clients
	cnt=0
	@moditems.each_value{|m| cnt+=m.remove_dead_clients if m.mod_type == "list"}
	cnt
end

#report_mail(subject, variables) ⇒ Object

def method_missing(sym,*args) p “#self missing method: #sym(#args)” end



300
301
302
# File 'lib/DrbDB.rb', line 300

def report_mail(subject, variables)
	@main_server.report_mail(subject,variables)
end

#sendmail(nick, subject, body) ⇒ Object



239
240
241
# File 'lib/DrbDB.rb', line 239

def sendmail(nick,subject,body)
	sendmail_to_nick(nick,subject,body)
end

#serving?Boolean

Returns:

  • (Boolean)


309
310
311
# File 'lib/DrbDB.rb', line 309

def serving?
	@state==SERVING
end

#setup_clientObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/DrbDB.rb', line 90

def setup_client
	if @connection['client_uri'].length>0
		begin
			@client=DRb::DRbObject.new(nil,@connection['client_uri'])
			einfo("client is alive?(#{@client}): #{@client.alive?}")
			
=begin
			@moditems.each_value{|mi| 
				ecode(mi.class.name)
				mi.update if mi.class == DrbRelationBuilder
			}
=end
		rescue
			@client=nil
			einfo("no client.")
		end
		begin
			@relation_builder.update if @relation_builder && @client
		rescue => err
			eexception(err)
		end
	end
end

#to_sObject



313
314
315
# File 'lib/DrbDB.rb', line 313

def to_s
	"[ManqodDb #{name}(#{Process.pid})]"
end

#unlock_id_of_base(base, lid) ⇒ Object



270
271
272
273
# File 'lib/DrbDB.rb', line 270

def unlock_id_of_base(base,lid)
	ids=if lid.class == Array then lid else [lid] end
	moditems_with_base(base){|b| ids.each{|iid| b.unlock_iter(iid)}}
end

#user_in_group?(user_id, group_id) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/DrbDB.rb', line 242

def user_in_group?(user_id,group_id)
	@users.user_in_group?(user_id,group_id)
end