#8 worker added

This commit is contained in:
Tangent 2019-06-28 19:37:35 -07:00
parent 1fda7ef7fe
commit 624bd9c2f6
3 changed files with 36 additions and 1 deletions

View File

@ -68,7 +68,7 @@ class extends lapis.Application
if tag_pattern != tag_pattern\lower! if tag_pattern != tag_pattern\lower!
return redirect_to: @url_for "tag", name: escape(tag_pattern\lower!), order: @order, asc_desc: @asc_desc, page: @page return redirect_to: @url_for "tag", name: escape(tag_pattern\lower!), order: @order, asc_desc: @asc_desc, page: @page
tag_pattern = escape_literal tag_pattern tag_pattern = escape_literal tag_pattern
tag_pattern = "'% " .. tag_pattern\sub(2, -2) .. " %'" tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
tracks = Tracks\paginated "WHERE tags LIKE #{tag_pattern} ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32 tracks = Tracks\paginated "WHERE tags LIKE #{tag_pattern} ORDER BY #{escape_identifier @order} #{@asc_desc}", per_page: 32
@last_page = 1 @last_page = 1

View File

@ -11,6 +11,11 @@ http {
charset UTF-8; charset UTF-8;
include mime.types; include mime.types;
lua_shared_dict worker 5k;
init_worker_by_lua '
require("worker")
';
server { server {
listen ${{PORT}}; listen ${{PORT}};
lua_code_cache ${{CODE_CACHE}}; lua_code_cache ${{CODE_CACHE}};

30
worker.moon Normal file
View File

@ -0,0 +1,30 @@
lock = require "resty.lock"
import Tags, Tracks from require "models"
import escape_literal from require "lapis.db"
add_job = (interval, name, fn, init) ->
run = (premature, arg) ->
if premature return
thread_lock = lock\new "worker". { timeout: 0, exptime: interval }
if thread_lock\lock "t#{name}"
time_lock = lock\new "worker", { exptime: interval }
if time_lock\lock "t#{name}"
arg = fn(arg)
thread_lock\unlock!
ngx.timer.at(interval, run, arg)
ngx.timer.at(0, run, init)
update_tag_counts = (id=1) ->
if tag = Tags\find(:id)
tag_pattern = escape_literal tag.name
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
count = Tracks\count "tags LIKE #{tag_pattern}"
tag\update(:count)
next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
if next_tag = next_tag[1]
return next_tag.id
add_job 60, "tags", update_tag_counts