• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-concurrency / concurrent-ruby / #2711

16 Jun 2014 12:13PM UTC coverage: 45.896% (-50.5%) from 96.422%
#2711

push

jdantonio
Merge pull request #112 from ruby-concurrency/remove-old-actor

Remove old Actor

1 of 2 new or added lines in 2 files covered. (50.0%)

1362 existing lines in 62 files now uncovered.

1219 of 2656 relevant lines covered (45.9%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/lib/concurrent/executor/serialized_execution.rb
1
require 'concurrent/logging'
1✔
2

3
module Concurrent
1✔
4

5
  # Ensures passed jobs in a serialized order never running at the same time.
6
  class SerializedExecution
1✔
7
    include Logging
1✔
8

9
    Job = Struct.new(:executor, :args, :block) do
1✔
10
      def call
1✔
11
        block.call *args
1✔
12
      end
13
    end
14

15
    def initialize
1✔
16
      @being_executed = false
1✔
17
      @stash          = []
1✔
18
      @mutex          = Mutex.new
1✔
19
    end
20

21
    # Submit a task to the executor for asynchronous processing.
22
    #
23
    # @param [Executor] executor to be used for this job
24
    #
25
    # @param [Array] args zero or more arguments to be passed to the task
26
    #
27
    # @yield the asynchronous task to perform
28
    #
29
    # @return [Boolean] `true` if the task is queued, `false` if the executor
30
    #   is not running
31
    #
32
    # @raise [ArgumentError] if no task is given
33
    def post(executor, *args, &task)
1✔
34
      return nil if task.nil?
1✔
35

36
      job = Job.new executor, args, task
1✔
37

38
      begin
39
        @mutex.lock
1✔
40
        post = if @being_executed
1✔
UNCOV
41
                 @stash << job
×
UNCOV
42
                 false
×
43
               else
44
                 @being_executed = true
1✔
45
               end
46
      ensure
47
        @mutex.unlock
1✔
48
      end
49

50
      call_job job if post
1✔
51
      true
1✔
52
    end
53

54
    private
1✔
55

56
    def call_job(job)
1✔
57
      did_it_run = begin
58
        job.executor.post { work(job) }
2✔
59
        true
1✔
60
      rescue RejectedExecutionError => ex
61
        false
×
62
      end
63

64
      # TODO not the best idea to run it myself
65
      unless did_it_run
1✔
66
        begin
67
          work job
×
68
        rescue => ex
69
          # let it fail
70
          log DEBUG, ex
×
71
        end
72
      end
73
    end
74

75
    # ensures next job is executed if any is stashed
76
    def work(job)
1✔
77
      job.call
1✔
78
    ensure
79
      begin
80
        @mutex.lock
1✔
81
        job = @stash.shift || (@being_executed = false)
1✔
82
      ensure
83
        @mutex.unlock
1✔
84
      end
85

86
      call_job job if job
1✔
87
    end
88

89
  end
90
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc