Class: Rosette::Queuing::Commits::CommitConductor

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/queuing/commits/commit_conductor.rb

Overview

Coordinates moving a commit through a number of processing stages.

Returns:

  • (void)

Constant Summary collapse

FINISHED_STATUS =

The status that indicates a commit is completely processed, i.e. has no more stages to pass through.

Rosette::DataStores::PhraseStatus::FINALIZED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rosette_config, repo_name, logger) ⇒ CommitConductor

Creates a new instance of +CommitConductor

Parameters:

  • rosette_config (Configurator)

    The Rosette config to use.

  • repo_name (String)

    The name of the repo to process commits for.

  • logger (Logger)

    The logger to use.


38
39
40
41
42
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 38

def initialize(rosette_config, repo_name, logger)
  @rosette_config = rosette_config
  @repo_name = repo_name
  @logger = logger
end

Instance Attribute Details

#loggerLogger (readonly)

Returns the logger to use.

Returns:

  • (Logger)

    the logger to use.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 16

class CommitConductor  # The status that indicates a commit is completely processed, i.e. has
  # no more stages to pass through.

  FINISHED_STATUS = Rosette::DataStores::PhraseStatus::FINALIZED

  attr_reader :rosette_config, :repo_name, :logger

  def self.stage_classes    # grab all classes that inherit from Stage

    @stage_classes ||= begin
      namespace = Rosette::Queuing::Commits
      namespace.constants
        .map { |const_sym| namespace.const_get(const_sym) }
        .select { |const| const < namespace::Stage }
    end
  end

  # Creates a new instance of +CommitConductor
  # @param [Configurator] rosette_config The Rosette config to use.
  # @param [String] repo_name The name of the repo to process commits for.
  # @param [Logger] logger The logger to use.
  # @return [CommitConductor]
  def initialize(rosette_config, repo_name, logger)
    @rosette_config = rosette_config
    @repo_name = repo_name
    @logger = logger
  end

  # Creates a new job for the commit and enqueues it on the configured
  # Rosette queue.
  #
  # @param [String] commit_id The commit to enqueue.
  # @return [void]
  def enqueue(commit_id)
    job = CommitJob.new(repo_name, commit_id)
    enqueue_job(job)
  end

  # Executes the current stage of the commit log and advances it to the
  # next. Also updates the commit log's status.
  #
  # @param [CommitLog] commit_log The commit log to advance.
  # @return [void]
  def advance(commit_log)
    create_stage_instance(commit_log).tap do |stage|
      return unless stage
      stage.execute!
      return if finished?(stage)
      enqueue_job(stage.to_job)
    end
  end

  protected

  def finished?(stage)
    stage.commit_log.status == FINISHED_STATUS
  end

  def enqueue_job(job)
    rosette_config.queue.enqueue(job)
  end

  def create_stage_instance(commit_log)
    self.class.stage_classes.each do |stage_class|
      if stage = stage_class.for_commit_log(commit_log, rosette_config, logger)
        return stage
      end
    end
    nil
  end
end

#repo_nameString (readonly)

Returns the name of the repo to process commits for.

Returns:

  • (String)

    the name of the repo to process commits for.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 16

class CommitConductor  # The status that indicates a commit is completely processed, i.e. has
  # no more stages to pass through.

  FINISHED_STATUS = Rosette::DataStores::PhraseStatus::FINALIZED

  attr_reader :rosette_config, :repo_name, :logger

  def self.stage_classes    # grab all classes that inherit from Stage

    @stage_classes ||= begin
      namespace = Rosette::Queuing::Commits
      namespace.constants
        .map { |const_sym| namespace.const_get(const_sym) }
        .select { |const| const < namespace::Stage }
    end
  end

  # Creates a new instance of +CommitConductor
  # @param [Configurator] rosette_config The Rosette config to use.
  # @param [String] repo_name The name of the repo to process commits for.
  # @param [Logger] logger The logger to use.
  # @return [CommitConductor]
  def initialize(rosette_config, repo_name, logger)
    @rosette_config = rosette_config
    @repo_name = repo_name
    @logger = logger
  end

  # Creates a new job for the commit and enqueues it on the configured
  # Rosette queue.
  #
  # @param [String] commit_id The commit to enqueue.
  # @return [void]
  def enqueue(commit_id)
    job = CommitJob.new(repo_name, commit_id)
    enqueue_job(job)
  end

  # Executes the current stage of the commit log and advances it to the
  # next. Also updates the commit log's status.
  #
  # @param [CommitLog] commit_log The commit log to advance.
  # @return [void]
  def advance(commit_log)
    create_stage_instance(commit_log).tap do |stage|
      return unless stage
      stage.execute!
      return if finished?(stage)
      enqueue_job(stage.to_job)
    end
  end

  protected

  def finished?(stage)
    stage.commit_log.status == FINISHED_STATUS
  end

  def enqueue_job(job)
    rosette_config.queue.enqueue(job)
  end

  def create_stage_instance(commit_log)
    self.class.stage_classes.each do |stage_class|
      if stage = stage_class.for_commit_log(commit_log, rosette_config, logger)
        return stage
      end
    end
    nil
  end
