ruby-on-railsrufus-schedulertzinfo

Rails rufus scheduler intercepted an error


I was basically fetching every country time through timezone and was storing in my constant.rb file .But it is throwing an error .I am using tzinfo gem for fetching the time .I don't know how to resolve this error


[my_task_scheduler.rb]

require 'rufus/scheduler'  
require "rubygems"
require "net/https"
require "uri"
require "json"
require 'tzinfo'


s = Rufus::Scheduler.new
s.every '1m' do 

    Tzone.each do |zone|    
        time = TZInfo::Timezone.get(zone)
         puts time.now.strftime("%I:%M %P")

    end 
end


[constant.rb]

Ctime = ["05:00"]

Tzone = [ "International Date Line West",
"Midway Island" ,
"American Samoa" ,
"Hawaii" ,
"Alaska" ,
"Pacific Time (US & Canada)" ,
"Tijuana" ]


my error is

{ 70091014224280 rufus-scheduler intercepted an error:
  70091014224280   job:
  70091014224280     Rufus::Scheduler::EveryJob "1m" {}
  70091014224280   error:
  70091014224280     70091014224280
  70091014224280     TZInfo::InvalidTimezoneIdentifier
  70091014224280     Invalid identifier
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/tzinfo-1.2.2/lib/tzinfo/zoneinfo_data_source.rb:208:in `load_timezone_info'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/tzinfo-1.2.2/lib/tzinfo/timezone.rb:92:in `get'
  70091014224280       /home/techbirds/shivam/config/initializers/my_tasks_scheduler.rb:14:in `block (2 levels) in <top (required)>'
  70091014224280       /home/techbirds/shivam/config/initializers/my_tasks_scheduler.rb:13:in `each'
  70091014224280       /home/techbirds/shivam/config/initializers/my_tasks_scheduler.rb:13:in `block in <top (required)>'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:234:in `call'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:234:in `do_call'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:258:in `do_trigger'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:300:in `block (3 levels) in start_work_thread'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:303:in `call'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:303:in `block (2 levels) in start_work_thread'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:289:in `loop'
  70091014224280       /home/techbirds/.rvm/gems/ruby-2.2.1/gems/rufus-scheduler-3.1.10/lib/rufus/scheduler/jobs.rb:289:in `block in start_work_thread'
  70091014224280   tz:
  70091014224280     ENV['TZ']: 
  70091014224280     Time.now: 2016-01-06 10:29:47 +0530
  70091014224280   scheduler:
  70091014224280     object_id: 46315140
  70091014224280     opts:
  70091014224280       {}
  70091014224280       frequency: 0.3
  70091014224280       scheduler_lock: #<Rufus::Scheduler::NullLock:0x00000004872158>
  70091014224280       trigger_lock: #<Rufus::Scheduler::NullLock:0x00000004872108>
  70091014224280     uptime: 60.072723042 (1m72)
  70091014224280     down?: false
  70091014224280     threads: 2
  70091014224280       thread: #<Thread:0x00000004872090>
  70091014224280       thread_key: rufus_scheduler_46315140
  70091014224280       work_threads: 1
  70091014224280         active: 1
  70091014224280         vacant: 0
  70091014224280         max_work_threads: 28
  70091014224280       mutexes: {}
  70091014224280     jobs: 1
  70091014224280       at_jobs: 0
  70091014224280       in_jobs: 0
  70091014224280       every_jobs: 1
  70091014224280       interval_jobs: 0
  70091014224280       cron_jobs: 0
  70091014224280     running_jobs: 1
  70091014224280     work_queue: 0
} 70091014224280 .

Solution

  • You are attempting to use time zone identifiers for ActiveSupport::TimeZone with TZInfo. However, TZInfo only understands identifiers from the IANA Time Zone Database. The 'invalid identifier' error you are seeing indicates that it has been passed something that it doesn't recognize.

    Behind the scenes, ActiveSupport::TimeZone uses TZInfo to perform conversions. You can see the mapping from the ActiveSupport::TimeZone identifiers to IANA Time Zone Database identifiers in ActiveSupport::TimeZone::MAPPING.

    You could switch to using the equivalent IANA Time Zone Database identifiers from the mapping:

    require 'tzinfo'
    
    Tzone = [
      "Pacific/Midway",      # International Date Line West
      "Pacific/Midway",      # Midway Island
      "Pacific/Pago_Pago",   # American Samoa
      "Pacific/Honolulu",    # Hawaii
      "America/Juneau",      # Alaska
      "America/Los_Angeles", # Pacific Time (US & Canada)
      "America/Tijuana"]     # Tijuana
    
    Tzone.each do |zone|
      time = TZInfo::Timezone.get(zone)
      puts time.now.strftime("%I:%M %P")
    end
    

    You could also keep using your existing time zone identifiers and use ActiveSupport to perform your calculations instead:

    require 'active_support/all'
    
    Tzone = [
      "International Date Line West",
      "Midway Island",
      "American Samoa",
      "Hawaii",
      "Alaska",
      "Pacific Time (US & Canada)",
      "Tijuana"]
    
    Tzone.each do |zone|
      Time.use_zone(zone) do
        puts Time.zone.now.strftime("%I:%M %P")
      end
    end