From b6c96301ef1b3dc65897e54f6e1d53a71223aeae Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 14 Jul 2022 17:59:32 +0200 Subject: [PATCH] export and publish opendata demarches --- ...ort_and_publish_demarches_publiques_job.rb | 18 ++++++++++++ config/env.example.optional | 3 ++ ...nd_publish_demarches_publiques_job_spec.rb | 28 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 app/jobs/cron/export_and_publish_demarches_publiques_job.rb create mode 100644 spec/jobs/cron/export_and_publish_demarches_publiques_job_spec.rb diff --git a/app/jobs/cron/export_and_publish_demarches_publiques_job.rb b/app/jobs/cron/export_and_publish_demarches_publiques_job.rb new file mode 100644 index 000000000..3ffede9e6 --- /dev/null +++ b/app/jobs/cron/export_and_publish_demarches_publiques_job.rb @@ -0,0 +1,18 @@ +class Cron::ExportAndPublishDemarchesPubliquesJob < Cron::CronJob + self.schedule_expression = "every month at 3:00" + + def perform(*args) + gzip_filepath = [ + ENV.fetch('DATAGOUV_TMP_DIR', 'tmp'), '/', + Time.zone.now.to_formatted_s(:number), + '-demarches.json.gz' + ].join + + begin + DemarchesPubliquesExportService.new(gzip_filepath).call + APIDatagouv::API.upload(gzip_filepath) + ensure + FileUtils.rm(gzip_filepath) + end + end +end diff --git a/config/env.example.optional b/config/env.example.optional index 73b01bbc5..0e2026159 100644 --- a/config/env.example.optional +++ b/config/env.example.optional @@ -140,3 +140,6 @@ VITE_LEGACY="" # around july 2022, we changed the duree_conservation_dossiers_dans_ds, allow instances to choose their own duration NEW_MAX_DUREE_CONSERVATION=12 +# +# Directory which temporarily holds demarches.json.gz during generation before publishing to datagouv +DATAGOUV_TMP_DIR="/tmp" diff --git a/spec/jobs/cron/export_and_publish_demarches_publiques_job_spec.rb b/spec/jobs/cron/export_and_publish_demarches_publiques_job_spec.rb new file mode 100644 index 000000000..89cec05a5 --- /dev/null +++ b/spec/jobs/cron/export_and_publish_demarches_publiques_job_spec.rb @@ -0,0 +1,28 @@ +RSpec.describe Cron::ExportAndPublishDemarchesPubliquesJob, type: :job do + let!(:procedure) { create(:procedure, :published, :with_service, :with_type_de_champ) } + let(:status) { 200 } + let(:body) { "ok" } + let(:stub) { stub_request(:post, /https:\/\/www.data.gouv.fr\/api\/.*\/upload\//) } + + describe 'perform' do + before do + stub + end + + subject { Cron::ExportAndPublishDemarchesPubliquesJob.perform_now } + + it 'send POST request to datagouv' do + subject + + expect(stub).to have_been_requested + end + + it 'removes gzip file even if an error occured' do + procedure.libelle = nil + procedure.save(validate: false) + + expect { subject }.to raise_error(StandardError) + expect(Dir.glob("*demarches.json.gz", base: 'tmp').empty?).to be_truthy + end + end +end