Skip to content

Commit d3f0d13

Browse files
committed
add an example for background tasks
1 parent 6f7e3fc commit d3f0d13

File tree

10 files changed

+106
-0
lines changed

10 files changed

+106
-0
lines changed

examples/bg-task/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data/
2+
pubsub/
3+
uploads/

examples/bg-task/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM ubuntu
2+
3+
ENV DEV false
4+
5+
RUN apt-get update && apt-get install ucspi-tcp
6+
7+
EXPOSE 3000
8+
9+
COPY . /app
10+
11+
CMD [ "/app/start.sh" ]

examples/bg-task/config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PROJECT_NAME="Background Task"

examples/bg-task/core.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../core.sh

examples/bg-task/pages/bg.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# helper function to map each
3+
# line of input to an SSE event
4+
event_stream() {
5+
local line
6+
while IFS= read -r line; do
7+
event "$1" "$line"
8+
done
9+
}
10+
11+
# start a long running task in the background,
12+
# and pipe its output to the SSE topic 'stuff'
13+
#
14+
# important things to note:
15+
# 1. we close stdin at the start with '</dev/null'
16+
# 2. we close stdout and stderr at the end of the pipe
17+
18+
</dev/null ./task.sh \
19+
| event_stream update \
20+
| publish stuff &>/dev/null &
21+
22+
# this happens immediately; we don't need to wait
23+
# for the above task to finish to get this response.
24+
echo 'Job started!'

examples/bg-task/pages/index.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
source config.sh
3+
4+
htmx_page << EOF
5+
<h1>${PROJECT_NAME}</h1>
6+
<button hx-post="/bg" hx-target="#target">Do Stuff</button>
7+
<div id="target">Not Started</div>
8+
<h2>Events</h2>
9+
<output hx-ext="sse" sse-connect="/sse" sse-swap="update" hx-swap="beforeend">
10+
EOF

examples/bg-task/pages/sse.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# sse
2+
3+
topic() {
4+
echo stuff
5+
}

examples/bg-task/start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../start.sh

examples/bg-task/static/style.css

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
body {
2+
padding: 0 40px;
3+
}
4+
#signature {
5+
position: absolute;
6+
z-index: 100;
7+
right: 20px;
8+
top: 20px;
9+
display: flex;
10+
align-items: center;
11+
justify-content: center;
12+
}
13+
14+
#signature a {
15+
text-decoration: none;
16+
}
17+
18+
#signature a:hover {
19+
text-decoration: underline;
20+
}
21+
22+
#signature svg {
23+
width: 36px;
24+
height: 36px;
25+
margin-top: -4px;
26+
vertical-align: middle;
27+
}
28+
29+
#form {
30+
display: flex;
31+
max-width: 400px;
32+
flex-direction: column;
33+
}
34+
35+
#count {
36+
font-size: 24px;
37+
}

examples/bg-task/task.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# simulate a binary doing background stuff
4+
sleep 1
5+
echo "hello"
6+
sleep 1
7+
echo "world"
8+
sleep 1
9+
echo "things"
10+
sleep 1
11+
echo "happened"
12+
sleep 1
13+
echo "goodbye"

0 commit comments

Comments
 (0)