Skip to content

A platform for community discussion. Free, open, simple.

11.5 KB
Raw Blame
# frozen_string_literal: true

module DiscourseWorkflows
  class Execution < ActiveRecord::Base
    self.table_name = "discourse_workflows_executions"

    belongs_to :workflow, class_name: "DiscourseWorkflows::Workflow", foreign_key: "workflow_id"

    has_one :execution_data,
            class_name: "DiscourseWorkflows::ExecutionData",
            foreign_key: "execution_id",
            dependent: :destroy

    has_many :initiated_workflow_call_runs,
             class_name: "DiscourseWorkflows::WorkflowCallRun",
             foreign_key: "parent_execution_id",
             dependent: :delete_all

    enum :status,
         { pending: 0, running: 1, success: 2, error: 3, waiting: 4, rate_limited: 5, skipped: 6 }
    enum :execution_mode, { normal: 0, error_mode: 1, manual: 2 }

    after_create { ExecutionStat.log(workflow_id) unless rate_limited? }

    scope :for_workflow, ->(workflow_id) { workflow_id ? where(workflow_id:) : all }
    scope :recent, ->(period = 7.days) { where("created_at >= ?", period.ago) }
    scope :successful, -> { where(status: :success) }
    scope :with_duration,
          -> { where.not(started_at: nil, finished_at: nil).where(status: %i[success error]) }

    scope :expired_waiting, -> { where(status: :waiting).where("waiting_until < ?", Time.current) }
    scope :stale, ->(cutoff) { where(status: %i[pending running]).where("updated_at < ?", cutoff) }

    CLEARED_WAIT_STATE = {
      resume_token: nil,
      waiting_node_id: nil,
      waiting_until: nil,
      timeout_action: nil,
    }.freeze

    scope :with_snapshot_summary,
          -> do
            joins(
              "LEFT JOIN discourse_workflows_execution_data snapshot " \
                "ON snapshot.execution_id = discourse_workflows_executions.id",
            ).select(
              "discourse_workflows_executions.*",
              "snapshot.workflow_data ->> 'name' AS snapshot_workflow_name",
              "jsonb_path_query_first(snapshot.workflow_data, '$.nodes[*] ? (@.id == $id)', " \
                "jsonb_build_object('id', discourse_workflows_executions.trigger_node_id)) " \
                "AS snapshot_trigger_node",
            )
          end

    TERMINAL_STATUSES_FOR_PURGE = %i[success error rate_limited skipped].freeze
    PURGE_BATCH_SIZE = 5_000

    def self.admin_execution_url(workflow_id, execution_id)
      "#{Discourse.base_url}/admin/plugins/discourse-workflows/workflows/" \
        "#{workflow_id}/executions/#{execution_id}"
    end

    def self.create_pending_manual!(workflow:, trigger_node_id:, trigger_data:)
      transaction do
        create!(
          workflow:,
          workflow_version_id: workflow.version_id,
          trigger_node_id:,
          trigger_data:,
          status: :pending,
          execution_mode: :manual,
        ).tap do |execution|
          ExecutionData.create!(
            execution:,
            workflow_data: WorkflowSnapshot.from_workflow(workflow, published: false).to_h,
          )
        end
      end.tap do |execution|
        ExecutionProgressPublisher.publish_created(
          execution,
          workflow_name: workflow.name,
          trigger_node: workflow.find_node(trigger_node_id),
        )
      end
    end

    def self.create_pending_step!(workflow:, node_id:, trigger_data: {}, run_data: {})
      transaction do
        create!(
          workflow:,
          workflow_version_id: workflow.version_id,
          trigger_node_id: node_id,
          trigger_data:,
          status: :pending,
          execution_mode: :manual,
        ).tap do |execution|
          ExecutionData.create!(
            execution:,
            workflow_data: WorkflowSnapshot.from_workflow(workflow, published: false).to_h,
            data: {
              "entries" => {
              },
              "context" => {
              },
              "node_contexts" => {
              },
              "run_data" => run_data,
            },
          )
        end
      end.tap do |execution|
        ExecutionProgressPublisher.publish_created(
          execution,
          workflow_name: workflow.name,
          trigger_node: workflow.find_node(node_id),
        )
      end
    end

    def self.purge_old
      retention_days = SiteSetting.workflow_executions_retention_days
      return if retention_days <= 0

      cutoff = retention_days.days.ago
      terminal_ids = statuses.values_at(*TERMINAL_STATUSES_FOR_PURGE)

      loop do
        ids =
          where(status: terminal_ids)
            .where("created_at < ?", cutoff)
            .limit(PURGE_BATCH_SIZE)
            .pluck(:id)
        break if ids.empty?

        ExecutionData.where(execution_id: ids).delete_all
        WorkflowCallRun.remove_execution_references(ids)
        where(id: ids).delete_all
      end
    end

    def self.claim_for_resume(execution, resume_token: execution.resume_token)
      scope = where(id: execution.id, status: :waiting, resume_token:)

      now = Time.current
      affected = scope.update_all(status: statuses[:running], updated_at: now)
      return nil if affected.zero?

      execution.status = :running
      execution.updated_at = now
      ExecutionProgressPublisher.publish(execution)
      execution
    end

    def self.claim_pending(execution)
      now = Time.current
      affected =
        where(id: execution.id, status: :pending).update_all(
          status: statuses[:running],
          started_at: now,
          updated_at: now,
        )
      return nil if affected.zero?

      execution.status = :running
      execution.started_at = now
      execution.updated_at = now
      ExecutionProgressPublisher.publish(execution)
      execution
    end

    def self.trigger_summary(node)
      return if node.nil?

      node = node.to_workflow_node if node.respond_to?(:to_workflow_node)
      { name: node["name"], type: node["type"] }
    end

    def self.compute_run_time_ms(steps)
      waiting_types = NodeType.waiting_identifiers
      timed =
        steps.select do |s|
          step_field(s, :started_at) && step_field(s, :finished_at) &&
            waiting_types.exclude?(step_field(s, :node_type))
        end
      return if timed.empty?
      total =
        timed.sum do |s|
          Time.parse(step_field(s, :finished_at).to_s) - Time.parse(step_field(s, :started_at).to_s)
        end
      (total * 1000).round
    end

    def self.step_field(step, key)
      step.is_a?(Hash) ? step[key.to_s] : step.public_send(key)
    end
    private_class_method :step_field

    def fail_with_timeout!
      message = I18n.t("discourse_workflows.errors.approval_timed_out")
      node_id = waiting_node_id
      claimed =
        transaction do
          guards = { status: :waiting, resume_token:, waiting_until:, timeout_action: }
          next false unless fail_if_unchanged!(message, guards)

          update_step_status_in_data!(
            node_id,
            Executor::Step::WAITING,
            Executor::Step::ERROR,
            message,
          )
          true
        end
      return false unless claimed

      notify_failure!(StandardError.new(message))
    end

    def fail_as_stale!
      error = Executor::ExecutionTimedOut.new(pending? ? :queue_timed_out : :run_timed_out)
      return false unless fail_if_unchanged!(error.message, { status:, updated_at: })

      notify_failure!(error)
    end

    def trigger_error_workflow(error, steps: execution_data&.steps_array || [])
      Executor::ErrorWorkflowTrigger.new(
        workflow,
        steps,
        execution: self,
        execution_mode: execution_mode.to_sym,
      ).trigger_error_workflow(error)
    end

    def waiting_step_input_items
      return [{ "json" => {} }] unless execution_data

      entries = execution_data.entries || {}
      steps = entries[waiting_node_id.to_s] || []
      waiting_step = steps.find { |s| s["status"] == "waiting" }
      waiting_step&.dig("input") || [{ "json" => {} }]
    end

    def find_waiting_node
      workflow_node(waiting_node_id)
    end

    def workflow_node(node_id)
      return workflow.find_published_node(node_id) if workflow_data.blank?

      WorkflowSnapshot.new(workflow_data).to_h["nodes"].find { |node| node["id"] == node_id.to_s }
    end

    def node_has_reachable_downstream_of_type?(node_id, type)
      if workflow_data.present?
        return(
          WorkflowSnapshot.new(workflow_data).node_has_reachable_downstream_of_type?(node_id, type)
        )
      end

      workflow.node_has_reachable_downstream_of_type?(node_id, type, published: true)
    end

    def workflow_snapshot_name
      if workflow_data.present?
        snapshot_name = WorkflowSnapshot.new(workflow_data).workflow_name
        return snapshot_name if snapshot_name.present?
      end

      workflow.active_version&.name || workflow.name
    end

    private

    def workflow_data
      execution_data&.workflow_data
    end

    def update_step_status_in_data!(node_id, from_status, to_status, error_msg = nil)
      return unless execution_data

      full_data = execution_data.data.deep_dup
      (full_data["entries"] || {}).each_value do |steps|
        Array(steps).each do |step|
          next unless step["node_id"] == node_id.to_s && step["status"] == from_status.to_s

          step["status"] = to_status.to_s
          step["error"] = error_msg if error_msg
          step["finished_at"] = Time.current.iso8601
        end
      end
      execution_data.update!(data: full_data)
    end

    def fail_if_unchanged!(message, guards)
      now = Time.current

      self
        .class
        .where(id:, **guards)
        .update_all(
          status: self.class.statuses[:error],
          error: message,
          finished_at: now,
          run_time_ms: execution_data && self.class.compute_run_time_ms(execution_data.steps_array),
          updated_at: now,
          **CLEARED_WAIT_STATE,
        )
        .positive?
    end

    def notify_failure!(error)
      reload
      trigger_error_workflow(error)
      DiscourseWorkflows::ExecutionProgressPublisher.publish(self, refresh: true)
      DiscourseWorkflows::WorkflowCallContinuation.child_failed!(self)
      true
    end
  end
