Permalink
Browse files

First blood.

  • Loading branch information...
0 parents commit ffe5827f04ccc897508c53d4749932ba5fc017bc @SaltwaterC committed Jun 22, 2011
Showing with 139 additions and 0 deletions.
  1. +12 −0 LICENSE.md
  2. +27 −0 README.md
  3. +96 −0 lib/simple-schedule.js
  4. +4 −0 package.json
@@ -0,0 +1,12 @@
+# simple-schedule license
+
+Copyright (c) 2011, Ștefan Rusu All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Ștefan Rusu nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ȘTEFAN RUSU BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
@@ -0,0 +1,27 @@
+## About
+
+Simple scheduler for node.js. Better documentation will follow.
+
+## Usage mode
+<pre>
+var schedule = require('simple-schedule');
+
+schedule.add('log');
+
+try
+{
+ schedule.insert('log', 1, 'LogEntry: foo');
+ schedule.insert('log', 2, 'LogEntry: bar');
+ schedule.insert('log', 3, 'LogEntry: baz');
+
+ // Dispatches 2 tasks every 2 seconds, executing the given callback
+ // schedule.dispatch callback, time interval, scheduler, number of tasks, execution callback
+ setInterval(schedule.dispatch, 2000, 'log', 2, function (id, item) {
+ console.log(id, item);
+ });
+}
+catch (e) {
+ console.error(e.message);
+}
+</pre>
+
@@ -0,0 +1,96 @@
+/**
+ * Scheduler for batch running a controlled amount of tasks per interval
+ */
+
+var schedulers = {};
+
+var validate = function (scheduler) {
+ if ( ! schedulers[scheduler]) {
+ throw new Error('Invalid scheduler: ' + scheduler);
+ }
+};
+/**
+ * Adds a scheduler
+ *
+ * @param scheduler
+ */
+exports.add = function (scheduler) {
+ if ( ! schedulers[scheduler]) {
+ schedulers[scheduler] = {
+ count: 0,
+ items: {}
+ };
+ }
+};
+/**
+ * Removes a scheduler
+ *
+ * @param scheduler
+ */
+exports.remove = function (scheduler) {
+ if (schedulers[scheduler]) {
+ delete(schedulers[scheduler]);
+ return true;
+ }
+ return false;
+};
+/**
+ * Adds a new scheduler task
+ *
+ * @param scheduler
+ * @param id
+ * @param item
+ */
+exports.insert = function (scheduler, id, item) {
+ validate(scheduler);
+
+ if ( ! schedulers[scheduler].items[id]) {
+ schedulers[scheduler].count++;
+ }
+
+ schedulers[scheduler].items[id] = item;
+};
+/**
+ * Dispatches a scheduler task to the callback argument
+ *
+ * @param scheduler
+ * @param count
+ * @param callback
+ */
+exports.dispatch = function (scheduler, count, callback) {
+ validate(scheduler);
+
+ if (schedulers[scheduler].count > 0) {
+ var index = 0;
+
+ for (id in schedulers[scheduler].items) {
+ callback(id, schedulers[scheduler].items[id]);
+ delete(schedulers[scheduler].items[id]);
+ schedulers[scheduler].count--;
+
+ index++;
+ if (index == count) {
+ break;
+ }
+ }
+ }
+};
+/**
+ * Returns the count parameter of a given scheduler
+ *
+ * @param scheduler
+ */
+exports.count = function (scheduler) {
+ validate(scheduler);
+ return schedulers[scheduler].count;
+};
+/**
+ * Returns the items of a given scheduler
+ *
+ * @param scheduler
+ */
+exports.items = function (scheduler) {
+ validate(scheduler);
+ return schedulers[scheduler].items;
+};
+
@@ -0,0 +1,4 @@
+{
+ "name": "simple-schedule",
+ "main": "./lib/simple-schedule.js"
+}

0 comments on commit ffe5827

Please sign in to comment.