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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f00d014342b32fc684f19226df74cb8edca44fdf4ead3f22fee4abb100ec336c
4
- data.tar.gz: 1f50457616bcbf474a92d7ab756ba68ecf9bb2164db44d63ab515d2579257551
3
+ metadata.gz: 56c357252f1ed6a5d890aedcbb1c9197989aa1b9a41a4c6f415f4208c9fa253a
4
+ data.tar.gz: 6417ab6df8dadc9eda6c19a351548303ae14ab4037654a008b17bcff98c2f69c
5
5
  SHA512:
6
- metadata.gz: ae3a5ea03ab880e48e39e8ebc13869f46f5aab4de5dd45538b069889a8c0d063e7683194428595d55fe3fd6458e1667689cbd075f2caccd1faeadef67029046b
7
- data.tar.gz: cac68f6747aadcb0998dcd67d5d251ace1be27bc24ef2d9e999ce59329cac5ca9b267dcb366995c67fd750311628ece5baba51b376e9a2742cfba23e38fdaa6b
6
+ metadata.gz: dd3e8979d83d2b1129a8ee612e4cd4f89bceea536996b94ad035e9657f30b508f8028bddc0503cf1ff98047efa77278c8c20359c1081f9bb581186130b8bc9da
7
+ data.tar.gz: 7496ddc434a3897acb82401c9b5007bbff5952a1acd4f7ebfade8cf2c2d5d7f8adc63e8f6a3231d9092e17b192a2dc4c2c08bc00f586463370a7d4e72eed3fbf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-08-10
4
+
5
+ - Initial release of usable code
6
+
3
7
  ## [0.1.0] - 2023-08-10
4
8
 
5
- - Initial release
9
+ - Initial release to reserve gem name
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- litescheduler (0.1.0)
4
+ litescheduler (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Litescheduler
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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
- module Litescheduler
6
- class Error < StandardError; end
7
- # Your code goes here...
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: litescheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohamed Hassan