分類
网站

Ten plugins and ten themes for WordPress

Here is a collection of plugins and themes from Chapter 16: The Part of Tens of the book WordPress for dummies by Lisa Sabin-Wilson.

Plugins

Custom Post Type UI

For custom post types and taxonomy.

Jetpack

A suit of plugins provided by wordpress.com.

Limit Login Attempts Reloaded

Limit Login Attempts.

Cookie Notice for GDPR & CCPA

Cookie Notice for GDPR & CCPA

Yoast SEO

Search engine optimization

BackupBuddy

For backing up and transfering website. Price strats from $80.

WP Super Cache

Create static HTML files from your dynamic WordPress content.

WooCommerce

Selling product or service on your website.

Google XML Sitemap

Create a Google-compliant XML site map of your entire site and submit it to major search engines.

Sucuri Security

Scan for malware, spam, blacklisting and other security issues hidden inside your code files.

Themes

Hybrid Core

Highly customizable parent theme. It's a WordPress Framework now.

Hestia

One-page theme built for use on a small-business website.

Responsive

Nine templates built on a fluid grid system as well as webmaster toos, logo management and multilingual support.

Ashe

Suit for personal website, lifestyle blog, bakery, travel agency.Support WooCommerce.

Prefer Blog

Simple blog with built-in Auther and Contact Us block.

BlackBird

Use your own logo, analytics code, customize featured text with an easy-to-use widget, post thumbnails, header image.

Storefront

For store with WooCommerce support, custom widgets and sidebars.

Sinatra

Good for general websites like restaurants, bakeries tech startups and blog.

Nisarg

A nice clean blog theme.

Optics

A minimalist theme featuring a grid layout. Clean and Simple.

分類
网站

在線 cron 定時請求

有時我們需要定時執行一些網絡任務,但是所使用的服務又不支持 crontab 定時任務,這時可以考慮使用在線的 cron-job.org 來觸發,它可以根據你的需要在在指定的時間或按照特定頻率執行網絡請求。是一個免費又開源的服務。

如果你的任務對於執行時間沒有那麼精確,也可以使用之前推薦過的 StatusCake ,它的 Push Test 也是非常好用的。如果是要監測網站或服務器的在線情況,則使用它的 Uptime test。

這兩個服務的免費版本個人使用應該都夠用,如果它們有幫到你,請量力考慮贊助它們。

分類
网站

使用 Django Q 方便地執行耗時任務

Django Q 是一個使用 Python 多進程製作的原生 Django 任务队列、调度器和 worker 应用。它具有很多優異特性但我只是粗淺使用了如下幾點:

  • 多進程 worker 池
  • 異步任務
  • 定時任務、cron 和重複的任務
  • 把失敗和成功結果保存到數據庫或緩存
  • 自動集成到 Django Admin,就可以在後臺添加和管理任務
  • 支持 Redis, Disque, IronMQ, SQS, MongoDB 或 ORM 這麼多種隊列代理方式,最方便的當然是 ORM
  • 注意:Django Q 的任務間隔粒度是 30 秒,如果你的任務頻率或精準度要求高於 30 秒,你需要嘗試修改源碼或使用其他任務隊列

安裝 Django Q

pip install django-q
#如果要使用 cron 規則,則也要安裝
pip install croniter
#在項目的 settings.py 文件 INSTALLED_APPS 裏加入 django_q
INSTALLED_APPS = (
    # other apps
    'django_q',
)
#設置代理方式,我選擇 ORM,
#只需把下面字段也加入 settings.py

#更多設置請參見 https://django-q.readthedocs.io/en/latest/configure.html
Q_CLUSTER = {
    'name': 'djangtasks',
    'workers': 2,
    'timeout': 180,
    'retry': 200,
    'queue_limit': 50,
    'bulk': 10,
    'orm': 'default'
}

#執行數據庫遷移來創建數據庫表
python manage.py migrate
#運行 Django Q 來處理任務隊列
python manage.py qcluster

我是使用 screen 來在後臺運行 qcluster,很方便的。

使用 Django Q 在 Django 後臺執行耗時任務

# file: views.py
import datetime
from django.http import HttpResponse
from django.utils import timezone
from django_q.tasks import async_task, schedule
from django_q.models import Schedule

def a_longtime_task(arg):
    time.sleep(30)
    return arg

def scheduled_task(arg):
    time.sleep(30)
    return arg

def a_longtime_task_request(request):
    #立即在後臺執行
    async_task(a_longtime_task,'args for the function')
    return HttpResponse('The longtime task has been started.')

def another_longtime_task_request(request):
    #三分鐘後執行
    schedule('YOURAPP.views.scheduled_task',
            'args for the function',
            schedule_type=Schedule.ONCE,
            next_run=timezone.now() + datetime.timedelta(minutes=3))
    return HttpResponse('The task has been scheduled.')

在 Django Admin 佈置定時任務

