lib/rails_cloud_tasks/scheduler.rb in rails-cloud-tasks-0.0.3 vs lib/rails_cloud_tasks/scheduler.rb in rails-cloud-tasks-0.0.4.rc1
- old
+ new
@@ -21,19 +21,11 @@
# Create & Update scheduler job on Google Cloud
# TODO: Support to delete scheduled jobs
def upsert
result = { success: [], failure: [] }
scheduler_jobs.each do |job|
- success = true
- begin
- client.create_job parent: location_path, job: job
- rescue Google::Cloud::AlreadyExistsError
- client.update_job job: job
- rescue StandardError
- success = false
- end
- success ? (result[:success] << job[:name]) : (result[:failure] << job[:name])
+ upsert_job(job) ? (result[:success] << job[:name]) : (result[:failure] << job[:name])
end
log_output(result)
end
protected
@@ -51,11 +43,11 @@
name: "#{location_path}/jobs/#{scheduler_prefix_name}--#{job[:name]}",
schedule: job[:schedule],
description: job[:description],
time_zone: job[:time_zone],
http_target: {
- uri: "#{host}#{tasks_path}/#{job[:name]}",
+ uri: "#{host}#{tasks_path}/#{job[:class_name] || job[:name]}",
http_method: 'POST',
body: job[:args].to_json
}.merge(auth)
}
end
@@ -81,8 +73,23 @@
end
def log(desc, prefix, tasks)
logger.info(desc)
logger.info(prefix + tasks.join("\n #{prefix}"))
+ end
+
+ private
+
+ def upsert_job(job)
+ success = true
+ begin
+ client.create_job parent: location_path, job: job
+ rescue Google::Cloud::AlreadyExistsError
+ client.update_job job: job
+ rescue StandardError => e
+ logger.error(e)
+ success = false
+ end
+ success
end
end
end