Here is an overview of my workflow:
- The
PdfFiles
table has 3 columns:id
,status
andfile
. - The form calls a server callable
start_pdf_generation(some_id)
and sets theinterval
of a timer to 1. - The server function
start_pdf_generation
starts the background taskpdf_generation
. - The
pdf_generation
background task adds a row to thePdfFiles
table with theid
and thestatus
, then it starts collecting info (it can take a minute ot two). While collecting info it updates thestatus
on that row. When all the info is ready, it uses it to render the PDF, then stores it to thefile
column and sets thestatus
to'done'
. - While that happens, the
tick
event of the timer keeps calling a server callableget_pdf_generation_status(some_id)
and updating aself.status_label.text
. - When the status is
'done'
, theget_pdf_generation_status
returns the PDF file for download and thetick
event sets theinterval
back to 0 to stop the polling.
I use a column for the status instead of the task status, because I was having some problems with checking the task status. I don’t remember the details, perhaps it was too slow? The disadvantage is that, if the status on the table never changes, you don’t know if it’s because the background task crashed or because it is genuinely taking a long time.
These are little implementation details, and you can play around with it. I hope you get the idea. On one end it’s more difficult than it should be. On the other hand you have all the freedom and control over what you can do.