這個就比較直接,比如要每天 10 點都執行上面例子中的 scheduled_task,就點擊 Admin 頁面 Scheduled tasks 旁邊的「新增」。

Func(填入完整的函數路徑):YOURAPP.views.scheduled_task
Args:'args for the function'
Schedule Type:Daily
Next Run(設置要運行的時間如):2022-08-27 10:00

最後點擊「儲存」就可以了。

同樣的,如果選擇 Cron 類型,就需要在 Cron 輸入框填入 Cron 計劃。比如在 9 點到 23 點期間,每 5,15,25,35,45,55 各執行一次:
5,15,25,35,45,55 9-23 * * *
更多 Cron 用法和組合可以參考 crontab.guru 這個網站。

等待執行的任務、失敗的任務和成功的任務都可以方便的在Admin頁面查看和操作,非常方便。

管理隊列任務與已完成任務 Schedule Task

# file: tasks.py
from django_q.models import Schedule, Task
from django.db.models import Q
#your model to be checked
from app1.models import Race
from django.utils import timezone

import datetime
import operator
from functools import reduce

# Task to delete successful old tasks
def delete_old_tasks():
    len_task_20 = 0
    len_task_gen = 0
    now = datetime.datetime.now()
    days_passed_2 = timezone.utc.localize(now - datetime.timedelta(days=2))
    days_passed_7 = timezone.utc.localize(now - datetime.timedelta(days=7))
    task_q_list = []
    task_q_list.append(Q(group__startswith='20'))
    task_q_list.append(Q(started__lt=days_passed_7))
    task_q_list.append(Q(success__exact=True))
    task_20_queryset = Task.objects.filter(reduce(operator.and_, task_q_list))
    len_task_20 = len(task_20_queryset)
    for task in task_20_queryset:
        task.delete()

    task_q_list = []
    task_q_list.append(Q(group__exact='generate_0_task'))
    task_q_list.append(Q(started__lt=days_passed_2))
    task_q_list.append(Q(success__exact=True))
    task_gen_queryset = Task.objects.filter(reduce(operator.and_, task_q_list))
    len_task_gen = len(task_gen_queryset)
    for task in task_gen_queryset:
        task.delete()

    return f'{len_task_20} checked tasks and {len_task_gen} checking tasks have been removed.'

#generate 0 task every 5 minuts
def generate_0_task():
    res=[]
    now = datetime.datetime.now()
    dto_now = timezone.utc.localize(now)
    start_datetime_6 = now + datetime.timedelta(minutes=6)
    dto_plus6 = timezone.utc.localize(start_datetime_6)
    current_races = Race.objects.filter(post_time_live__lte=dto_plus6,
                                          post_time_live__gte=dto_now)
    if len(current_races) == 0:
        res.append('no current races')
        return res
    for race in current_races:
        if race.race_conditions.find('SIMULCAST')>-1:
            res.append(str(race)+': task passed SIMULCAST')
            continue
        tasks = Schedule.objects.filter(name=str(race))
        if len(tasks)==0:
            schedule('app1.views.zero_mtp_task',
                    race.id, race.track_id, race.race_number, race.race_date, race.post_time_live.isoformat(),
                    schedule_type=Schedule.ONCE,
                    next_run=race.post_time_live,
                    name=str(race))
            res.append(str(race)+': task added'+' post time:'+str(race.post_time_live))
        else:
            res.append(str(race)+': task already added'+' post time:'+str(race.post_time_live))
    return res

本文更新於 2023/01/27。

分類
网站

Django with PostgreSQL

參考 PostgreSQL:Linux downloads (Red Hat family) 來安裝PostgreSQL,比如 Fedora 35 可以這樣:

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/F-35-x86_64/pgdg-fedora-repo-latest.noarch.rpm
sudo dnf install -y postgresql13-server libpq-devel
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13

安裝完成後來初始化一個數據庫和用戶。

#使用 root 登錄 PostgreSQL
sudo -u postgres psql
#創建數據庫
CREATE DATABASE project;
#創建新用戶
CREATE USER user42 WITH PASSWORD 'password42';
#下面這三行優化是 Django 推薦的
ALTER ROLE user42 SET client_encoding TO 'utf8';
ALTER ROLE user42 SET default_transaction_isolation TO 'read committed';
ALTER ROLE user42 SET timezone TO 'Asia/Taipei';
#把數據庫授權給新用戶
GRANT ALL PRIVILEGES ON DATABASE project TO user42;
#退出數據庫
\q

要在 Django 中使用 PostgreSQL 數據庫,需要還安裝 psycopg2:

pip install Django psycopg2
#如果像我一樣在 Fedora 安裝失敗了
#可以嘗試安裝編譯好的版本
pip install Django psycopg2-binary

之後在項目的設置中把默認的 SQLite 數據庫資料改成 PostgreSQL 就可以了。更像詳細的步驟與解釋,可以參考:How To Use PostgreSQL with your Django Application on Ubuntu 20.04

刪除 Django 數據庫中的表格並重建

