Skip to content

Fix issue where publishTime is always 1970-01-01T00:00:00#7454

Merged
aalej merged 2 commits intomasterfrom
aalej-pubsub-publishtime
Jul 16, 2024
Merged

Fix issue where publishTime is always 1970-01-01T00:00:00#7454
aalej merged 2 commits intomasterfrom
aalej-pubsub-publishtime

Conversation

@aalej
Copy link
Copy Markdown
Contributor

@aalej aalej commented Jul 16, 2024

Description

Fixes #7441

Scenarios Tested

Using:

functions/index.js

import { onRequest } from "firebase-functions/v2/https";
import { onMessagePublished } from "firebase-functions/v2/pubsub";
import { PubSub } from '@google-cloud/pubsub'

export const example = onMessagePublished(
    {
        topic: 'example',
        timeoutSeconds: 540,
        memory: '2GiB',
    },
    (event) => {
        console.log(`Publish time: ${event.data.message.publishTime}`);
    },
);

export const publishMessage = onRequest(async (req, res) => {
    const pubSubClient = new PubSub()

    const [messageId] = await pubSubClient.topic('example').publishMessage({
        data: Buffer.from(JSON.stringify({ generateRun: true })),
    })

    res.send(`Message ID ${messageId}`)
})

Steps:

  1. Run firebase emulators:start --project demo-project
  2. Open a new terminal and run curl http://127.0.0.1:5001/demo-project/us-central1/publishMessage, or open "http://127.0.0.1:5001/demo-project/us-central1/publishMessage" in a browser
    3 Outputs:
i  emulators: Starting emulators: functions, pubsub
i  emulators: Detected demo project ID "demo-project", emulated services will use a demo configuration and attempts to access non-emulated services for this project will fail.
i  pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/Users/<user>/Desktop/firebase-tools/issues/7441/functions" for Cloud Functions...
⚠  functions: Your requested "node" version "18" doesn't match your global version "20". Using node@20 from host.
Serving at port 8820

✔  functions: Loaded functions definitions from source: example, publishMessage.
✔  functions[us-central1-example]: pubsub function initialized.
✔  functions[us-central1-publishMessage]: http function initialized (http://127.0.0.1:5001/demo-project/us-central1/publishMessage).

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://127.0.0.1:4000/               │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ 127.0.0.1:5001 │ http://127.0.0.1:4000/functions │
├───────────┼────────────────┼─────────────────────────────────┤
│ Pub/Sub   │ 127.0.0.1:8085 │ n/a                             │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at 127.0.0.1:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
 
i  functions: Beginning execution of "us-central1-publishMessage"
i  functions: Finished "us-central1-publishMessage" in 85.38625ms
i  functions: Beginning execution of "us-central1-example"
>  Publish time: 2024-07-16T16:11:15.847Z
i  functions: Finished "us-central1-example" in 3.460125ms

Sample Commands

@aalej aalej marked this pull request as draft July 16, 2024 14:54
Copy link
Copy Markdown
Member

@joehan joehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Not the first time we've been fooled by getMilliseconds 😬

@aalej aalej marked this pull request as ready for review July 16, 2024 16:51
@aalej aalej merged commit 0249942 into master Jul 16, 2024
@aalej aalej deleted the aalej-pubsub-publishtime branch March 25, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants