https://github.com/tannerkrewson/simple-job-queue
a simple job queue in node
https://github.com/tannerkrewson/simple-job-queue
Last synced: 8 months ago
JSON representation
a simple job queue in node
- Host: GitHub
- URL: https://github.com/tannerkrewson/simple-job-queue
- Owner: tannerkrewson
- Created: 2018-03-13T00:44:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-13T22:52:59.000Z (over 8 years ago)
- Last Synced: 2025-02-01T19:45:00.551Z (over 1 year ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# simple-job-queue
## Install
1. Run MySQL on the default port (3306) with the following properties:
```
host : 'localhost'
user : 'root'
password : 'password'
// If you need to use a different user, change the settings in /controllers/queue.js.
```
2. Run the following commands to prep the database:
```
CREATE DATABASE `simple-job-queue`;
USE `simple-job-queue`;
CREATE TABLE `jobs` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`status` tinyint(4) DEFAULT NULL,
`url` text,
`result` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
);
```
3. Run this command: `npm install`.
4. Finally, to start the server, run: `npm start`.
## Usage
Go to `localhost:3000` to use an HTML interface to interact with the API.
### Queue new job
Given a URL, creates a new job to fetch its content. Returns the id of the job.
* **URL:** `/job`
* **Method:** `POST`
* **Data Params**
* `url`: string with a HTTP/HTTPS URL
* **Success Response:**
* `201 Created`
* **Content:** `{"id":13,"url":"http://google.com/"}`
* **Error Response:**
* `400 Bad Request`: Bad URL
* `500 Internal Server Error`: Failed to add to database
### Check job status
Given a valid job id, returns the status, and if the job was successful, its
results.
* **Job Status Codes**
* `0`: Job not completed, still in queue
* `1`: Job was successful, results present in request
* `-1`: Job failed
* **URL:** `/job`
* **Method:** `GET`
* **URL Params**
* `?id=[integer]`: id of a job
* **Success Response:**
* `200 OK`
* **Content:** `{"id":13,"status":1,"url":"http://google.com/","result":"..."}`
* **Error Response:**
* `404 Not Found`: Invalid job id
## Implementation
An [async](https://github.com/caolan/async) queue is used in
`/controllers/queue.js`. Not only does it queue up fetches, but it also queues
any interaction with the database.
If this project were to be developed further, here are a few things I would
want to add:
* Abstract database credentials into a config file or NODE_ENV.
* Secure from MySQL injections
* More informative job status codes
* Store the queue in the database itself in case the server crashes
* Store job result in a separate table
* Add tests for internal functions, as well as the external API itself
* Use a different kind of id for jobs
* Endpoint for getting multiple or all jobs