Module: PandaPal::OrganizationConcerns::TaskScheduling

Extended by:
ActiveSupport::Concern
Includes:
SettingsValidation
Included in:
PandaPal::Organization
Defined in:
app/models/panda_pal/organization_concerns/task_scheduling.rb,
app/models/panda_pal/organization_concerns/task_scheduling.rb

Defined Under Namespace

Classes: ScheduledTaskExecutor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SettingsValidation

#settings_structure, #validate_settings

Class Method Details

.build_settings_entry(desc) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/panda_pal/organization_concerns/task_scheduling.rb', line 118

def self.build_settings_entry(desc)
  k = desc[:key]
  kl = ' ' * (k.to_s.length - 4)

  default_schedule = '<Computed>'
  default_schedule = desc[:schedule] if desc[:schedule].is_a?(String)
  default_schedule = '<Disabled>' unless desc[:schedule].present?

  {
    required: false,
    description: <<~MARKDOWN,
      Override schedule for '#{k.to_s}' task.

      **Default**: #{default_schedule}

      Set to `false` to disable or supply a Cron string:
      ```yaml
      #{k.to_s}: 0 0 0 * * * America/Denver
      ##{kl}     │ │ │ │ │ │ └── Timezone (Optional)
      ##{kl}     │ │ │ │ │ └── Day of Week
      ##{kl}     │ │ │ │ └── Month
      ##{kl}     │ │ │ └── Day of Month
      ##{kl}     │ │ └── Hour
      ##{kl}     │ └── Minute
      ##{kl}     └── Second (Optional)
      ````
    MARKDOWN
    json_schema: {
      oneOf: [
        { type: 'string', pattern: '^((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,6})(\w+\/\w+)?$' },
        { enum: [false] },
      ],
      default: desc[:schedule].is_a?(String) ? desc[:schedule] : '0 0 3 * * * America/Denver',
    },
    validate: ->(value, *args, errors:, **kwargs) {
      begin
        Rufus::Scheduler.parse(value) if value
        nil
      rescue ArgumentError
        errors << "<path> must be false or a Crontab string"
      end
    }
  }
end

Instance Method Details

#generate_scheduleObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/panda_pal/organization_concerns/task_scheduling.rb', line 93

def generate_schedule
  schedule = {}
  self.class._schedule_descriptors.values.each do |desc|
    cron_time = schedule_task_cron_time(desc)
    next unless cron_time.present?

    schedule["org:#{name}-#{desc[:key]}"] = {
      'cron' => cron_time,
      'queue' => desc[:queue],
      'class' => ScheduledTaskExecutor.to_s,
      'args' => [name, desc[:key]],
      'enabled' => !Rails.env.development?,
    }
  end
  schedule
end

#sync_scheduleObject



110
111
112
113
114
115
116
# File 'app/models/panda_pal/organization_concerns/task_scheduling.rb', line 110

def sync_schedule
  new_schedules = generate_schedule
  unschedule_tasks(new_schedules.keys)
  new_schedules.each do |k, v|
    Sidekiq.set_schedule(k, v)
  end
end