Sidekiq:如何在(重新)部署时开始工作?

时间:2017-03-21 00:49:34

标签: ruby-on-rails ruby sidekiq rails-activejob

我有一些查询某些外部API的作业。我有两种类型的工作。

收集大量数据的一项工作。

另一个是每15分钟更新一次数据。

我希望批量作业在部署时触发一次以收集批量数据,然后在更新作业按设定的时间间隔执行时处于休眠状态,以保持数据的最佳状态。

如何在部署时将作业设置为触发一次?

修改

这是我到目前为止所尝试过的。

使用rake任务

Redis and Puma stuff above...
sidekiq_1  | 2017-03-21T02:20:36.829Z 1 TID-gq6vufr4k INFO: Booting Sidekiq 4.2.3 with redis options {:url=>"redis://redis:6379/"}
sidekiq_1  | 2017-03-21T02:20:37.449Z 1 TID-gq6vufr4k INFO: Running in ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
sidekiq_1  | 2017-03-21T02:20:37.449Z 1 TID-gq6vufr4k INFO: See LICENSE and the LGPL-3.0 for licensing details.
sidekiq_1  | 2017-03-21T02:20:37.450Z 1 TID-gq6vufr4k INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
sidekiq_1  | 2017-03-21T02:20:37.456Z 1 TID-gq6vufr4k INFO: Loading Schedule
sidekiq_1  | 2017-03-21T02:20:37.456Z 1 TID-gq6vufr4k INFO: Schedule empty! Set Sidekiq.schedule
sidekiq_1  | 2017-03-21T02:20:37.457Z 1 TID-gq6vufr4k INFO: Schedules Loaded
sidekiq_1  | 2017-03-21T02:20:37.463Z 1 TID-gq6vufr4k INFO: Reloading Schedule
sidekiq_1  | 2017-03-21T02:20:37.464Z 1 TID-gq6vufr4k INFO: Loading Schedule
sidekiq_1  | 2017-03-21T02:20:37.464Z 1 TID-gq6vufr4k INFO: Scheduling UpdateEventbriteJob {"cron"=>"*/15 * * * *", "class"=>"UpdateEventbriteJob"}
sidekiq_1  | 2017-03-21T02:20:37.969Z 1 TID-gq6vufr4k INFO: Scheduling UpdateMeetupJob {"cron"=>"*/15 * * * *", "class"=>"UpdateMeetupJob"}
sidekiq_1  | 2017-03-21T02:20:38.353Z 1 TID-gq6vufr4k INFO: Scheduling UpdateStatusJob {"cron"=>"0 0 * * *", "class"=>"UpdateStatusJob"}
sidekiq_1  | 2017-03-21T02:20:38.383Z 1 TID-gq6vufr4k INFO: Schedules Loaded
sidekiq_1  | 2017-03-21T02:20:38.383Z 1 TID-gq6vufr4k INFO: Starting processing, hit Ctrl-C to stop
sidekiq_1  | 2017-03-21T02:20:38.610Z 1 TID-gq6wf5uic FetchEventbriteJob JID-6803f9b93ce89e02ccb73d1d INFO: start
sidekiq_1  | 2017-03-21T02:20:38.623Z 1 TID-gq6wf5vp4 FetchEventbriteJob JID-79b83c0d2be3c647d8c8b52e INFO: start
sidekiq_1  | 2017-03-21T02:20:38.652Z 1 TID-gq6wf5v68 FetchMeetupJob JID-5c65f2262582a1e278b9986d INFO: start
sidekiq_1  | 2017-03-21T02:20:38.752Z 1 TID-gq6wf5tx8 FetchMeetupJob JID-cedc08eb695de6c14e371259 INFO: start
sidekiq_1  | 2017-03-21T02:20:38.854Z 1 TID-gq6wf5uqo FetchEventbriteJob JID-227879792a37d35bb6815a5d INFO: start
web_1      | Puma starting in single mode...
web_1      | * Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
web_1      | * Min threads: 5, max threads: 5
web_1      | * Environment: development
web_1      | * Listening on tcp://0.0.0.0:3000
web_1      | Use Ctrl-C to stop

看起来就像它开始一样,但它并没有真正去任何地方:

# application.rb
config.after_initialize do
  Event.build_eventbrite
  Event.build_meetup
end

# event.rb
def build_eventbrite
  FetchEventbriteJob.perform_later
end

def build_meetup
  FetchMeetupJob.perform_later
end

我试图调用一个调用相同工作的方法:

