タスクチェーンのフレームワーク
App Engineでは実行時間に制限があるため、長時間にわたる処理を行う場合は複数のタスクに分割する必要があります。この手法は一般にタスクチェーンと呼ばれています(たぶん)。
タスクチェーンを抽象化してフレームワークにできるか考えてみました。
タスクチェーンを実現するには、タスク間のデータの受け渡し、タスクの一意性保証を実装する必要があります。
Driver
タスクチェーンを開始するためのサーブレットです。Driverを経由せず、Webブラウザやcronからタスクチェーンを開始することも可能です。
Task
タスクチェーンにおけるタスクを抽象化したものです。
Taskは入力として Map
public interface Task { /** * Process of the task. * @param input data passed from last task * @param context the task context * @return output data that will be passed to next task (return null if last) */ public abstract Map<String, String> run(Map<String, String> input, Context context); }
TaskChainController
タスクチェーンをSlim3のコントローラで定義するための仕組みです。
例えば、こんな感じ。
public class AddBooksController extends TaskChainController { @Override protected Configuration configure() { Configuration configuration = new Configuration(); return configuration; } @Override public Map<String, String> run(Map<String, String> input, Context context) { for(Map.Entry<String, String> e : input.entrySet()) { System.err.println(e.getKey()+"\t"+e.getValue()); } return null; } }
コンセプトの実装はこちらにあります。
https://github.com/int128/elshard/tree/master/src/main/org/hidetake/elshard/task/
実はデモサイトを準備してたりしますが、まだ動きません(汗
http://elshard-demo.appspot.com/
考え方や名前の付け方は象の影響を強く受けています^^ ただ、Datastoreの特徴を生かすにはAppEngineに特化して考えた方がいい気がしています。大量のデータを処理する最適な方法をこれから考えていきます。