violet_rails icon indicating copy to clipboard operation
violet_rails copied to clipboard

VacuumJob Plugin: External API Connection to remove API resources of an API namespace

Open Ayon95 opened this issue 3 years ago • 1 comments

VacuumJob Plugin

For the latest version of this plugin see here: https://github.com/restarone/violet_rails/blob/master/test/plugin_fixtures/vacuum_job.yml

search for VacuumJob

    class VacuumJob
      def initialize(parameters)  
        @external_api_client = parameters[:external_api_client]
      end

      def start
        # Fetching provided inputs (namespaceid, batch_size, dimension, order, time_before)
        api_namespace_id = @external_api_client.metadata["API_NAMESPACE_ID"]
        batch_size = @external_api_client.metadata["BATCH_SIZE"]
        dimension = @external_api_client.metadata["DIMENSION"]
        order = @external_api_client.metadata["ORDER"].upcase.starts_with?('ASC') ? 'ASC' : 'DESC'
        older_than = @external_api_client.metadata["OLDER_THAN"].to_f # in minutes

        raise 'ApiResource of another ApiNamespace cannot be deleted.'if api_namespace_id.to_s != @external_api_client.api_namespace_id.to_s

        api_resources_to_be_deleted = ApiResource.where(api_namespace_id: api_namespace_id).where("(EXTRACT (EPOCH FROM  now() - #{dimension} )::int/60) > ?", older_than).order("#{dimension} #{order}").limit(batch_size)

        ActiveRecord::Base.transaction do
          api_resources_to_be_deleted.each do |api_resource|
            api_resource.destroy!
          end
        end
      end
    end
    # at the end of the file we have to implicitly return the class 
    VacuumJob

The following metadata is required for the plugin to work:

vacuumjob-metadata

ORDER

  • sorting order of API resources
  • type: string (can be "ascending" or "descending")

DIMENSION

  • timestamp used to calculate how much time has passed since an API resource was created or updated
  • type: string (can be "created_at" or "updated_at")

BATCH_SIZE

  • the number of API resources to be destroyed in a single run
  • type: integer

OLDER_THAN

  • the number of minutes that should pass since the creation or modification of an API resource before it can be deleted
  • type: integer

API_NAMESPACE_ID

  • ID of the API namespace whose API resources should be removed
  • type: integer

Ayon95 avatar Aug 22 '22 18:08 Ayon95

Request to label it as documentation.

Ayon95 avatar Aug 22 '22 18:08 Ayon95