适用于localhost但在Heroku上失败

时间:2013-05-30 12:49:05

标签: ruby-on-rails-3 heroku memcached delayed-job

我正在使用delayed_jobs在后台运行任务。

我正在使用ajax启动任务,worker获取一些uuid并写入缓存任务的状态和结果。

然后我使用另一个ajax每秒轮询一次,看看我是否得到了结果。

它在我的本地主机上运行良好,但是当我上传到heroku时它没有。

我检查了日志,我可以看到工作人员可以读取它写入的缓存,但是当主线程试图访问它时它是空的。

我正在使用瘦服务器,memcachier和dalli。

这是用于写入缓存的代码:

def self.get_meta_info(link_url,job_uuid)
    begin
      #..........
      result =  {
          title: "stuff here..."

      }
      #..........

      Rails.cache.write({job_uuid: job_uuid,type: 'result'},result.to_json)
      Rails.cache.write({job_uuid: job_uuid,type: 'status'},'success')

#the next to lines return the data in the logs
      Rails.logger.info("get_meta_info written to hash at #{job_uuid}")
      Rails.logger.info("get_meta_info result for #{job_uuid} was: #{Rails.cache.read({job_uuid: job_uuid,type: 'result'})}")

    rescue Exception => ex
      Rails.cache.write({job_uuid: job_uuid,type: 'result'},ex)
      Rails.cache.write({job_uuid: job_uuid,type: 'status'},'error')
    end
  end

这是我用于轮询的服务器端代码:(每秒由ajax调用)

  def get_meta_info_result
    job_uuid = params[:job_uuid]
    status = Rails.cache.read({job_uuid: job_uuid,type: 'status'})
    #the next to lines return nothing in the logs
    Rails.logger.info("nlp_provider_controller.get_meta_info_result for uuid  #{job_uuid} read status #{status}")
    Rails.logger.info("nlp_provider_controller.get_meta_info_result for uuid  #{job_uuid} read result #{Rails.cache.read({job_uuid: job_uuid,type: 'result'})}")
    respond_to do |format|
      if status=='success'
        format.json {render json: Rails.cache.read({job_uuid: job_uuid,type: 'result'})}
      elsif status=='error'
        format.json{render :nothing => true, status: :no_content }
      else
        format.json{render :nothing => true, status: :partial_content }
      end
    end

我不知道如何解决这个问题。

坦克你!

1 个答案:

答案 0 :(得分:0)

两天来解决这个问题。愚蠢的错误。

有两个配置,development.rb和production.rb。不是我不知道,但通常我在一个单独的初始化器中配置。

我在开发中配置了Redis,而不是在生产中配置。

添加了:

redis_url = ENV["REDISTOGO_URL"] || "redis://127.0.0.1:6379/0/MyApp"
MyApp::Application.config.cache_store = :redis_store, redis_url

(基于:http://blog.jerodsanto.net/2011/06/connecting-node-js-to-redis-to-go-on-heroku/

它有效。

相关问题