Skip to content

Commit

Permalink
Chore/further emulator cleanup 3 (#3711)
Browse files Browse the repository at this point in the history
chore: improve emulator documentation
  • Loading branch information
pizzaisdavid committed Jun 29, 2024
1 parent bb29ad0 commit 5495122
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
12 changes: 6 additions & 6 deletions functions/emulator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#
# Optionally, initial data can be setup by mounting `/seed`.
#
# If you want so not get errors due to email missing templates,
# you also need to mount those. The functions code while in the
# emulator expects them to be in `/templates`.
# If you want to not get errors due to missing email templates,
# you also need to mount those. The functions code in the
# emulator expects them to be located at `/templates`.
#
# COMMANDS
# We need some files from the root directory of the project,
# therefore these commands should be ran from the root directory.
# therefore these commands should be ran from there.
#
# BUILD
# docker build -f ./functions/emulator/Dockerfile -t emulator .
Expand Down Expand Up @@ -98,7 +98,7 @@ RUN mkdir /seed
# These should be the ports specified in firebase.json
EXPOSE 4001 4002 4003 4004 4005 4006 4007 4008

# Used to tell the code we are in the emulator
# Used to tell the functions code we are in the emulator
ENV IS_EMULATED=true

CMD \
Expand All @@ -107,4 +107,4 @@ CMD \
firebase emulators:start \
--project demo-community-platform-emulated \
--only auth,functions,firestore,pubsub,storage,hosting,database \
--import=/seed
--import=/seed
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Firebase Emulator

To run backend functions locally, Firebase provides a suite of emulators to mimic most functionality seen online (e.g firestore, storage, functions, triggers etc.)

For simplicity, although each service is an individual emulator and we are running multiple services, we will refer to them all a emulator.
For simplicity, although each service is an individual emulator and we are running multiple services, we will refer to them all collectively as a single emulator.

# Getting started

Expand Down Expand Up @@ -50,7 +50,7 @@ This starts the Firebase emulator, loads code into it, and watches for changes.

### Stopping the backend

Due to some technical limitations, the `CTRL+C` keyboard shortcut may not stop the emulator, so it may be necessary to run:
Due to technical limitations of concurrently, the `CTRL+C` keyboard shortcut does not reach the emulator, so it will probably be necessary to explicitly run:

```
yarn run backend:emulator:stop
Expand All @@ -62,7 +62,7 @@ It is assumed that all of these commands will be ran from the root directory of

## Visiting the frontend

The frontend should start at [localhost:4000](http://localhost:4000). You should see a small banner at the bottom of the page that informs emulators are in use.
The frontend should start at [localhost:4000](http://localhost:4000). You should see a small banner at the bottom of the page that informs the emulator is in use.

![](./images/emulator-docker-frontend.png)

Expand All @@ -76,9 +76,9 @@ Clicking on tabs will take you to a page similar to the Firebase console, from w

## Seed data

The emulator loads hardcoded data and any changes are only stored in temporary memory. No changes are kept between sessions.
Hardcoded data is loaded into the emulator on start-up, any changes will be lost the next time the emulator is started.

You may experience some strange data issues but that is probably due to the browser's caching mechanisms. You can verify that by using another browser; in that case the original browser's indexeddb cache would need to be manually cleared.
You may experience some strange data issues, for example incorrectly getting error messages that a user already exists after restarting, but that is probably due to browser caching. You can verify that by using another browser; in that case the original browser's indexeddb cache would need to be manually cleared.

### User logins

Expand All @@ -102,42 +102,43 @@ password: thanks_emulator_man

### Improving it

You can improve the seed data by making changes via the application or Firebase UI, exporting it, and making a pull request. This will help make development/testing easier for you and others in the future.
You can improve the seed data by making changes via the application or Firebase user interface, exporting the data, and making a pull request. This will help make development and testing easier for you and others in the future.

1. Get the container name using `docker ps`.

2. Run the export script:

```
docker exec -it <name> /app/export.js
docker exec -it <container_name> /app/export.js
```

3. Transfer the data from the container to your machine:

```
docker cp <name>:/app/dump ./functions/data/
docker cp <container_name>:/app/dump ./functions/data/
```

4. Delete the current `emulator` folder.

5. Rename the `dump` folder to `emulator`.

6. But note, each folder in the export must be checked into Git. If not, this will cause problems with seeding. By default, Git does not track empty folders, so you must force it to track it by adding a .gitkeep file to the folder.
6. But note, each folder in the export must be checked into Git. If not, this will cause problems when the emulator tries to start. By default, Git does not track empty folders, so you must force it to track it by adding a .gitkeep file to the folder.

## Function development

### Writing functions code

The emulator binds to the `functions/dist` folder so that changes made will be reflected in the emulator. On Linux these changes should be picked up immediately. On Windows the changes are not always detected and may require spinning the emulator down and then starting back up.
The emulator binds to the `./functions/dist` folder so that changes made will be reflected in the emulator. On Linux these changes should be picked up immediately. On Windows the changes are not always detected and may require spinning the emulator down and then starting back up.

### Invoking functions

Functions can be invoked in different ways depending on their trigger type.

For functions triggered by storage or database changes, making changes directly on the dashboard or from the frontend should trigger the corresponding function.
Similarly callable functions should be called triggered from frontend code.

For functions triggered by http request you can call directly either from command line, a web browser or REST client such as [Insomnia](https://insomnia.rest/)
Similarly callable functions should be triggered from the frontend.

For functions triggered by HTTP request, you can call them directly either from command line, web browser or REST client such as [Insomnia](https://insomnia.rest/)

E.g. calling the emulator `seed-users-create` function via a GET request:

Expand All @@ -153,9 +154,9 @@ Read more about [connecting your app to the Cloud Functions Emulator](https://fi

If the emulator throws an error you may want to check generated debug.log files. These will exist in the container in the root `/app` folder.

By default, when using the commands above, the log files will sync to `./functions/logs` folder.
By default, when using the commands above, the log files will sync to the `./functions/logs` folder on the host machine.

But you can access the file system within the docker container directly using the
You can access the file system within the docker container directly using the
[Remote-Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension for vscode, and running the command to `attach to running container`.

![](images/emulator-docker-remote.png)
Expand All @@ -166,12 +167,12 @@ Once running in the container you can open the `/app` folder to view files
Alternatively while the container is running, you can request Docker to execute commands directly on the container:

```
docker exec -it <name> ls
docker exec -it <name> cat /app/firestore-debug.log
docker exec -it <container_name> ls
docker exec -it <container_name> cat /app/firestore-debug.log
```

(The `name` can be retrieved by running `docker ps`.)
(The `container_name` can be retrieved by running `docker ps`.)

## Troubleshooting

See the `/functions/emulator/Dockerfile` for some debugging tips.
See the `./functions/emulator/Dockerfile` for some debugging tips.

0 comments on commit 5495122

Please sign in to comment.