スケジュールタスクを知らなかった

  • 投稿日:
  • by
  • カテゴリ:

昨年末のこと、大晦日は嫁の実家に行っているので、「年が改まったら、自動的に記事が投稿されるように設定しておこう」と、公開日時の指定を行ないました。
元日の朝に確認してみたら、下書きのままで公開されていませんでした。 「なんだよ~」とぶーたれつつ手動で公開したのですが、よく使う機能ではないので原因は調べずそのままになっていました。

そもそもダイナミックパブリッシングは使わず、スタティックな構成でMTを使っています。 プロセスが常駐している訳でもないのに、なんで時間が来たら公開されるのか不思議でした。

同じことは古いコメント欄を閉じる Blog Janitor プラグインでも言えます。 
MTCloseCommentsプラグインは、メインページ内にタグを書いておくことで、再構築のタイミングで実行されます。
対する Blog Janitor は、自動で実行されるらしいのですが、なんで自動で動くのか理解できませんでした。

Blog Janitor - Close Comments and Delete Duplicates - MT Hacks

Blog Janitor does his work automatically, you don't need to edit your templates. Just tell Blog Janitor the tasks you want him to perform (by adjusting the plugin settings), and he will go to work periodically (using the scheduled tasks feature of MT).

MTが持っている「scheduled tasks」という機能で自動的に実行されるらしいのですが、プラグインを導入して設定してみても、動いた形跡がないんですよね。 ログを見ても何も書かれていませんし...


モヤモヤしつつもほったらかしにしていたんですが、ググってみたらすぐ見つかりました。

指定日投稿や公開キュー等のスケジュール処理の設定

Movable Type では、指定日投稿や迷惑コメント/トラックバックの削除、公開キュー等のスケジュールタスクを拡張しており、次のさまざまな方法でスケジュールタスクを実行できます。
 
* cron や Windows タスク・スケジューラを利用した run-periodic-tasks スクリプトの実行
* ログ・フィードの取得時に実行
* XML-RPC API で拡張された mt.runPeriodicTasks の利用

やはりcronで動かす必要があるみたいです。 そりゃそうだよな。

で、cronに tools/run-periodic-tasks を登録してみたのですが、実行してみると

Can't locate MT/TheSchwartz.pm in @INC (@INC contains: ./lib ../lib extlib ../extlib /usr/local/lib/webmin /usr/local/lib/perl5/5.8.9/BSDPAN /usr/local/lib/perl5/site_perl/5.8.9/mach /usr/local/lib/perl5/site_perl/5.8.9 /usr/local/lib/perl5/5.8.9/mach /usr/local/lib/perl5/5.8.9 .) at /usr/local/www/apache22/cgi-bin/MT5/tools/run-periodic-tasks line 32.

と表示されます。

TheSchwartz.pm は、ちゃんと lib/MT に入っているのですが、なんで見つからないんでしょうね?
ちなみに、コンソールから tools/run-periodic-tasks を実行すると、ちゃんと動くようです。


さらにググってみると、同じ問題を抱えた方がいました。

Can't Find TheSchwartz - Movable Type Forums

You create a file call mt-rpt.pl, place it in your root MT folder with mt.cgi, chmod mt-rpt.pl to 755, and have the cron script call mt-rpt.pl. The contents of mt-rpt.pl are this:
 
#!/usr/bin/perl -w
use strict;
chdir '/...file path to your site folders.../cgi-bin/mt';
my @args = ('/usr/bin/perl', '-w', './tools/run-periodic-tasks');
system(@args) == 0 or die "system @args failed: $?";
 
The problem was that on my clients cloud server calling directly to the script was not do-able but calling to this tiny script was.

この mt-rpt.pl の chdir の部分のパスを自分の環境に合わせて変えて、MTのディレクトリに配置して cron で実行したら、ちゃんと動いてくれました。

ブログの公開プロファイル

デーモンモードでの run-periodic-tasks の実行
 
cron を使用しなくても、run-periodic-tasks をデーモンモードで実行すると、約5分おきに処理が実行されます。
 
run-periodic-tasks をデーモンモードで実行するには、以下のコマンドを実行します。
 
$ ./tools/run-periodic-tasks -d &

コマンドラインでちゃんと動くなら、デーモンモードで動かすというのも選択肢の一つですね。