|
| 1 | +# Shut down |
| 2 | + |
| 3 | +Provides a way for users to gracefully shut down Glean, |
| 4 | +by blocking until it is finished performing pending tasks, |
| 5 | +such as recording metrics and uploading pings. |
| 6 | + |
| 7 | +{{#include ../../../shared/blockquote-info.html}} |
| 8 | + |
| 9 | +## How the Glean SDKs execute tasks |
| 10 | + |
| 11 | +> Most calls to Glean APIs are dispatched[^1]. This strategy is adopted, because most tasks |
| 12 | +> performed by the Glean SDKs involve file system read or write operations, HTTP requests and |
| 13 | +> other time consuming actions. |
| 14 | +> |
| 15 | +> Each Glean SDK has an internal structure called "Dispatcher" which makes sure API calls |
| 16 | +> get executed in the order they were called, while not requiring the caller to block on the |
| 17 | +> completion of each of these tasks. |
| 18 | +> |
| 19 | +> [^1]: Here this term indicates the tasks are run asynchronously in JavaScript or in a different |
| 20 | +> thread for all other SDKs. |
| 21 | +
|
| 22 | +## API |
| 23 | + |
| 24 | +### `shutdown` |
| 25 | + |
| 26 | +{{#include ../../../shared/tab_header.md}} |
| 27 | +<div data-lang="Kotlin" class="tab"></div> |
| 28 | +<div data-lang="Java" class="tab"></div> |
| 29 | +<div data-lang="Swift" class="tab"></div> |
| 30 | +<div data-lang="Python" class="tab"></div> |
| 31 | +<div data-lang="Rust" class="tab"></div> |
| 32 | +<div data-lang="JavaScript" class="tab"> |
| 33 | + |
| 34 | +```js |
| 35 | +import Glean from "@mozilla/glean/webext"; |
| 36 | + |
| 37 | +async function onUninstall() { |
| 38 | + // Flips Glean upload status to `false`, |
| 39 | + // which triggers sending of a `deletion-request` ping. |
| 40 | + Glean.setUploadEnabled(false); |
| 41 | + |
| 42 | + // Block on shut down to guarantee all pending pings |
| 43 | + // (including the `deletion-request` sent above) |
| 44 | + // are sent before the extension is uninstalled. |
| 45 | + await Glean.shutdown(); |
| 46 | + |
| 47 | + // Uninstall browser extension without asking for user approval before doing so. |
| 48 | + await browser.management.uninstallSelf({ showConfirmDialog: false }); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The `shutdown` API is available for all JavaScript targets, even though the above example |
| 53 | +is explicitly using the `webext` target. |
| 54 | +</div> |
| 55 | +<div data-lang="Firefox Desktop" class="tab"></div> |
| 56 | +{{#include ../../../shared/tab_footer.md}} |
| 57 | + |
| 58 | +## Reference |
| 59 | + |
| 60 | +* [JavaScript API docs](https://mozilla.github.io/glean.js/classes/core_glean.default.html#shutdown) |
0 commit comments