From 9d3fd00e61da77884973607c2d974721abd006e3 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 20 Feb 2023 16:50:14 +0100
Subject: [PATCH] cron

---
 app/models/communication/website.rb                  | 4 ++++
 app/models/communication/website/with_connections.rb | 2 +-
 app/models/concerns/with_git.rb                      | 2 +-
 cron.json                                            | 3 +++
 lib/tasks/auto.rake                                  | 5 +++++
 5 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 9c6ae1823..532ad6302 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -76,6 +76,10 @@ class Communication::Website < ApplicationRecord
     ", term: "%#{sanitize_sql_like(term)}%")
   }
 
+  def self.save_and_sync_websites!
+    find_each &:save_and_sync
+  end
+
   def to_s
     "#{name}"
   end
diff --git a/app/models/communication/website/with_connections.rb b/app/models/communication/website/with_connections.rb
index c7b01d907..29e440483 100644
--- a/app/models/communication/website/with_connections.rb
+++ b/app/models/communication/website/with_connections.rb
@@ -4,7 +4,7 @@ module Communication::Website::WithConnections
   included do
     has_many  :connections
     
-    after_save :clean_connections!
+    before_save :clean_connections!
   end
 
   def clean_connections!
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index e920fb7c8..84542aa76 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -53,7 +53,6 @@ module WithGit
     websites_for_self.each do |website|
       website.touch
       next unless website.git_repository.valid?
-      dependencies = git_dependencies(website).to_a.flatten.uniq.compact
       dependencies.each do |object|
         Communication::Website::GitFile.sync website, object
       end
@@ -65,6 +64,7 @@ module WithGit
   def destroy_from_git
     websites_for_self.each do |website|
       next unless website.git_repository.valid?
+      # FIXME
       dependencies = git_destroy_dependencies(website).to_a.flatten.uniq.compact
       dependencies.each do |object|
         Communication::Website::GitFile.sync website, object, destroy: true
diff --git a/cron.json b/cron.json
index 72d6dd6fd..bfa2bc642 100644
--- a/cron.json
+++ b/cron.json
@@ -2,6 +2,9 @@
   "jobs": [
     {
       "command": "0 1 * * * rails auto:update_publications_from_hal"
+    },
+    {
+      "command": "0 3 * * * rails auto:save_and_sync_websites"
     }
   ]
 }
\ No newline at end of file
diff --git a/lib/tasks/auto.rake b/lib/tasks/auto.rake
index 7c47ebaaa..2af83edd4 100644
--- a/lib/tasks/auto.rake
+++ b/lib/tasks/auto.rake
@@ -3,4 +3,9 @@ namespace :auto do
   task update_hal: :environment do
     Research::Hal.update_from_api!
   end
+
+  desc 'Save all websites to update connections and clean Git repositories'
+  task save_and_sync_websites: :environment do
+    Communication::Website.save_and_sync_websites!
+  end
 end
\ No newline at end of file
-- 
GitLab