sidekiq_1  | 2017-03-21T02:29:23.858Z 1 TID-gqzwqq0wc INFO: Booting Sidekiq 4.2.3 with redis options {:url=>"redis://redis:6379/"}
sidekiq_1  | undefined method `build_eventbrite' for Event (call 'Event.connection' to establish a connection):Class
sidekiq_1  | /usr/local/bundle/gems/activerecord-5.0.0.1/lib/active_record/dynamic_matchers.rb:21:in `method_missing'
sidekiq_1  | /work_project/config/application.rb:29:in `block in <class:Application>'
sidekiq_1  | /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
sidekiq_1  | /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
sidekiq_1  | /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:44:in `each'
sidekiq_1  | /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/application/finisher.rb:65:in `block in <module:Finisher>'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `instance_exec'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `run'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `call'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
sidekiq_1  | /usr/local/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:54:in `run_initializers'
sidekiq_1  | /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/application.rb:352:in `initialize!'
sidekiq_1  | /work_project/config/environment.rb:5:in `<top (required)>'
sidekiq_1  | /usr/local/bundle/gems/sidekiq-4.2.3/lib/sidekiq/cli.rb:242:in `require'
sidekiq_1  | /usr/local/bundle/gems/sidekiq-4.2.3/lib/sidekiq/cli.rb:242:in `boot_system'
sidekiq_1  | /usr/local/bundle/gems/sidekiq-4.2.3/lib/sidekiq/cli.rb:50:in `run'
sidekiq_1  | /usr/local/bundle/gems/sidekiq-4.2.3/bin/sidekiq:12:in `<top (required)>'
sidekiq_1  | /usr/local/bundle/bin/sidekiq:17:in `load'
sidekiq_1  | /usr/local/bundle/bin/sidekiq:17:in `<top (required)>'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `load'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `kernel_load'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in `run'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in `exec'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in `dispatch'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in `start'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/exe/bundle:32:in `block in <top (required)>'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
sidekiq_1  | /usr/local/bundle/gems/bundler-1.14.6/exe/bundle:24:in `<top (required)>'
sidekiq_1  | /usr/local/bundle/bin/bundle:22:in `load'
sidekiq_1  | /usr/local/bundle/bin/bundle:22:in `<main>'
work_project_sidekiq_1 exited with code 1
web_1      | Exiting
web_1      | /usr/local/bundle/gems/activerecord-5.0.0.1/lib/active_record/dynamic_matchers.rb:21:in `method_missing': undefined method `build_eventbrite' for Event (call 'Event.connection' to establish a connection):Class (NoMethodError)
web_1      |  from /work_project/config/application.rb:29:in `block in <class:Application>'
web_1      |  from /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
web_1      |  from /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
web_1      |  from /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:44:in `each'
web_1      |  from /usr/local/bundle/gems/activesupport-5.0.0.1/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/application/finisher.rb:65:in `block in <module:Finisher>'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `instance_exec'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `run'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `call'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
web_1      |  from /usr/local/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/initializable.rb:54:in `run_initializers'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/application.rb:352:in `initialize!'
web_1      |  from /work_project/config/environment.rb:5:in `<top (required)>'
web_1      |  from /work_project/config.ru:3:in `require_relative'
web_1      |  from /work_project/config.ru:3:in `block in <main>'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:55:in `instance_eval'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:55:in `initialize'
web_1      |  from /work_project/config.ru:in `new'
web_1      |  from /work_project/config.ru:in `<main>'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:49:in `eval'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:49:in `new_from_string'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/builder.rb:40:in `parse_file'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:318:in `build_app_and_options_from_config'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:218:in `app'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/server.rb:59:in `app'
web_1      |  from /usr/local/bundle/gems/rack-2.0.1/lib/rack/server.rb:353:in `wrapped_app'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/server.rb:124:in `log_to_stdout'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/server.rb:77:in `start'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:90:in `block in server'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
web_1      |  from /usr/local/bundle/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
web_1      |  from bin/rails:4:in `require'
web_1      |  from bin/rails:4:in `<main>'
work_project_web_1 exited with code 1

看起来Rails并不喜欢这种方法

version: '2'
services:
  db:
    image: postgres:9.6.0
  redis:
    image: redis:3.2.4
    command: redis-server
    ports:
      - '6379:6379'
    volumes:
      - '.:/var/lib/redis/data'
  # To run jobs in development, uncomment out this block
  sidekiq:
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml
    env_file: .env
    links:
      - db
      - redis
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    env_file: .env
    volumes:
      - .:/work_project
    ports:
      - '3000:3000'
    depends_on:
      - db
      - redis

我的docker-compose文件:

^A{8}[A=]

1 个答案:

答案 0 :(得分:0)

也许有点矫枉过正,但你可以看看Sidekiq Scheduler Gem。它提供了很多,比如定期调度作业,但也包括启动时触发的选项。

相关问题