export and publish opendata demarches

This commit is contained in:
Christophe Robillard 2022-07-14 17:59:32 +02:00
parent 77a9a2ba12
commit b6c96301ef
3 changed files with 49 additions and 0 deletions

View file

@ -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

View file

@ -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"

View file

@ -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