@mastra/upstash 1.4.2 → 1.4.3

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/dist/index.js CHANGED
@@ -230,20 +230,29 @@ var BackgroundTasksUpstash = class extends BackgroundTasksStorage {
230
230
  const { key, processedRecord } = processRecord(TABLE_BACKGROUND_TASKS, toStorageRecord(task));
231
231
  await this.client.set(key, processedRecord);
232
232
  }
233
- async updateTask(taskId, update) {
234
- const existing = await this.getTask(taskId);
235
- if (!existing) return;
236
- const merged = { ...existing };
237
- if ("status" in update) merged.status = update.status;
238
- if ("result" in update) merged.result = update.result;
239
- if ("error" in update) merged.error = update.error;
240
- if ("suspendPayload" in update) merged.suspendPayload = update.suspendPayload;
241
- if ("retryCount" in update) merged.retryCount = update.retryCount;
242
- if ("startedAt" in update) merged.startedAt = update.startedAt;
243
- if ("suspendedAt" in update) merged.suspendedAt = update.suspendedAt;
244
- if ("completedAt" in update) merged.completedAt = update.completedAt;
245
- const { key, processedRecord } = processRecord(TABLE_BACKGROUND_TASKS, toStorageRecord(merged));
246
- await this.client.set(key, processedRecord);
233
+ async updateTask(taskId, update, options) {
234
+ const patch = {};
235
+ if ("status" in update) patch.status = update.status;
236
+ if ("result" in update) patch.result = update.result ?? null;
237
+ if ("error" in update) patch.error = update.error ?? null;
238
+ if ("suspendPayload" in update) patch.suspend_payload = update.suspendPayload ?? null;
239
+ if ("retryCount" in update) patch.retry_count = update.retryCount;
240
+ if ("startedAt" in update) patch.startedAt = update.startedAt?.toISOString() ?? null;
241
+ if ("suspendedAt" in update) patch.suspendedAt = update.suspendedAt?.toISOString() ?? null;
242
+ if ("completedAt" in update) patch.completedAt = update.completedAt?.toISOString() ?? null;
243
+ if (Object.keys(patch).length === 0) return false;
244
+ const key = getKey(TABLE_BACKGROUND_TASKS, { id: taskId });
245
+ const result = await this.client.eval(`
246
+ local existing = redis.call('GET', KEYS[1])
247
+ if not existing then return 0 end
248
+ local record = cjson.decode(existing)
249
+ if ARGV[1] ~= '' and record.status ~= ARGV[1] then return 0 end
250
+ local patch = cjson.decode(ARGV[2])
251
+ for field, value in pairs(patch) do record[field] = value end
252
+ redis.call('SET', KEYS[1], cjson.encode(record))
253
+ return 1
254
+ `, [key], [options?.expectedStatus ?? "", JSON.stringify(patch)]);
255
+ return Number(result) === 1;
247
256
  }
248
257
  async getTask(taskId) {
249
258
  const key = getKey(TABLE_BACKGROUND_TASKS, { id: taskId });