lib/popcorn: introduce popcorn tasks
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 12 May 2017 00:44:19 +0000 (20:44 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 16 May 2017 16:38:19 +0000 (12:38 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/popcorn/pop_tasks.nit [new file with mode: 0644]
lib/popcorn/popcorn.nit

diff --git a/lib/popcorn/pop_tasks.nit b/lib/popcorn/pop_tasks.nit
new file mode 100644 (file)
index 0000000..a0e1aa1
--- /dev/null
@@ -0,0 +1,78 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Popcorn threaded tasks
+#
+# Tasks allow you to execute code in another thread than the app listening loop.
+# Useful when you want to run some tasks periodically.
+#
+# Let's say you want to purge the `downloads/` directory of your app every hour:
+#
+# ~~~nitish
+# class PurgeTask
+#      super PopTask
+#
+#      var dir: String
+#
+#      redef fun main do
+#              loop
+#                      dir.rmdir
+#                      3600.sleep
+#              end
+#      end
+# end
+#
+# var app = new App
+#
+# # Register a new task
+# app.register_task(new PurgeTask("downloads/"))
+#
+# # Add your handlers
+# # app.use('/', new MyHandler)
+#
+# # Run the tasks
+# app.run_tasks
+#
+# # Start the app
+# app.listen("0.0.0.0", 3000)
+# ~~~
+module pop_tasks
+
+import pop_handlers
+import pthreads
+
+# An abstract Popcorn task
+#
+# Redefine the `main` method to do something
+#
+# TODO provide a CRON-like syntax like SpringBoot?
+abstract class PopTask
+       super Thread
+
+       redef fun main do return null
+end
+
+redef class App
+
+       # Tasks to run
+       var tasks = new Array[PopTask]
+
+       # Register a new task in `self`
+       fun register_task(task: PopTask) do tasks.add task
+
+       # Run all registered tasks
+       fun run_tasks do for task in tasks do task.start
+end
index 190d312..0ae1017 100644 (file)
@@ -18,6 +18,7 @@
 module popcorn
 
 import nitcorn
+import pop_tasks
 import pop_sessions
 import pop_logging
 intrude import pop_handlers