這似乎不是正確的回滾數據庫的操作辦法,但是也可以一試。

#從 migrations 目錄找到要刪除的數據庫變更文件,刪除掉
#進入數據庫操作程序
python manage.py dbshell
#查看並找到要刪除的表名
SELECT * FROM pg_catalog.pg_tables;
#如果是MariaDB,這樣查看表名
show tables;
#比如表名爲 task_day,則這樣刪除
DROP TABLE task_day;
#退出數據庫操作程序
exit;
#刪掉數據庫裏的 migrations
python manage.py migrate --prune task

#現在可以重新建立表格了
python manage.py makemigrations
python manage.py migrate

本文更新於 2022/10/24。

分類
Linux 软件

Fedora 35 與 Nvidia GPU 驅動

TL;NR

如果電腦開啓了 Secure Boot, 又不想頭痛地處理簽名,可以關掉。然後跟隨這個網頁的方法安裝開源的 Nvidia 驅動: Howto/NVIDIA。或者跟隨這個網頁安裝 Nvidia 官方的驅動:Fedora 36/35/34 NVIDIA [515.57 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers Install Guide。這個網頁裏也有介紹如果禁用 Fedora 自帶的 Nouveau 驅動的方法。

The Story

打算嘗試在 Fedora 上編輯視頻,一番搜索後發現 DaVinci Resolve 好像很強大,而且還有 Linux 的免費版本。下載安裝很順利,但是打開後歡迎頁面是黑屏。禁用 Secure Boot 裝了 rpmfusion 推薦的驅動後可以打開了,但是 GPU 設置成 CUDA 模式提示無法渲染圖像,錯誤編碼 999,改成 OpenGL 模式不報錯,但是連 JPG 圖像的預覽都是花的,我以爲是 rpmfusion 的驅動不行(實際不是)。於是去裝官網驅動,禁用了 Nouveau 驅動後安裝成功,但是 DaVinci 還是無法使用,後來發現可能是筆記本自動選擇顯卡 Optimus 有關,可以參考 OptimusHow to Set Nvidia as Primary GPU on Optimus-based Laptops。我並沒有把 Nvidia 設置爲默認,因爲覺得平常板載就夠了。所以後來我使用環境變量的方法成功運行了 DaVinci Resolve

__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia /opt/resolve/bin/resolve

最後我發現相機拍攝的 mov 視頻文件導入後還是只有音頻,想要編輯的話要先對視頻進行轉碼。而且導出格式也有蠻多限制,所以就放棄了 DaVinci Resolve。

然後我搜索到 Linux 平臺另一款「強大」的視頻編輯器 Lightworks,到官網下載免費版體驗,發現需要註冊,註冊時發現有 recaptcha 人機驗證,我換了好幾個網絡居然都無法通過。無奈從網上找了個安裝包,安裝後打開軟件發現需要登錄才能使用所以還是要註冊。最後是第二天在手機瀏覽器裏註冊成功了。然後發現導出 1080p 的視頻居然是付費版本才有的功能,2022年了喂~

最後我去看 Fedora 的手冊,上面推薦 Kdenlive,從官網直接下載 AppImage 檔直接運行,視頻和圖片都順利導入,導出也都沒有什麼限制,h264、h265、ProRes 全都 OK!而且也支持視頻穩定和關鍵幀調整。雖然兩個視頻漸變過度的 Mixes 操作很怪異,但是至少能達到想要的效果。時間軸的操作可能由於還不會用,所有也感覺不太順手,但是至少可以正常導入、剪輯、調整顏色、加字幕和出片了。

分類
软件

Anuto TD 一個塔防遊戲

Anuto TD 是一款製作精良的塔防遊戲,它「浪費」了我很多時間!

分類
說說

220518

Emanon 今天跟我講。上週在市場買菜的時候,在一家沒去過幾次的菜攤買完菜後,順嘴問了一句,你家有青皮蕉嗎?對方說,下次有。結果今天去買菜,買完後店家問她,要不要看看我家的青皮蕉。這個事情讓她想起當年在上海,她和朋友在路上遇到一個擺路邊攤賣碟的,她朋友就隨口問了一個碟,那人表示現在沒有。後來過來一段時間,無意經過那裏的時候,那位攤主很開心叫住她說,你上次說的那張碟我給你找來咯!Emanon 和她朋友都很驚奇,因爲只是隨口這麼一問,根本沒想到對方會認真對待,並且記住了朋友的模樣。不由的會想,這事要是在東北,即使你去一家正規商店跟對方拜託好,說的很決絕,一定給你找來,可能轉身就會忘。因爲當年東北的市場不是自由市場,處處洋溢着權力氣息——售貨員沒有義務,潛在的顧客也沒有權力,來促成這筆交易。社會上的大家表面上親如兄弟,這是我的誰誰誰,實際上可能都沒把對方放在眼裏。僅有的一點信任,是斷然不能給予陌生人的。話說的有點重,但這就是當年印象。