Download media after Background tasks ends

Here is an overview of my workflow:

  • The PdfFiles table has 3 columns: id, status and file.
  • The form calls a server callable start_pdf_generation(some_id) and sets the interval of a timer to 1.
  • The server function start_pdf_generation starts the background task pdf_generation.
  • The pdf_generation background task adds a row to the PdfFiles table with the id and the status, then it starts collecting info (it can take a minute ot two). While collecting info it updates the status on that row. When all the info is ready, it uses it to render the PDF, then stores it to the file column and sets the status to 'done'.
  • While that happens, the tick event of the timer keeps calling a server callable get_pdf_generation_status(some_id) and updating a self.status_label.text.
  • When the status is 'done', the get_pdf_generation_status returns the PDF file for download and the tick event sets the interval 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.

1 Like