クーロン

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

クーロン

web2pyのクーロンは、アプリケーションが予め設定された時刻に、プラットフォームに依存しない形で、タスクを実行することができるように提供されたものです。

The web2py cron provides the ability for applications to execute tasks at preset times, in a platform independent manner.

各アプリケーションでは、クーロンの機能は"app/cron/crontab"にあるクーロンタブファイルによって定義されます。その構文は45の参照に従っています(web2py独自の拡張がいくつかあります)。

For each application, cron functionality is defined by a crontab file "app/cron/crontab", following the syntax defined in ref. 45 (with some extensions that was web2py specific).

このことは、すべてのアプリケーショが個別にクーロンの設定を持つことができ、クーロンの設定がホストOSに左右されることなくweb2pyから変更できることを意味します。

This means that every application can have a separate cron configuration and that cron config can be changed from within web2py without affecting the host OS itself.

次の例を見てください:

Here is an example:

1.

2.

3.

4.

5.

0-59/1 * * * * root python /path/to/python/script.py

30 3 * * * root *applications/admin/cron/db_vacuum.py

*/30 * * * * root **applications/admin/cron/something.py

@reboot root *mycontroller/myfunction

@hourly root *applications/admin/cron/expire_sessions.py

この例の最後の2行は、web2pyの追加機能を提供するために通常のクーロン構文を拡張したものを利用しています。

The last two lines in this example, use extensions to regular cron syntax to provide additional web2py functionality.

web2pyのクーロンはweb2pyアプリケーション固有の事情をサポートするいくつかの拡張構文を持っています。

web2py cron has a some extra syntax to support web2py application specifics.

もしスクリプト/関数の名前がアスタリスク(*)で始まり、".py"で終わる場合、それはweb2pyの環境で実行されます。これは、すべてのコントローラとモデルを自由に使えることを意味します。2つのアスタリスク(**)を使用した場合、モデルは実行されません。これは、オーバーヘッドが少なく、潜在的なロックの問題を回避する際に、推奨される呼び出し方法です。

If the task/script is prefixed with an asterisk (*) and ends with ".py", it will be executed in the web2py environment. This means you will have all the controllers and models at your disposal. If you use two asterisks (**), the MODELs will not be executed. This is the recommended way of calling as it has less overhead and avoids potential locking problems.

ただし、web2py環境で実行されるスクリプト/関数は、手動のdb.commit()が関数の最後に必要となります。そうしないとトランザクションが戻されます。

Notice that scripts/functions executed in the web2py environment require a manualdb.commit() at the end of the function or the transaction will be reverted.

web2pyは(クーロンが動作する)シェルモードではチケットや意味のあるトレースバックを生成しません。web2pyコードがエラーなしで動作することを、クーロンタスクとしてセットする前に確認してください。

web2py does not generate tickets or meaningful tracebacks in shell mode (in which cron is run). Make sure that your web2py code runs without errors before you set it up as a cron task, as you will likely not be able to see them when run from cron.

さらに、どのようにモデルを使用しているかに注意してください。タスクの実行は別プロセスで行われますが、データベースのロックは考慮する必要があります。データベースをブロックするクーロンタスクに対して待機するページを避けるためです。クーロンタスクでデータベースを使わない場合は、** 構文を使用してください。

Moreover, be careful how you use models. While the execution happens in a separate process, database locks have to be taken into account in order to avoid pages waiting for cron tasks that may be blocking the database. Use the ** syntax if you don't need to use the database in your cron task.

また、コントローラの関数を呼び出すことができます。パスを指定する必要はありません。コントローラと関数は、呼び出し側のアプリケーションのものです。上に列挙した注意事項に特に注意を払ってください。たとえば:

You can also call a controller function. There is no need to specify a path. The controller and function will be that of the invoking application. Take special care about the caveats listed above. Example:

1.

*/30 * * * * root *mycontroller/myfunction

このクーロンタブの最初のフィールドで@rebootと明記すると、所定のタスクはweb2py起動時に一度だけ実行されます。この特徴を利用して、web2py起動時にアプリケーションのデータを、事前にキャッシュしたり、検証したり、初期化したりすることができます。なお、クーロンタスクはアプリケーションに並行して実行されます。したがって、もしアプリケーションがクーロンタスクが終わるまでリクエストを受け取る準備ができない場合、タスクが反映されたかチェックするような実装をするべきです。例は次の通りです:

If you specify @reboot in the first field in the crontab file, the given task will be executed only once, on web2py startup. You can use this feature if you want to pre-cache, check or initialize data for an application on web2py startup. Note that cron tasks are executed in parallel with the application --- if the application is not ready to serve requests until the cron task is finished, you should implement checks to reflect this. Example:

1.

