In part 3 we explored adding object store capabilities with an intuitive file system interface.
In part 4 we are going to look at adding scheduled tasks to our application.
Decorating Scheduled Tasks
With pebl scheduled tasks are created with a python decorator placed on the function that you want to run at a regular interval:
import pebl
@pebl.cron("task", "@hourly")
def task():
import time
print(" -- task running at:", time.time())
The first argument to the decorator is a unique identifier provided by you. Pebl ensures that there's only one scheduled task ever registered for each identifier, meaning that updates & redeploys behave as expected.
The second argument is the interval, and currently only two values are supported, @hourly
and @daily
.
Simple Example
Let's incorporate a scheduled task into our Flask application! And while doing so let's also utilize the object store. We will create a simple daily task that snapshots the current count.
import pebl
import redis
from flask import Flask
conn = redis.Redis(**pebl.redis("redis-1"))
app = Flask(__name__)
@app.route("/")
def root():
return "Hello!"
@app.route("/counter", methods=["POST"])
def increment():
conn.incr("counter")
return "success!"
@app.route("/counter", methods=["GET"])
def get_count():
curr = conn.get("counter")
return {
"counter": int(curr) if curr else 0
}
@pebl.cron("snapshot", "@hourly")
def counter_snapshot():
path = f"/snapshots/{int(time.time())}"
curr = conn.get("counter")
with open(path, "w") as f:
f.write(curr.encode())
pebl.service(app, "your-subdomain.pebl.rocks")
Give this a try by running it with pebl run
!
Conclusion
And with that we have covered four key capabilities provided by the pebl SDK, and also how to run and deploy pebl workloads.
Top comments (0)