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

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