[SOLVED] Getting Error frequently while loading a prosody plugin

local usermanager = require “core.usermanager”
if not usermanager.user_exists(user, host) then
usermanager.create_user(user, host, password);
end
module:log(“info”, “user has been created successfully”);

Sometime this runs success fully, and sometime i get the below error. I am not able to find any pattern. Other plugins works fine.

/usr/lib/prosody/core/storagemanager.lua:65: attempt to index local ‘host_session’ (a nil value)
stack traceback:
/usr/lib/prosody/core/storagemanager.lua:65: in function ‘initialize_host’
/prosody-plugins/mod_create_prosody_user.lua:6: in main chunk
[C]: in function ‘xpcall’
/usr/lib/prosody/core/modulemanager.lua:183: in function ‘do_load_module’
/usr/lib/prosody/core/modulemanager.lua:261: in function ‘load’
/usr/lib/prosody/core/modulemanager.lua:83: in function ‘?’
/usr/lib/prosody/util/events.lua:79: in function </usr/lib/prosody/util/events.lua:75>
(…tail calls…)
/usr/lib/prosody/core/hostmanager.lua:108: in function ‘activate’
/usr/lib/prosody/core/hostmanager.lua:58: in function ‘?’
/usr/lib/prosody/util/events.lua:79: in function </usr/lib/prosody/util/events.lua:75>
(…tail calls…)
/usr/lib/prosody/util/startup.lua:399: in function ‘prepare_to_start’
/usr/lib/prosody/util/startup.lua:635: in function ‘f’
/usr/lib/prosody/util/async.lua:139: in function ‘func’
/usr/lib/prosody/util/async.lua:127: in function </usr/lib/prosody/util/async.lua:125>

I was able to find out the issue, This happens when host is not initialised before the prosody plugin is called. Solved the above issue using prosody host initialised hook

if prosody.hosts[host] == nil then
prosody.events.add_handler(“host-activated”, add_user);
else
add_user(host)
end
func add_user(host) {
if host == expectedhost then
module:log(“info”, “use present successfully %s”, host);
if not usermanager.user_exists(user, host) then
usermanager.create_user(user, password, host);
module:log(“info”, “user has been created successfully”);
else
module:log(“info”, “user already present successfully”);
end
end
}