end

# == Schema Information
#
# Table name: discourse_workflows_executions
#
#  id                  :bigint           not null, primary key
#  error               :text
#  execution_mode      :integer          default("normal"), not null
#  finished_at         :datetime
#  resume_token        :string(64)
#  run_time_ms         :integer
#  started_at          :datetime
#  status              :integer          default("pending"), not null
#  timeout_action      :string(32)
#  trigger_data        :jsonb
#  waiting_until       :datetime
#  created_at          :datetime         not null
#  updated_at          :datetime         not null
#  job_id              :string
#  trigger_node_id     :string(100)
#  waiting_node_id     :string(100)
#  workflow_id         :bigint           not null
#  workflow_version_id :string(36)       not null
#
# Indexes
#
#  idx_dwf_executions_on_job_id                       (job_id) UNIQUE WHERE (job_id IS NOT NULL)
#  idx_dwf_executions_on_resume_token                 (resume_token) WHERE (resume_token IS NOT NULL)
#  idx_dwf_executions_on_retention                    (created_at) WHERE (status = ANY (ARRAY[2, 3, 5, 6]))
#  idx_dwf_executions_on_status_waiting_until         (status,waiting_until)
#  idx_dwf_executions_on_waiting_until                (waiting_until) WHERE ((waiting_until IS NOT NULL) AND (status = 4))
#  idx_dwf_executions_on_workflow_created_at_id_desc  (workflow_id,created_at DESC,id DESC)
#  idx_dwf_executions_on_workflow_version_id          (workflow_version_id)
#