|
| 1 | +--- |
| 2 | +id: background_assembly |
| 3 | +title: | |
| 4 | + Assembling documents in the background |
| 5 | +sidebar_label: | |
| 6 | + Assembling documents in the background |
| 7 | +slug: background_assembly |
| 8 | +--- |
| 9 | + |
| 10 | +Assembly Line `3.2.0` and later introduce a background processing option to the [ALDocumentBundle](al_document.md) |
| 11 | +class. |
| 12 | + |
| 13 | +Background processing: |
| 14 | + |
| 15 | +* Is safer when your user may generate large documents that take more than 60 seconds to assemble or convert to PDF |
| 16 | +* Prevents frustrating 504 gateway timeout errors |
| 17 | + |
| 18 | +Negatives include: |
| 19 | + |
| 20 | +* Requires more care from the developer |
| 21 | +* Usually prevents you from using an "edit" button on the final screen |
| 22 | +* Means assembling documents will always take at least 10 seconds |
| 23 | + |
| 24 | +If your users currently have to wait a long time to get their finished documents, or if your users |
| 25 | +ever see an error screen because the downloads took too long to make, you should use background processing. |
| 26 | + |
| 27 | +If you have only a few small documents and there is no user-provided content (like photos or |
| 28 | +other uploaded exhibits), you should try without using background processing first. |
| 29 | + |
| 30 | +## Adding background processing to your interview |
| 31 | + |
| 32 | +Background processing is a built-in feature of the ALDocumentBundle class. Using it involves: |
| 33 | + |
| 34 | +1. Starting the built-in background assembly `task` in your interview order block |
| 35 | +1. Setting conditions on when to display a waiting screen |
| 36 | +1. Editing the download screen so it makes use of the background assembled documents |
| 37 | + |
| 38 | +In most cases, when you use background processing, you should also: |
| 39 | + |
| 40 | +1. Disable the preview screen |
| 41 | +1. Show a review screen in-line before the download screen. |
| 42 | + |
| 43 | +### Editing your interview order block |
| 44 | + |
| 45 | +Find the line in your interview order block where you would like to start the background |
| 46 | +assembly. In most cases, this should be just before the download screen. |
| 47 | + |
| 48 | +Add a line like this: |
| 49 | + |
| 50 | +```python |
| 51 | + if not al_user_bundle.generate_downloads_with_docx_task.ready(): |
| 52 | + al_download_waiting_screen |
| 53 | +``` |
| 54 | + |
| 55 | +:::warning Make sure all required variables are defined |
| 56 | + |
| 57 | +Background assembly requires that all of the logic required to assemble your documents |
| 58 | +was triggered **before** the background processing starts. You cannot have any |
| 59 | +undefined variables that are needed for the document to generate. |
| 60 | + |
| 61 | +Note that if you use `skip undefined` this isn't usually a problem. |
| 62 | +::: |
| 63 | + |
| 64 | +In some cases, you may want to start the background assembly earlier than just before the download screen. |
| 65 | +For example, you may want to show the user instructions, or get information for a task |
| 66 | +that will not change the documents, such as eFiling. |
| 67 | + |
| 68 | +### Editing the download screen |
| 69 | + |
| 70 | +On the download screen, edit the reference to `download_list_html()` to let it use |
| 71 | +the background assembled documents. Using cached files is disabled by default. |
| 72 | + |
| 73 | +```yaml |
| 74 | +--- |
| 75 | +id: download |
| 76 | +event: download_screen |
| 77 | +question: | |
| 78 | + Download the documents |
| 79 | +subquestion: | |
| 80 | + Your documents are ready for download. |
| 81 | +
|
| 82 | + If you need to make any changes, click the "Undo" button and click the button to edit |
| 83 | + the answer you want to change. |
| 84 | +
|
| 85 | + ${ al_user_bundle.download_list_html(use_previously_cached_files=True, include_full_pdf=True) } |
| 86 | +``` |
| 87 | +
|
| 88 | +### And don't forget |
| 89 | +
|
| 90 | +* Remove the preview screen. A waiting screen before the preview isn't a good user experience for most interviews. |
| 91 | +* Add the review screen to your interview order block. The user will not be able to click the "edit answers" button |
| 92 | + on the download screen. |
| 93 | +
|
| 94 | +### Customizing the user experience |
| 95 | +
|
| 96 | +#### What happens in the background task |
| 97 | +
|
| 98 | +You may want to provide a non-default set of options to the file generation step. |
| 99 | +
|
| 100 | +Override `al_user_bundle.generate_downloads_with_docx_task` or define a task with your |
| 101 | +own name. The default contents of this task are: |
| 102 | + |
| 103 | +```yaml |
| 104 | +--- |
| 105 | +generic object: ALDocumentBundle |
| 106 | +event: x.create_downloads_with_docx |
| 107 | +code: | |
| 108 | + download_response = x.get_cacheable_documents(key="final", pdf=True, docx=True, include_full_pdf=True) |
| 109 | + background_response_action(x.attr_name('save_downloads'), download_response=download_response) |
| 110 | +``` |
| 111 | + |
| 112 | +#### The waiting screen |
| 113 | + |
| 114 | +Override `al_download_waiting_screen`, or define a screen with your own name. The default contents |
| 115 | +of this question block are: |
| 116 | + |
| 117 | +``` |
| 118 | +--- |
| 119 | +id: waiting screen |
| 120 | +question: | |
| 121 | + Please wait while we make your documents |
| 122 | +subquestion: | |
| 123 | + This can take a few minutes. |
| 124 | + <div class="spinner-border text-primary d-flex justify-content-center" role="status"> |
| 125 | + <span class="sr-only">Making documents...</span> |
| 126 | + </div> |
| 127 | +event: al_download_waiting_screen |
| 128 | +reload: True |
| 129 | +``` |
| 130 | + |
| 131 | +The default screen uses [standard Bootstrap components](https://getbootstrap.com/docs/5.2/components/spinners/) |
| 132 | +to create the spinning circle. You can put anything you like here, although an animation is recommended. |
| 133 | + |
| 134 | +If you would like the screen to reload more often than every 10 seconds, [you can set it as low as 4 seconds](https://docassemble.org/docs/modifiers.html#reload). |
| 135 | + |
| 136 | +## Key methods and built-in task names |
| 137 | + |
| 138 | +`al_download_waiting_screen` is a Assembly Line provided example of a generic waiting screen. |
| 139 | + |
| 140 | +`generate_downloads_task` is the name of a [background task object](https://docassemble.org/docs/background.html#background_action) defined |
| 141 | +in the standard includes as an attribute of each ALDocumentBundle. It triggers a task to produce a full PDF of the bundle using the `final` |
| 142 | +key, a full ZIP, and it will not generate DOCX versions of the items, only downloadable (PDF) versions. |
| 143 | + |
| 144 | +`create_downloads` is an event defined in the Assembly Line standard includes as an attribute for each ALDocumentBundle. |
| 145 | +This event is triggered by `generate_downloads_task`. |
| 146 | + |
| 147 | +`generate_downloads_with_docx_task` is an alternative to `generate_downloads_task` that does produce the DOCX |
| 148 | +as well as PDF version of each document. You can safely use this even if you do not always want to display |
| 149 | +the DOCX on the download screen. |
| 150 | + |
| 151 | +In turn, `generate_downloads_with_docx_task` triggers the event `create_downloads_with_docx` |
| 152 | + |
| 153 | +[get_cacheable_documents()](al_document.md#get_cacheable_documents) is a new method that generates |
| 154 | +all **enabled** documents in the bundle and returns a list, where each item is a tuple of the DOCX and |
| 155 | +PDF versions of the document. |
| 156 | + |
| 157 | +Optionally, it will also produce a .ZIP and a .PDF version of the full bundle. |
| 158 | + |
| 159 | +This is called from a YAML block in the Assembly Line standard include files to define the `_downloadable_files` |
| 160 | +attribute of a bundle. |
| 161 | + |
| 162 | +[download_list_html()](al_document.md#download_list_html) has an optional parameter, |
| 163 | +`use_previously_cached_files` which must be set to `True` to make use of the `_downloadable_files` |
| 164 | +attribute of the bundle. |
| 165 | + |
| 166 | +## Troubleshooting |
| 167 | + |
| 168 | +If your background process has an error, the details of the error (typically a missing or undefined variable) |
| 169 | +will be found in the [`worker.log` file](https://docassemble.org/docs/admin.html#logs) |
0 commit comments