Project

General

Profile

Access to Redmine infrastructure from inside a plugin

Added by Denis Adamchik almost 5 years ago

Currently I am working on a plugin to provide some scheduled activity. For that I use the 'sidekiq-scheduler' gem and redis server. I have two classes: a simple one, which provides a perform method of the Sidekiq::Worker module

require 'sidekiq-scheduler'
require_relative '../../lib/issue_status_master'

class IssueStatusWorker

  include Sidekiq::Worker

  def perform
    IssueStatusMaster.update!
  end

end

and another one, which implements the business logic

class IssueStatusMaster

  include Redmine::I18n

  def self.update!
    # Some business logic, involving Redmine model classes
  end

end

The location of the first file is 'app/workers/issue_status_worker.rb', while that of the second is 'lib/issue_status_master.rb'.

When I run sidekiq from the redmine root:

'sidekiq -r ./plugins/issue_master/app/workers/issue_status_worker.rb' -C /plugins/issue_master/config/sidekiq.yml I get an error: 'uninitialized constant IssueStatusMaster::Redmine'.

When I tried to send to the class the required Redmine module during the initialization instead of directly including it within the class declaration

ActionDispatch::Callbacks.to_prepare do
  IssueStatusMaster.send(:include, Redmine::I18n)
end

I was able to use its methods but I still had no access to Redmine model classes.
As far as I can get migration of the plugin does not automatically give such an access. What should I do to fix the problem?

In addition, I have a rake task in 'lib/tasks' directory, which calls the 'update' method of the IssueStatusMaster class and the transaction goes flawlessly.