Skip to content

Commit 896f5b3

Browse files
committed
init commit
0 parents  commit 896f5b3

24 files changed

+1115
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*.cr]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

.github/workflows/crystal.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Crystal CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
container:
14+
image: crystallang/crystal
15+
16+
services:
17+
redis:
18+
image: redislabs/redistimeseries
19+
options: >-
20+
--health-cmd "redis-cli ping"
21+
--health-interval 10s
22+
--health-timeout 5s
23+
--health-retries 5
24+
25+
steps:
26+
- uses: actions/checkout@v2
27+
28+
- name: Install dependencies
29+
run: shards install
30+
31+
- name: Run tests
32+
run: crystal spec
33+
env:
34+
REDIS_URL: redis:6379
35+
REDIS_HOST: redis
36+
REDIS_PORT: 6379

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/docs/
2+
/lib/
3+
/bin/
4+
/.shards/
5+
*.dwarf

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: crystal
2+
3+
# Uncomment the following if you'd like Travis to run specs and check code formatting
4+
# script:
5+
# - crystal spec
6+
# - crystal tool format --check

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Elias J. Perez
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# JoobQ
2+
3+
TODO: Write a description here
4+
5+
- [x] Priority queues based on number of workers
6+
- [x] Reliable queue
7+
- [x] Error Handling
8+
- [x] Retry Jobs with automatic Delays
9+
- [x] Cron Like Periodic Jobs
10+
- [x] Delayed Jobs
11+
- [] Expiring Jobs
12+
- [] Batches?
13+
- [] Approve Queue
14+
- [] Job Locking / Disable Concurrent Execution(1 Job Per Instance)
15+
- [] Throttle (Rate limit)
16+
- [] Stop running Queues
17+
18+
## How to sell
19+
- By Number of Workers
20+
- By Worker Size
21+
- By Number of Queues
22+
23+
## How To Integrate
24+
- API Libraries
25+
- Client Libraries
26+
-
27+
## Installation
28+
29+
```yaml
30+
dependencies:
31+
joobq:
32+
github: eliasjpr/joobq
33+
```
34+
35+
```bash
36+
$ shards install
37+
```
38+
39+
## Usage
40+
41+
Scheduled Jobs
42+
43+
```crystal
44+
JoobQ.scheduler.define do
45+
at("5 4 * * *") { Somejob.perform }
46+
end
47+
```
48+
49+
## Development
50+
51+
TODO: Write development instructions here
52+
53+
## Contributing
54+
55+
1. Fork it (<https://github.com/your-github-user/joobq/fork>)
56+
2. Create your feature branch (`git checkout -b my-new-feature`)
57+
3. Commit your changes (`git commit -am 'Add some feature'`)
58+
4. Push to the branch (`git push origin my-new-feature`)
59+
5. Create a new Pull Request
60+
61+
## Contributors
62+
63+
- [Elias J. Perez](https://github.com/your-github-user) - creator and maintainer

bench/bench.cr

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "../src/joobq"
2+
require "benchmark"
3+
4+
struct TestJob
5+
include JoobQ::Job
6+
7+
property x : Int32
8+
@retries = 3
9+
@queue = "queue:test"
10+
11+
def initialize(@x : Int32)
12+
end
13+
14+
def perform
15+
x + 1
16+
end
17+
end
18+
19+
struct FailJob
20+
include JoobQ::Job
21+
@queue = "queue:fail"
22+
@retries = 3
23+
24+
def initialize
25+
end
26+
27+
def perform
28+
raise "Bad"
29+
end
30+
end
31+
32+
JoobQ.reset
33+
34+
module JoobQ
35+
QUEUES = {
36+
"queue:test" => JoobQ::Queue(TestJob).new("queue:test", 80),
37+
"queue:fail" => JoobQ::Queue(FailJob).new("queue:fail", 20),
38+
}
39+
end
40+
41+
JoobQ.run
42+
43+
sleep

bench/load.cr

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "../src/joobq"
2+
3+
struct TestJob
4+
include JoobQ::Job
5+
6+
property x : Int32
7+
@retries = 3
8+
@queue = "queue:test"
9+
10+
def initialize(@x : Int32)
11+
end
12+
13+
def perform
14+
x + 1
15+
end
16+
end
17+
18+
struct FailJob
19+
include JoobQ::Job
20+
@queue = "queue:fail"
21+
@retries = 3
22+
23+
def initialize
24+
end
25+
26+
def perform
27+
raise "Bad"
28+
end
29+
end
30+
31+
JoobQ.reset
32+
33+
module JoobQ
34+
QUEUES = {
35+
"queue:test" => JoobQ::Queue(TestJob).new("queue:test", 80),
36+
"queue:fail" => JoobQ::Queue(FailJob).new("queue:fail", 20),
37+
}
38+
end
39+
40+
1000000.times do |i|
41+
TestJob.perform(x: i)
42+
FailJob.perform
43+
end

shard.lock

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2.0
2+
shards:
3+
cron_parser:
4+
git: https://github.com/kostya/cron_parser.git
5+
version: 0.3.0
6+
7+
pool:
8+
git: https://github.com/ysbaddaden/pool.git
9+
version: 0.2.3
10+
11+
redis:
12+
git: https://github.com/stefanwille/crystal-redis.git
13+
version: 2.6.0
14+

shard.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: joobq
2+
version: 0.1.0
3+
4+
authors:
5+
- Elias J. Perez <[email protected]>
6+
7+
targets:
8+
joobq:
9+
main: src/joobq.cr
10+
11+
crystal: 0.34.0
12+
13+
license: MIT
14+
15+
dependencies:
16+
redis:
17+
github: stefanwille/crystal-redis
18+
version: ~> 2.6.0
19+
20+
cron_parser:
21+
github: kostya/cron_parser

spec/job_spec.cr

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require "./spec_helper"
2+
3+
module JoobQ
4+
describe Job do
5+
context "one queue for single job type" do
6+
it "works" do
7+
queue = "single"
8+
9+
REDIS.del queue
10+
REDIS.del Sets::Delayed.to_s
11+
REDIS.del queue
12+
13+
Job1.perform
14+
15+
REDIS.llen(queue).should eq 1
16+
end
17+
end
18+
19+
context "one queue for multiple job types" do
20+
it "works" do
21+
queue = "example"
22+
23+
REDIS.del queue
24+
REDIS.del Sets::Delayed.to_s
25+
REDIS.del queue
26+
27+
ExampleJob.perform(x: 1)
28+
FailJob.perform
29+
30+
REDIS.llen(queue).should eq 2
31+
end
32+
33+
it "performs and enqueue jobs asyncronously" do
34+
queue = "example"
35+
36+
REDIS.del queue
37+
REDIS.del Sets::Delayed.to_s
38+
39+
job_id = ExampleJob.perform(x: 1)
40+
41+
job_id.should be_a UUID
42+
REDIS.llen(queue).should eq 1
43+
end
44+
45+
it "performs jobs at later time" do
46+
queue = "example"
47+
REDIS.del queue
48+
REDIS.del Sets::Delayed.to_s
49+
50+
job_id = ExampleJob.perform(within: 1.hour, x: 1)
51+
job_id.should be_a UUID
52+
53+
REDIS.zcard(Sets::Delayed.to_s).should eq 1
54+
end
55+
end
56+
end
57+
end

spec/jobbq_spec.cr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "./spec_helper"
2+
3+
describe JoobQ do
4+
describe "#register" do
5+
it "registers a queue" do
6+
JoobQ.queues.size.should eq 5
7+
end
8+
9+
it "gets queue by name" do
10+
JoobQ["joobq_queue1"].should be_a JoobQ::Queue(ExampleJob)
11+
JoobQ["joobq_queue2"].should be_a JoobQ::Queue(FailJob)
12+
JoobQ["joobq_queue3"].should be_a JoobQ::Queue(ExampleJob | FailJob)
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)