要如何保護GAE 的排程工作

說穿了GAE 的排程只是用一台機器固定去開指定的網址
去驅動該網址對應的controller 裡面寫的python script
也就是說, 假設我寫了一個controller 叫做 "/worker/"
那麼其實只要輸入該url , 誰都可以去驅動controller 做事

但一般來說, 我們列入排程的工作不會是希望一般人都可以去驅動的
通常都是涉及資料庫整理之類的東西, 隨便亂執行的話
可能會造成一筆不小的開銷, 更糟的是可能會造成資料庫的錯亂
(不在預期的時間裡面執行整理)
GAE 想到了這個問題, 以下的方式就可以保護之:

A cron handler is just a normal handler defined in app.yaml. You can prevent users from accessing URLs used by scheduled tasks by restricting access to administrator accounts. Scheduled tasks can access admin-only URLs. You can restrict a URL by adding login: admin to the handler configuration in app.yaml.



application: hello-cronversion: 1runtime: pythonapi_version: 1
handlers:- url: /report/weekly
  script: reports.py
  login: admin

留言

熱門文章