[英]How to define arbitrary tasks in the Play Framework? (like ruby rake)
How to define arbitrary tasks in the Play Framework? 如何在Play框架中定义任意任务?
I mean tasks run from the command line, something similar to ruby rake. 我的意思是任务从命令行运行,类似于ruby rake。
I'm aware of the ant tool but looking for a better alternative. 我知道蚂蚁工具,但正在寻找更好的选择。
For Play 2, you can create new tasks using SBT, by following the documentation here: 对于Play 2,您可以按照以下文档使用SBT创建新任务:
http://www.scala-sbt.org/release/docs/Detailed-Topics/Tasks http://www.scala-sbt.org/release/docs/Detailed-Topics/任务
In the context of a Play 2 generated Build.scala
, it might look like this: 在Play 2生成的Build.scala
,它可能看起来像这样:
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "foo"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
anorm
)
val hello = TaskKey[Unit]("hello", "Prints 'Hello World'")
val helloTask = hello := {
println("Hello World")
}
lazy val main = play.Project(appName, appVersion, appDependencies).settings(
helloTask
)
}
[edit] This answer is for the Play 1.* series! [编辑] 这个答案适用于Play 1. *系列!
You should write a custom module, then your commands go into the commands.py
file, ref: http://www.playframework.org/documentation/1.2.4/releasenotes-1.1#commands 您应该编写一个自定义模块,然后将命令放入commands.py
文件中,参考: http : //www.playframework.org/documentation/1.2.4/releasenotes-1.1#commands
You can look at existing modules to get inspired, eg: https://github.com/sim51/logisima-play-yml/blob/master/commands.py 您可以查看现有模块以获取启发,例如: https : //github.com/sim51/logisima-play-yml/blob/master/commands.py
Basically you define the commands you want and launch them from the "execute" method, eg: 基本上,您定义所需的命令,然后从“ execute”方法启动它们,例如:
COMMANDS = ['namespace:command']
def execute(**kargs):
command = kargs.get("command")
app = kargs.get("app")
args = kargs.get("args")
env = kargs.get("env")
if command == "namespace:command":
do_something()
if you want to launch something java - often the case! 如果您想启动Java,通常是这样! -: -:
def do_something():
java_cmd = app.java_cmd([], None, "play.modules.mymodule.MyClass", args)
try:
subprocess.call(java_cmd, env=os.environ)
except OSError:
print "Could not execute the java executable, please make sure the JAVA_HOME environment variable is set properly (the java executable should reside at JAVA_HOME/bin/java). "
sys.exit(-1)
print
Ps. 附言
creating a custom module is as easy as: 创建自定义模块很容易:
play new-module mymodule
This is a primer: http://playframework.wordpress.com/2011/02/27/play-modules/ , considering that official Play! 这是入门书籍: http : //playframework.wordpress.com/2011/02/27/play-modules/ ,考虑到该官方Play! module documentation is quite limited in that respect 在这方面模块文档非常有限
edit 编辑
I thought I'd add a little piece of information: 我以为我会补充一点信息:
before being able to execute your commands, you must BUILD your module. 在能够执行命令之前,必须先构建模块。 It does not run like the rest of play with a dynamic compilation. 它与动态编译的其余部分不同。
play build-module mymodule
new-module/build-module expect the module to be at the root of the project folder, but if you have many that becomes a mess. new-module / build-module期望模块位于项目文件夹的根目录,但是如果您有很多模块,那将是一团糟。 build-module module-srcs/mymodule
works perfectly fine. build-module module-srcs/mymodule
可以正常工作。
We are using Play Jobs for that kind of tasks. 我们正在使用Play Jobs执行此类任务。
@Every("1h")
public class WelcomeUser extends Job {
public void doJob() {
List<User> newUsers = User.find("newAccount = true").fetch();
for(User user : newUsers) {
Notifier.sayWelcome(user);
}
}
}
or 要么
Bootstrap job for tasks like db_migration : 诸如db_migration之类的任务的引导作业:
@OnApplicationStart
public class Bootstrap extends Job {
public void doJob() {
if(Page.count() == 0) {
new Page("root").save();
Logger.info("The page tree was empty. A root page has been created.");
}
}
} }
Have a look play documents : http://www.playframework.org/documentation/1.2.4/jobs 看一下播放文件: http : //www.playframework.org/documentation/1.2.4/jobs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.