globald.lua 3.06 KB
local skynet = require "skynet"
local harbor = require "skynet.harbor"
local json = require("shared.json")
local redisproxy = require("shared.redisproxy")

require "shared.init"
require "utils.init"
require "RedisKeys"
require "skynet.manager"

local ipairs = ipairs
local table_insert = table.insert
local tarr2tab = table.array2Table
local string_format = string.format

local pointDataMark = {}
local utils = {}

local CHECK_MAIL_STATUS_INTERVAL 	= 60
local function mailQuene()
	local delayEmail = tonum(redisproxy:hget("autoincrement_set", "delay_email"))
	if delayEmail == 0 then
		return
	end
	local begin = math.max(delayEmail - 100, 1)
	local mails = redisproxy:pipelining(function (red)
		for id = begin, delayEmail do
			red:hgetall(string.format("delayEmail:%s", id))
		end
	end)
	if not mails then
		return
	end
	local now = skynet.timex()
	local mailList = {}
	for index, data in ipairs(mails) do
		if next(data) then
			local email = tarr2tab(data)
			if tonum(email.startTime) <= now then
				table_insert(mailList, email)

				if #mailList > 100 then
					break
				end
			end
		end
	end
	if #mailList == 0 then
		return
	end
	redisproxy:pipelining(function (red)
		for _, email in ipairs(mailList) do
			red:del(string_format("delayEmail:%s", email.id))
		end
	end)
	table.sort(mailList, function(a, b)
		return tonum(a.id) < tonum(b.id)
	end)
	for _, email in ipairs(mailList) do
		local gid = redisproxy:hincrby("autoincrement_set", "email", 1)
		if email.mid then
			redisproxy:hmset(string_format("globalEmail:%s", gid), 
				"id", gid,
				"createtime", email.endTime,
				"title", email.title,
				"stitle", email.stitle,
				"content", email.content,
				"attachments", email.attachments,
				"endtime", email.endTime,
				"mid", email.mid,
				"timestamp", email.startTime
			)
		else
			redisproxy:hmset(string_format("globalEmail:%s", gid), 
				"id", gid,
				"createtime", email.endTime,
				"stitle", email.stitle,
				"content", email.content,
				"attachments", email.attachments,
				"endtime", email.endTime,
				"timestamp", email.startTime
			)
		end
	end
	redisproxy:hset("autoincrement_set", "emailTimestamp", now)
end

-- @desc: 定时邮件队列检测
local function check_mail_queue()
	pcall(mailQuene)
	skynet.timeout(CHECK_MAIL_STATUS_INTERVAL, check_mail_queue)
end



local CMD = {}

local cacheWorldMsg = {}
local CACHE_WORLD_MSG_COUNT = 50
function CMD.sendWorldMsg(channel, msg)
	cacheWorldMsg[channel] = cacheWorldMsg[channel] or {}
	table.insert(cacheWorldMsg[channel], msg)
	for i = #cacheWorldMsg[channel] - CACHE_WORLD_MSG_COUNT, 1, -1 do
		table.remove(cacheWorldMsg[channel], i)
	end
end


function CMD.getWorldMsg(channel)
	local msgs = cacheWorldMsg[channel] or {}
	return msgs
end

function CMD.start()
	check_mail_queue()
end

local function __init__()
	skynet.dispatch("lua", function(_, _, command, ...)
		local f = CMD[command]
		if f then
			if command == "sendWorldMsg" then
				skynet.ignoreret()
				f(...)
			else
				skynet.ret(skynet.pack(f(...)))
			end
		end
	end)
	redisd = harbor.queryname("REDIS")
	skynet.register("GLOBALD")
end

skynet.start(__init__)