123456789101112131415161718192021222324252627282930313233 |
- lock = require "resty.lock"
-
- import Tags, Tracks from require "models"
- import escape_literal from require "lapis.db"
-
- next_id = (id=0) ->
- next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
- unless next_tag
- next_tag = Tags\select "WHERE id > '0' ORDER BY id ASC LIMIT 1"
- if next_tag
- return next_tag[1].id
-
- update_tag_counts = (premature, tag_id) ->
- if premature return
-
- thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
- if thread_lock\lock "update_tag_counts.thread"
- time_lock = lock\new "worker", { exptime: 2 }
- if time_lock\lock "update_tag_counts.time"
-
- if tag_id
- if tag = Tags\find id: tag_id
- tag_pattern = escape_literal tag.name
- tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
- count = Tracks\count "tags LIKE #{tag_pattern}"
- tag\update(:count)
-
- tag_id = next_id(tag_id) or tag_id
- thread_lock\unlock!
-
- ngx.timer.at(2, update_tag_counts, tag_id)
-
- ngx.timer.at(0, update_tag_counts)
|