12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- --书籍任务
- local M = {}
- local mysqldbx = require "mysqldbx"
- local tools = require "tools"
- local skynet = require "skynet"
- local cjson = require "cjson"
- local config = require "run_config"
- local mysql = require "skynet.db.mysql"
- local db
- local mysqldtaskbx = {}
- function M.task()
- -- skynet.error("执行一次task")
- local sql = string.format("SELECT * FROM video_product where status = 10 AND list_time is not null ")
- local res = mysqldtaskbx.Singleton().query(sql)
- -- 获取当前服务器时间戳
- local current_timestamp = os.time()
- if #res >0 then
- -- tools.dump(res)
- for i = 1, #res, 1 do
- local mysql_time_str = res[i].list_time
- local id = res[i].id
- -- 将MySQL时间字符串转换为时间戳
- local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)"
- local year, month, day, hour, min, sec = mysql_time_str:match(pattern)
- local mysql_timestamp = os.time({
- year = year,
- month = month,
- day = day,
- hour = hour,
- min = min,
- sec = sec
- })
-
- if mysql_timestamp < current_timestamp then
- -- skynet.error("mysql_timestamp:",mysql_timestamp)
- -- skynet.error("current_timestamp:",current_timestamp)
- sql = string.format("UPDATE video_product SET status = 1 WHERE id = %d ",id)
- -- skynet.error("sql:",sql)
- -- tools.dump(res[i])
- mysqldtaskbx.Singleton().query(sql)
- else
- --还没到时间
- end
- end
- end
- skynet.timeout(500, M.task)
- end
- function mysqldtaskbx.start()
- local function on_connect(db)
- db:query("set charset utf8mb4");
- end
- local conf = config.db_cnf.book_server.mysqldb_task_cnf
- db = mysql.connect{
- host=conf.ip,
- port=conf.port,
- database=conf.db,
- user=conf.user,
- password=conf.password,
- charset="utf8mb4",
- max_packet_size = 1024 * 1024,
- on_connect = on_connect
- }
- if not db then
- skynet.error("mysql connect fail")
- end
- end
- function mysqldtaskbx.Singleton()
- if db == nil then
- mysqldtaskbx.start()
- end
- return mysqldtaskbx
- end
- function mysqldtaskbx.query(sql)
- return db:query(sql)
- end
- skynet.start(function () --skynet.start启动一个timeout来执行function,创建了一个协程
- skynet.timeout(500, M.task) --由于function函数还没用完协程,所有这个timeout又创建了一个协程
- end)
- return M
|