litescheduler 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/litescheduler/version.rb +1 -1
- data/lib/litescheduler.rb +97 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56c357252f1ed6a5d890aedcbb1c9197989aa1b9a41a4c6f415f4208c9fa253a
|
4
|
+
data.tar.gz: 6417ab6df8dadc9eda6c19a351548303ae14ab4037654a008b17bcff98c2f69c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd3e8979d83d2b1129a8ee612e4cd4f89bceea536996b94ad035e9657f30b508f8028bddc0503cf1ff98047efa77278c8c20359c1081f9bb581186130b8bc9da
|
7
|
+
data.tar.gz: 7496ddc434a3897acb82401c9b5007bbff5952a1acd4f7ebfade8cf2c2d5d7f8adc63e8f6a3231d9092e17b192a2dc4c2c08bc00f586463370a7d4e72eed3fbf
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/litescheduler.rb
CHANGED
@@ -1,8 +1,102 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "litescheduler/version"
|
4
|
+
require "singleton"
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
class Litescheduler
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
attr_reader :environment, :scheduler, :max_contexts, :storage, :context, :mutex
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@environment = detect_environment
|
13
|
+
@scheduler = detect_scheduler
|
14
|
+
@max_contexts = detect_max_contexts
|
15
|
+
@storage = detect_storage
|
16
|
+
@context = detect_context
|
17
|
+
# a single mutex per process (is that ok?)
|
18
|
+
@mutex = Thread::Mutex.new
|
19
|
+
end
|
20
|
+
|
21
|
+
# spawn a new execution context
|
22
|
+
def spawn(&block)
|
23
|
+
case @scheduler
|
24
|
+
when :fiber then Fiber.schedule(&block)
|
25
|
+
when :polyphony then spin(&block)
|
26
|
+
when :iodine then Thread.new(&block)
|
27
|
+
when :threaded then Thread.new(&block)
|
28
|
+
else
|
29
|
+
raise StandardError.new("Unknown scheduler: `#{@scheduler}`")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# switch the execution context to allow others to run
|
34
|
+
def switch
|
35
|
+
if @scheduler == :fiber
|
36
|
+
Fiber.scheduler.yield
|
37
|
+
true
|
38
|
+
elsif @scheduler == :polyphony
|
39
|
+
Fiber.current.schedule
|
40
|
+
Thread.current.switch_fiber
|
41
|
+
true
|
42
|
+
else
|
43
|
+
# Thread.pass
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# bold assumption, we will only synchronize threaded code
|
49
|
+
# this is a no-op for fibers
|
50
|
+
def synchronize(&block)
|
51
|
+
# do nothing, just run the block as is
|
52
|
+
return yield if @scheduler == :fiber
|
53
|
+
return yield if @scheduler == :polyphony
|
54
|
+
|
55
|
+
@mutex.synchronize(&block)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Detect the Rack or Rails environment.
|
61
|
+
def detect_environment
|
62
|
+
return Rails.env if defined? Rails
|
63
|
+
return ENV["RACK_ENV"] if ENV.key?("RACK_ENV")
|
64
|
+
return ENV["APP_ENV"] if ENV.key?("APP_ENV")
|
65
|
+
|
66
|
+
"development"
|
67
|
+
end
|
68
|
+
|
69
|
+
# identify which scheduler we are running in
|
70
|
+
# we currently support :fiber, :polyphony, :iodine & :threaded
|
71
|
+
# in the future we might want to expand to other schedulers
|
72
|
+
def detect_scheduler
|
73
|
+
return :fiber if Fiber.scheduler
|
74
|
+
return :polyphony if defined? Polyphony
|
75
|
+
return :iodine if defined? Iodine
|
76
|
+
|
77
|
+
:threaded
|
78
|
+
end
|
79
|
+
|
80
|
+
def detect_max_contexts
|
81
|
+
return 50 if scheduler == :fiber
|
82
|
+
return 50 if scheduler == :polyphony
|
83
|
+
|
84
|
+
5
|
85
|
+
end
|
86
|
+
|
87
|
+
def detect_storage
|
88
|
+
if scheduler == :fiber || scheduler == :poylphony
|
89
|
+
Fiber.current.storage
|
90
|
+
else
|
91
|
+
Thread.current
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def detect_context
|
96
|
+
if scheduler == :fiber || scheduler == :poylphony
|
97
|
+
Fiber.current
|
98
|
+
else
|
99
|
+
Thread.current
|
100
|
+
end
|
101
|
+
end
|
8
102
|
end
|