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:
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)
FileServer
action, which is a standard and minimal file server
HttpRequest
class and services to create it
Serializable::inspect
to show more useful information
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# 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
lib/popcorn/pop_tasks.nit:17,1--78,3