分類
网站

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 是一款製作精良的塔防遊戲,它「浪費」了我很多時間!

分類
Linux

Fedora 35 安裝 VirtualBox

沒想到給 Fedora 35 安裝 VirtualBox 會遇到這麼一個坑,所以記錄下。如果你的電腦沒有啓用 EFI Secure Boot 應該不會遇到這個問題。報錯信息如下:

vboxdrv.sh: failed: modprobe vboxdrv failed. Please use 'dmesg' to find out why.

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.  If your system is using EFI Secure Boot you may need to sign the
kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load
them. Please see your Linux system's documentation for more information.

如果你不想關閉安全啓動,那麼可以按如下步驟來解決:

1, 安裝 mokutil

sudo dnf update
sudo dnf install mokutil

2, 在新文件夾裏創建 RSA 密鑰

sudo -i
mkdir /root/signed-modules
cd /root/signed-modules
openssl req -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -nodes -days 36500 -subj "/CN=VirtualBox/"
chmod 600 MOK.priv

3, 準備導入密鑰的密碼,等下重啓電腦後會詢問你設置的密碼。

sudo mokutil --import MOK.der

4, 重啓電腦,電腦會進入一個藍屏,選擇 Enroll MOK --> Continue --> 輸入你剛剛設置的密碼,然後你的電腦會重啓。

5, 進入剛剛的文件夾新建一個腳本。

cd /root/signed-modules
nano sign-virtual-box

腳本內容如下:

#!/bin/bash

for modfile in $(dirname $(modinfo -n vboxdrv))/*.ko; do
  echo "Signing $modfile"
  /usr/src/kernels/$(uname -r)/scripts/sign-file sha256 \
                                /root/signed-modules/MOK.priv \
                                /root/signed-modules/MOK.der "$modfile"
done

如果腳本執行失敗了,可以嘗試運行下面命令,然後根據結果修改上面腳本。

find /usr/src -name signfile

5, 爲腳本增加執行權限並運行。

chmod 700 sign-virtual-box
./sign-virtual-box 

6, 把簽名後的驅動加入內核中就可以了。

modprobe vboxdrv

Ubuntu 用戶可以參考這篇 https://stegard.net/2016/10/virtualbox-secure-boot-ubuntu-fail/。以上解決辦法由 Younes LAB 作答與 Sign virtual box modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) Centos 8 - Stack Overflow


從 VirtualBox RPM 源安裝 VirtualBox

由於之前沒有搜到上面的辦法,還以爲是安裝的問題,嘗試了從 VirtualBox RPM 源安裝 VirtualBox,其實是不能解決問題的,但是這裏也記錄一下。

1, 安裝依賴。

sudo dnf -y install @development-tools
sudo dnf -y install kernel-headers kernel-devel dkms elfutils-libelf-devel qt5-qtx11extras

2, 添加VirtualBox RPM 源。

Fedora 36 / Fedora 35:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/35/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

Fedora 34:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/34/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

Fedora 33:

cat <<EOF | sudo tee /etc/yum.repos.d/virtualbox.repo 
[virtualbox]
name=Fedora $releasever - $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/fedora/33/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
EOF

3, 導入 VirtualBox GPG 密鑰,確認指紋匹配後按 y。

sudo dnf search virtualbox
Fedora  -  - VirtualBox                                 58  B/s | 181  B     00:03
Fedora  -  - VirtualBox                                906  B/s | 1.7 kB     00:01
Importing GPG key 0x98AB5139:
 Userid     : "Oracle Corporation (VirtualBox archive signing key) <[email protected]>"
 Fingerprint: 7B0F AB3A 13B9 0743 5925 D9C9 5442 2A4B 98AB 5139
 From       : https://www.virtualbox.org/download/oracle_vbox.asc
Is this ok [y/N]: y

4, 安裝 VirtualBox。

sudo dnf install VirtualBox-6.1

5, 把當前用戶添加到 vboxusers 用戶組。

sudo usermod -a -G vboxusers $USER
newgrp vboxusers

然後就可以通過命令 virtualbox 或應用菜單 》System 》Oracle VM VirtualBox來啓動了。但是這個時候還沒有裝 Extension Pack,表現爲虛擬機只支持 USB 1.1,屏幕無法縮放。好在擴展包直接從官網下載就可以,然後直接雙擊就能順利運行安裝。

此安裝教程來自 How To Install VirtualBox 6 on Fedora 36/35/34/33/32 - ComputingForGeeks