1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Popcorn threaded tasks
19 # Tasks allow you to execute code in another thread than the app listening loop.
20 # Useful when you want to run some tasks periodically.
22 # Let's say you want to purge the `downloads/` directory of your app every hour:
40 # # Register a new task
41 # app.register_task(new PurgeTask("downloads/"))
44 # # app.use('/', new MyHandler)
50 # app.listen("0.0.0.0", 3000)
57 # An abstract Popcorn task
59 # Redefine the `main` method to do something
61 # TODO provide a CRON-like syntax like SpringBoot?
62 abstract class PopTask
65 redef fun main
do return null
71 var tasks
= new Array[PopTask]
73 # Register a new task in `self`
74 fun register_task
(task
: PopTask) do tasks
.add task
76 # Run all registered tasks
77 fun run_tasks
do for task
in tasks
do task
.start