end

#rosette_configConfigurator (readonly)

Returns the Rosette config to use.

Returns:

  • (Configurator)

    the Rosette config to use.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 16

class CommitConductor  # The status that indicates a commit is completely processed, i.e. has
  # no more stages to pass through.

  FINISHED_STATUS = Rosette::DataStores::PhraseStatus::FINALIZED

  attr_reader :rosette_config, :repo_name, :logger

  def self.stage_classes    # grab all classes that inherit from Stage

    @stage_classes ||= begin
      namespace = Rosette::Queuing::Commits
      namespace.constants
        .map { |const_sym| namespace.const_get(const_sym) }
        .select { |const| const < namespace::Stage }
    end
  end

  # Creates a new instance of +CommitConductor
  # @param [Configurator] rosette_config The Rosette config to use.
  # @param [String] repo_name The name of the repo to process commits for.
  # @param [Logger] logger The logger to use.
  # @return [CommitConductor]
  def initialize(rosette_config, repo_name, logger)
    @rosette_config = rosette_config
    @repo_name = repo_name
    @logger = logger
  end

  # Creates a new job for the commit and enqueues it on the configured
  # Rosette queue.
  #
  # @param [String] commit_id The commit to enqueue.
  # @return [void]
  def enqueue(commit_id)
    job = CommitJob.new(repo_name, commit_id)
    enqueue_job(job)
  end

  # Executes the current stage of the commit log and advances it to the
  # next. Also updates the commit log's status.
  #
  # @param [CommitLog] commit_log The commit log to advance.
  # @return [void]
  def advance(commit_log)
    create_stage_instance(commit_log).tap do |stage|
      return unless stage
      stage.execute!
      return if finished?(stage)
      enqueue_job(stage.to_job)
    end
  end

  protected

  def finished?(stage)
    stage.commit_log.status == FINISHED_STATUS
  end

  def enqueue_job(job)
    rosette_config.queue.enqueue(job)
  end

  def create_stage_instance(commit_log)
    self.class.stage_classes.each do |stage_class|
      if stage = stage_class.for_commit_log(commit_log, rosette_config, logger)
        return stage
      end
    end
    nil
  end
end

Class Method Details

.stage_classesObject


23
24
25
26
27
28
29
30
31
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 23

def self.stage_classes  # grab all classes that inherit from Stage

  @stage_classes ||= begin
    namespace = Rosette::Queuing::Commits
    namespace.constants
      .map { |const_sym| namespace.const_get(const_sym) }
      .select { |const| const < namespace::Stage }
  end
end

Instance Method Details

#advance(commit_log) ⇒ void

This method returns an undefined value.

Executes the current stage of the commit log and advances it to the next. Also updates the commit log's status.

Parameters:

  • commit_log (CommitLog)

    The commit log to advance.


59
60
61
62
63
64
65
66
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 59

def advance(commit_log)
  create_stage_instance(commit_log).tap do |stage|
    return unless stage
    stage.execute!
    return if finished?(stage)
    enqueue_job(stage.to_job)
  end
end

#enqueue(commit_id) ⇒ void

This method returns an undefined value.

Creates a new job for the commit and enqueues it on the configured Rosette queue.

Parameters:

  • commit_id (String)

    The commit to enqueue.


49
50
51
52
# File 'lib/rosette/queuing/commits/commit_conductor.rb', line 49

def enqueue(commit_id)
  job = CommitJob.new(repo_name, commit_id)
  enqueue_job(job)
end