From 06361bf53cba918582e3ec8faf86b4a48ce81ed5 Mon Sep 17 00:00:00 2001 From: Max Savin Date: Mon, 8 Jul 2019 17:27:23 +0200 Subject: [PATCH] Improve and document Jobs.find() --- DOCUMENTATION.md | 25 +++++++++++++++++++ .../server/imports/actions/find/process.js | 8 ++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index 3793427..ecafadc 100755 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -5,6 +5,7 @@ Steve Jobs is an all inclusive package for scheduling background jobs. It automa - [Jobs.configure](#jobsconfigure) - [Jobs.register](#jobsregister) - [Jobs.run](#jobsrun) + - [Jobs.find](#jobsfind) - [Jobs.execute](#jobsexecute) - [Jobs.reschedule](#jobsreschedule) - [Jobs.replicate](#jobsreplicate) @@ -164,6 +165,30 @@ The configuration object supports the following inputs: - **`callback`** - Function - Run a callback function after scheduling the job +### Jobs.find + +`Jobs.find` allows you to find a single pending job by its arguments, and run logic against it. You can pass in a callback in the end to check for the document, and you can run the same operations with-in the function scope as you would with `Jobs.register`, as well as `this.run` as a shortcut to `Jobs.run`. + +```javascript +Jobs.find("sendReminder", "jony@apple.com", "The future is here!", function (jobDoc) { + if (jobDoc) { + this.reschedule({ + in: { + minutes: 5 + } + }); + + return jobDoc; + } else { + this.run({ + in: { + minutes: 5 + } + }) + } +}); +``` + ### Jobs.execute `Jobs.execute` allows you to run a job ahead of its due date. It can only work on jobs that have not been resolved. diff --git a/package/server/imports/actions/find/process.js b/package/server/imports/actions/find/process.js index f2604bc..0709991 100755 --- a/package/server/imports/actions/find/process.js +++ b/package/server/imports/actions/find/process.js @@ -2,8 +2,12 @@ import { Utilities } from "../../utilities" import { toolbelt } from "./toolbelt.js" var process = function (doc, callback) { - var Toolbelt = new toolbelt(doc); - + var Toolbelt; + + if (typeof doc === "object") { + Toolbelt = new toolbelt(doc); + } + try { var jobResult = callback.apply(Toolbelt, [doc]); }