parent
5f7873c29f
commit
a8b5d4dbbe
@ -0,0 +1,52 @@ |
||||
#!/usr/bin/env python |
||||
# -*- coding:utf-8 -*- |
||||
# project: 4月 |
||||
# author: liuyu |
||||
# date: 2020/4/7 |
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler |
||||
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job |
||||
|
||||
from api.utils.crontab.sync_cache import sync_download_times |
||||
|
||||
import atexit |
||||
import fcntl |
||||
|
||||
# 主要是为了防止多进程出现的多个定时任务同时执行 |
||||
f = open("scheduler.lock", "wb") |
||||
try: |
||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
||||
|
||||
# 开启定时工作 |
||||
try: |
||||
# 实例化调度器 |
||||
scheduler = BackgroundScheduler() |
||||
# 调度器使用DjangoJobStore() |
||||
scheduler.add_jobstore(DjangoJobStore(), "default") |
||||
|
||||
# 设置定时任务,选择方式为interval,时间间隔为10s |
||||
# 另一种方式为每天固定时间执行任务,对应代码为: |
||||
# @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='9', minute='30', second='10',id='task_time') |
||||
@register_job(scheduler, "interval", seconds=10) |
||||
def sync_download_times_job(): |
||||
# 这里写你要执行的任务 |
||||
sync_download_times() |
||||
|
||||
register_events(scheduler) |
||||
scheduler.start() |
||||
except Exception as e: |
||||
print(e) |
||||
# 有错误就停止定时器 |
||||
scheduler.shutdown() |
||||
|
||||
except: |
||||
pass |
||||
|
||||
def unlock(): |
||||
fcntl.flock(f, fcntl.LOCK_UN) |
||||
f.close() |
||||
|
||||
atexit.register(unlock) |
||||
|
||||
|
||||
|
@ -0,0 +1,20 @@ |
||||
#!/usr/bin/env python |
||||
# -*- coding:utf-8 -*- |
||||
# project: 4月 |
||||
# author: liuyu |
||||
# date: 2020/4/7 |
||||
|
||||
from api.models import Apps |
||||
from django.core.cache import cache |
||||
from fir_ser.settings import CACHE_KEY_TEMPLATE |
||||
|
||||
|
||||
def sync_download_times(): |
||||
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") |
||||
key = "%s%s" %(down_tem_key,'*') |
||||
for app_download in cache.iter_keys(key): |
||||
app_id = app_download.split(down_tem_key)[1] |
||||
Apps.objects.filter(app_id=app_id).update(count_hits=cache.get(app_download)) |
||||
|
||||
|
||||
|
@ -0,0 +1,47 @@ |
||||
#!/usr/bin/env python |
||||
# -*- coding:utf-8 -*- |
||||
# project: 4月 |
||||
# author: liuyu |
||||
# date: 2020/4/7 |
||||
|
||||
from django.core.cache import cache |
||||
from api.models import Apps,UserInfo |
||||
import time |
||||
from fir_ser.settings import CACHE_KEY_TEMPLATE |
||||
from api.utils.storage.storage import Storage,LocalStorage |
||||
from api.utils.crontab import run |
||||
|
||||
def get_download_url_by_cache(app_obj, filename, limit, isdownload=False): |
||||
now = time.time() |
||||
download_val = cache.get("%s_%s" % ('download_url', filename)) |
||||
if download_val: |
||||
if download_val.get("time") > now - 60: |
||||
return download_val.get("download_url") |
||||
else: |
||||
if isdownload: |
||||
local_storage = LocalStorage('localhost', False) |
||||
return local_storage.get_download_url(filename, limit, 'plist') |
||||
user_obj = UserInfo.objects.filter(pk=app_obj.get("user_id")).first() |
||||
storage = Storage(user_obj) |
||||
return storage.get_download_url(filename, limit) |
||||
|
||||
|
||||
def get_app_instance_by_cache(app_id, limit): |
||||
app_obj_cache = cache.get("%s_%s" % ('app_instance', app_id)) |
||||
if not app_obj_cache: |
||||
app_obj_cache = Apps.objects.filter(app_id=app_id).values("pk", 'user_id', 'type').first() |
||||
cache.set("%s_%s" % ('app_instance', app_id), app_obj_cache, limit) |
||||
return app_obj_cache |
||||
|
||||
|
||||
def get_app_download_by_cache(app_id): |
||||
down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") |
||||
key = "%s%s" %(down_tem_key,app_id) |
||||
download_times = cache.get(key) |
||||
if not download_times: |
||||
download_times=Apps.objects.filter(app_id=app_id).values("count_hits").first().get('count_hits') |
||||
cache.set(key, download_times + 1, 900) |
||||
else: |
||||
cache.incr(key) |
||||
return download_times + 1 |
||||
|
Loading…
Reference in new issue