@pbvision/cloud-run-service 0.0.42 → 0.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/cloud-run-service",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "description": "fastify-firestore-service Web Framework on Cloud Run",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@google-cloud/tasks": "^4.0.1",
31
- "@pbvision/fastify-firestore-service": "^0.0.47",
31
+ "@pbvision/fastify-firestore-service": "^0.0.48",
32
32
  "google-auth-library": "^9.4.2",
33
33
  "ua-parser-js": "^1.0.37"
34
34
  },
package/src/tasks.js CHANGED
@@ -47,7 +47,9 @@ export async function enqueueCloudTask ({
47
47
  service = 'internal',
48
48
  name = undefined,
49
49
  hashNameParts = undefined,
50
- ignoreNameAlreadyUsedError = false
50
+ ignoreNameAlreadyUsedError = false,
51
+ delaySecs = 0,
52
+ scheduledEpoch = undefined // can only be up to 30 days from now
51
53
  }) {
52
54
  const project = process.env.GCLOUD_PROJECT
53
55
  const region = process.env.REGION
@@ -66,6 +68,20 @@ export async function enqueueCloudTask ({
66
68
  }
67
69
  }
68
70
  }
71
+ const currentEpoch = new Date().getTime() / 1000
72
+ if (delaySecs) {
73
+ task.scheduleTime = {
74
+ seconds: Math.ceil(currentEpoch + delaySecs)
75
+ }
76
+ assert(!scheduledEpoch, 'cannot specify both delaysSecs and scheduledEpoch')
77
+ }
78
+ if (scheduledEpoch) {
79
+ scheduledEpoch = Math.floor(scheduledEpoch)
80
+ task.scheduleTime = { seconds: Math.floor(scheduledEpoch) }
81
+ }
82
+ if (task.scheduleTime) {
83
+ assert(task.scheduleTime.seconds <= currentEpoch + 30 * 86400, 'cannot delay for more than 30 days')
84
+ }
69
85
  if (hashNameParts) {
70
86
  assert(Array.isArray(hashNameParts))
71
87
  assert(!name, 'cannot specify both name and hashNameParts')
@@ -1,7 +1,7 @@
1
1
  import crypto from 'node:crypto'
2
2
 
3
3
  import { CloudTasksClient } from '@google-cloud/tasks'
4
- import { jest } from '@jest/globals'
4
+ import { expect, jest } from '@jest/globals'
5
5
 
6
6
  import { enqueueCloudTask } from '../src/tasks.js'
7
7
 
@@ -62,6 +62,20 @@ class TestTasks extends BaseTest {
62
62
  await this.check({ payload: { x: 3 } })
63
63
  }
64
64
 
65
+ async testEnqueueTaskWithDelay () {
66
+ const now = new Date().getTime() / 1000
67
+ await this.check(
68
+ { payload: { x: 3 }, delaySecs: 10 },
69
+ { scheduleTime: { seconds: expect.closeTo(now + 10, -0.7) } })
70
+ }
71
+
72
+ async testEnqueueTaskWithSchedule () {
73
+ const target = 100 + Math.floor(new Date().getTime() / 1000)
74
+ await this.check(
75
+ { payload: { x: 3 }, scheduledEpoch: target },
76
+ { scheduleTime: { seconds: target } })
77
+ }
78
+
65
79
  async testEnqueueTaskWithName () {
66
80
  await this.check(
67
81
  { name: 'x', payload: { x: 3 } },