How to define custom tasks in the Play Framework? (e.g. ruby ​​rake)

How to define arbitrary tasks in the playback platform?

I mean tasks run from the command line, something similar to ruby ​​rake.

I know the ant tool, but am looking for a better alternative.

+8
playframework rake
source share
3 answers

[edit] This answer is for the Play 1. series! *

You must write your own module, then your commands will go to the commands.py , ref file: http://www.playframework.org/documentation/1.2.4/releasenotes-1.1#commands

You can look at existing modules to get inspiration, for example: https://github.com/sim51/logisima-play-yml/blob/master/commands.py

Basically, you define the necessary commands and start them from the "execute" method, for example:

 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 run something java - often happens! -

 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 simple as:

 play new-module mymodule 

This is a primer: http://playframework.wordpress.com/2011/02/27/play-modules/ , given that the official Play! modular documentation is very limited in this regard.

change

I thought I would add some information:

Before you can execute your commands, you must CREATE your module. It does not work, like everyone else, with dynamic compilation.

 play build-module mymodule 

new-module / build-module expects the module to be at the root of the project folder, but if you have a lot, it will become a mess. build-module module-srcs/mymodule works fine.

+3
source share

For Play 2, you can create new tasks using SBT, following the documentation here:

http://www.scala-sbt.org/release/docs/Detailed-Topics/Tasks

In the context of the generated Build.scala playback 2 Build.scala it might look like this:

 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 ) } 
+6
source share

We use Play Jobs to perform such tasks.

 @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

Download job for tasks like 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."); } } 

}

Check out the docs for the game: http://www.playframework.org/documentation/1.2.4/jobs

0
source share

All Articles