@reboot * * * * root *mycontroller/myfunction

どのようにweb2pyを起動しているかに応じて、4つの動作モードがweb2pyのクーロンに対してあります。

Depending on how you are invoking web2py, there are four modes of operation for web2py cron.

  • ソフトクーロン:すべての実行モードで利用可能

  • Soft cron: available under all execution modes

  • ハードクーロン:組み込みのサーバを使用している場合に利用可能(直接もしくはApacheのmod_proxyを介して)

  • Hard cron: available if using the built-in web server (either directly or via Apache mod_proxy)

  • 外部クーロン:システム自身のクーロンサービスにアクセス出来る場合に利用可能

  • External cron: available if you have access to the system's own cron service

  • クーロンなし

  • No cron

組み込みのサーバを利用している場合、デフォルトはハードクーロンです。他のすべての場合は、デフォルトでソフトクーロンです。

The default is hard cron if you are using the built-in web server; in all other cases the default is soft cron.

ソ フトクーロンはCGI、FASTCGI、WSGIを使用してい場合、デフォルトになります。あなたのタスクは、クーロンタブで指定した時刻の後にくる web2pyへの最初の呼び出し(ページロード)で実行されます(ただし、ユーザーに遅延を発生させないためページの処理が終わった後に行われます)。明らかに、いつタスクが実行されるのかの正確な時刻は、サイトが受け取るトラフィックに依存し、不確実性があります。また、クーロンタスクは、Webサーバーがページ読み込みのタイムアウトを設定している場合、中断されるおそれがあります。これらの制限が許容できない場合は、"外部クーロン"を参照してください。ソフトクーロンは合理的な最後の手段ですが、あなたのWebサーバーが他のクーロン手法が利用出来る場合には、ソフトクーロンよりそちらを利用したほうが良いです。

Soft cron is the default if you are using CGI, FASTCGI or WSGI. Your tasks will be executed in the first call (page load) to web2py after the time specified in crontab (but after processing the page, so no delay to the user is visible). Obviously, there is some uncertainty exactly when the task will be executed depending on the traffic the site receives. Also, the cron task may get interrupted if the web server has a page load timeout set. If these limitations are not acceptable, see "external cron". Soft cron is a reasonable last resort, but if your web server allows other cron methods, they should be preferred over soft cron.

ハードクーロンは、(直接またはApacheのmod_proxyを介して)組み込みのWebサーバーを使用している場合、デフォルトになります。ハードクーロンは並列スレッドによって実行されるので、ソフトクーロンと異なり、実行時間や実行する正確な時刻に関して制約はありません。

Hard cron is the default if you are using the built-in web server (either directly or via Apache mod_proxy). Hard cron is executed in a parallel thread, so unlike soft cron there are no limitations with regard to run time or execution time precision.

外部クーロンは、どの場合でもデフォルトではありませんが、システムクーロンの設備にアクセスできる必要があります。これは並列プロセスで動作するため、ソフトクーロンで適用されるような制限はありません。これはWSGIやFastCGIの下でクーロンを利用する場合に推奨される方法です。

External cron is not default in any scenario, but requires you to have access to the system cron facilities. It runs in a parallel process, so none of the limitations of soft cron apply. This is the recommended way of using cron under WSGI or FASTCGI.

次のサンプル行は、システムクーロン(通常は /etc/crontab)に挿入されるものです:

Example of line to add to the system crontab, (usually /etc/crontab):

1.

0-59/1 * * * * web2py cd /var/www/web2py/ && python web2py.py -C -D 1 >> /tmp/cron.output 2>&1

外部クーロンで動すならば、-Nコマンドラインパラメータをweb2pyの起動スクリプトまたは設定に追加していることを確認してください。複数のタイプのクーロンの衝突を回避するためです。

If you are running external cron, make sure you add the -N command line parameter to your web2py startup script or config so there is no collision of multiple types of cron.

特定のプロセスでどんなクーロン機能も必要ない場合は、-Nコマンドラインパラメータを用いてそれを無効にすることができます。ただしこれは、いくつかのメンテナンスタスク(自動的なセッションディレクトリのクリーンなど)も無効にするかもしれないことに注意してください。この関数の最も一般的な用途は以下の通りです:

In cases where you do not need any cron functionality within a particular process, you can use the -N command line parameter to disable it. Note that this might disable some maintenance tasks (like the automatic cleaning of session dirs). The most common use of this function:

  • すでにシステムから起動される外部クーロンを開始している場合(WSGIの設定で最も一般的)

  • You already have set up external cron triggered from the system (most common with WSGI setups)

  • アプリケーションをデバッグする際、アクションや出力をクーロンに干渉されたくない場合

  • You want to debug your application without cron interfering either with actions or with output