lib/gooddata/models/project.rb
def transfer_schedules(from_project, to_project)
        cache = to_project
                  .processes.sort_by(&:name)
                  .zip(from_project.processes.sort_by(&:name))
                  .flat_map do |remote, local|
                    local.schedules.map do |schedule|
                      [remote, local, schedule]
                    end
                  end

        remote_schedules = to_project.schedules
        remote_stuff = remote_schedules.map do |s|
          v = s.to_hash
          after_schedule = remote_schedules.find { |s2| s.trigger_id == s2.obj_id }
          v[:after] = s.trigger_id && after_schedule && after_schedule.name
          v[:remote_schedule] = s
          v[:params] = v[:params].except("EXECUTABLE", "PROCESS_ID")
          v.compact
        end

        local_schedules = from_project.schedules
        local_stuff = local_schedules.map do |s|
          v = s.to_hash
          after_schedule = local_schedules.find { |s2| s.trigger_id == s2.obj_id }
          v[:after] = s.trigger_id && after_schedule && after_schedule.name
          v[:remote_schedule] = s
          v[:params] = v[:params].except("EXECUTABLE", "PROCESS_ID")
          v.compact
        end

        diff = GoodData::Helpers.diff(remote_stuff, local_stuff, key: :name, fields: [:name, :cron, :after, :params, :hidden_params, :reschedule])
        stack = diff[:added].map do |x|
          [:added, x]
        end

        stack += diff[:changed].map do |x|
          [:changed, x]
        end

        schedule_cache = remote_schedules.reduce({}) do |a, e|
          a[e.name] = e
          a
        end

        results = []
        loop do # rubocop:disable Metrics/BlockLength
          break if stack.empty?
          state, changed_schedule = stack.shift
          if state == :added
            schedule_spec = changed_schedule
            if schedule_spec[:after] && !schedule_cache[schedule_spec[:after]]
              stack << [state, schedule_spec]
              next
            end
            remote_process, process_spec = cache.find do |_remote, local, schedule|
              (schedule_spec[:process_id] == local.process_id) && (schedule.name == schedule_spec[:name])
            end

            GoodData.logger.info("Creating schedule #{schedule_spec[:name]} for process #{remote_process.name}")

            executable = nil
            if process_spec.type != :dataload
              executable = schedule_spec[:executable] || (process_spec.type == :ruby ? 'main.rb' : 'main.grf')
            end
            params = schedule_parameters(to_project.pid, schedule_spec)
            created_schedule = remote_process.create_schedule(schedule_spec[:cron] || schedule_cache[schedule_spec[:after]], executable, params)
            schedule_cache[created_schedule.name] = created_schedule

            results << {
              state: :added,
              process: remote_process,
              schedule: created_schedule
            }
          else
            schedule_spec = changed_schedule[:new_obj]
            if schedule_spec[:after] && !schedule_cache[schedule_spec[:after]]
              stack << [state, schedule_spec]
              next
            end

            remote_process, process_spec = cache.find do |i|
              i[2].name == schedule_spec[:name]
            end

            schedule = changed_schedule[:old_obj][:remote_schedule]

            GoodData.logger.info("Updating schedule #{schedule_spec[:name]} for process #{remote_process.name}")

            schedule.params = (schedule_spec[:params] || {})
            schedule.cron = schedule_spec[:cron] if schedule_spec[:cron]
            schedule.after = schedule_cache[schedule_spec[:after]] if schedule_spec[:after]
            schedule.hidden_params = schedule_spec[:hidden_params] || {}
            if process_spec.type != :dataload
              schedule.executable = schedule_spec[:executable] || (process_spec.type == :ruby ? 'main.rb' : 'main.grf')
            end

            schedule.reschedule = schedule_spec[:reschedule]
            schedule.name = schedule_spec[:name]
            schedule.save
            schedule_cache[schedule.name] = schedule

            results << {
              state: :changed,
              process: remote_process,
              schedule: schedule
            }
          end
        end

        diff[:removed].each do |removed_schedule|
          GoodData.logger.info("Removing schedule #{removed_schedule[:name]}")

          removed_schedule[:remote_schedule].delete

          results << {
            state: :removed,
            process: removed_schedule.process,
            schedule: removed_schedule
          }
        end

        results
      end
# Clones project along with etl and schedules.
#
# @param client [GoodData::Rest::Client] GoodData client to be used for connection
# @param from_project [GoodData::Project | GoodData::Segment | GoodData:Client | String] Object to be cloned from. Can be either segment in which case we take the master, client in which case we take its project, string in which case we treat is as an project object or directly project
Clones project along with etl and schedules.
Clones project along with etl and schedules .
def transfer_schedules(from_project, to_project)
        cache = to_project
                  .processes.sort_by(&:name)
                  .zip(from_project.processes.sort_by(&:name))
                  .flat_map do |remote, local|
                    local.schedules.map do |schedule|
                      [remote, local, schedule]
                    end
                  end
        remote_schedules = to_project.schedules
        remote_stuff = remote_schedules.map do |s|
          v = s.to_hash
          after_schedule = remote_schedules.find { |s2| s.trigger_id == s2.obj_id }
          v[:after] = s.trigger_id && after_schedule && after_schedule.name
          v[:remote_schedule] = s
          v[:params] = v[:params].except("EXECUTABLE", "PROCESS_ID")
          v.compact
        end
        local_schedules = from_project.schedules
        local_stuff = local_schedules.map do |s|
          v = s.to_hash
          after_schedule = local_schedules.find { |s2| s.trigger_id == s2.obj_id }
          v[:after] = s.trigger_id && after_schedule && after_schedule.name
          v[:remote_schedule] = s
          v[:params] = v[:params].except("EXECUTABLE", "PROCESS_ID")
          v.compact
        end
        diff = GoodData::Helpers.diff(remote_stuff, local_stuff, key: :name, fields: [:name, :cron, :after, :params, :hidden_params, :reschedule])
        stack = diff[:added].map do |x|
          [:added, x]
        end
        stack += diff[:changed].map do |x|
          [:changed, x]
        end
        schedule_cache = remote_schedules.reduce({}) do |a, e|
          a[e.name] = e
          a
        end
        results = []
        loop do 
          break if stack.empty?
          state, changed_schedule = stack.shift
          if state == :added
            schedule_spec = changed_schedule
            if schedule_spec[:after] && !schedule_cache[schedule_spec[:after]]
              stack << [state, schedule_spec]
              next
            end
            remote_process, process_spec = cache.find do |_remote, local, schedule|
              (schedule_spec[:process_id] == local.process_id) && (schedule.name == schedule_spec[:name])
            end
            GoodData.logger.info("Creating schedule #{schedule_spec[:name]} for process #{remote_process.name}")
            executable = nil
            if process_spec.type != :dataload
              executable = schedule_spec[:executable] || (process_spec.type == :ruby ? 'main.rb' : 'main.grf')
            end
            params = schedule_parameters(to_project.pid, schedule_spec)
            created_schedule = remote_process.create_schedule(schedule_spec[:cron] || schedule_cache[schedule_spec[:after]], executable, params)
            schedule_cache[created_schedule.name] = created_schedule
            results << {
              state: :added,
              process: remote_process,
              schedule: created_schedule
            }
          else
            schedule_spec = changed_schedule[:new_obj]
            if schedule_spec[:after] && !schedule_cache[schedule_spec[:after]]
              stack << [state, schedule_spec]
              next
            end
            remote_process, process_spec = cache.find do |i|
              i[2].name == schedule_spec[:name]
            end
            schedule = changed_schedule[:old_obj][:remote_schedule]
            GoodData.logger.info("Updating schedule #{schedule_spec[:name]} for process #{remote_process.name}")
            schedule.params = (schedule_spec[:params] || {})
            schedule.cron = schedule_spec[:cron] if schedule_spec[:cron]
            schedule.after = schedule_cache[schedule_spec[:after]] if schedule_spec[:after]
            schedule.hidden_params = schedule_spec[:hidden_params] || {}
            if process_spec.type != :dataload
              schedule.executable = schedule_spec[:executable] || (process_spec.type == :ruby ? 'main.rb' : 'main.grf')
            end
            schedule.reschedule = schedule_spec[:reschedule]
            schedule.name = schedule_spec[:name]
            schedule.save
            schedule_cache[schedule.name] = schedule
            results << {
              state: :changed,
              process: remote_process,
              schedule: schedule
            }
          end
        end
        diff[:removed].each do |removed_schedule|
          GoodData.logger.info("Removing schedule #{removed_schedule[:name]}")
          removed_schedule[:remote_schedule].delete
          results << {
            state: :removed,
            process: removed_schedule.process,
            schedule: removed_schedule
          }
        end
        results
      end
def transfer_schedules ( from_project , to_project ) cache = to_project . processes . sort_by ( & :name ) . zip ( from_project . processes . sort_by ( & :name ) ) . flat_map do | remote , local | local . schedules . map do | schedule | [ remote , local , schedule ] end end remote_schedules = to_project . schedules remote_stuff = remote_schedules . map do | s | v = s . to_hash after_schedule = remote_schedules . find { | s2 | s . trigger_id == s2 . obj_id } v [ :after ] = s . trigger_id && after_schedule && after_schedule . name v [ :remote_schedule ] = s v [ :params ] = v [ :params ] . except ( "EXECUTABLE" , "PROCESS_ID" ) v . compact end local_schedules = from_project . schedules local_stuff = local_schedules . map do | s | v = s . to_hash after_schedule = local_schedules . find { | s2 | s . trigger_id == s2 . obj_id } v [ :after ] = s . trigger_id && after_schedule && after_schedule . name v [ :remote_schedule ] = s v [ :params ] = v [ :params ] . except ( "EXECUTABLE" , "PROCESS_ID" ) v . compact end diff = GoodData :: Helpers . diff ( remote_stuff , local_stuff , key : :name , fields : [ :name , :cron , :after , :params , :hidden_params , :reschedule ] ) stack = diff [ :added ] . map do | x | [ :added , x ] end stack += diff [ :changed ] . map do | x | [ :changed , x ] end schedule_cache = remote_schedules . reduce ( { } ) do | a , e | a [ e . name ] = e a end results = [ ] loop do break if stack . empty? state , changed_schedule = stack . shift if state == :added schedule_spec = changed_schedule if schedule_spec [ :after ] && ! schedule_cache [ schedule_spec [ :after ] ] stack << [ state , schedule_spec ] next end remote_process , process_spec = cache . find do | _remote , local , schedule | ( schedule_spec [ :process_id ] == local . process_id ) && ( schedule . name == schedule_spec [ :name ] ) end GoodData . logger . info ( "Creating schedule #{schedule_spec[:name]} for process #{remote_process.name}" ) executable = nil if process_spec . type != :dataload executable = schedule_spec [ :executable ] || ( process_spec . type == :ruby ? 'main.rb' : 'main.grf' ) end params = schedule_parameters ( to_project . pid , schedule_spec ) created_schedule = remote_process . create_schedule ( schedule_spec [ :cron ] || schedule_cache [ schedule_spec [ :after ] ] , executable , params ) schedule_cache [ created_schedule . name ] = created_schedule results << { state : :added , process : remote_process , schedule : created_schedule } else schedule_spec = changed_schedule [ :new_obj ] if schedule_spec [ :after ] && ! schedule_cache [ schedule_spec [ :after ] ] stack << [ state , schedule_spec ] next end remote_process , process_spec = cache . find do | i | i [ 2 ] . name == schedule_spec [ :name ] end schedule = changed_schedule [ :old_obj ] [ :remote_schedule ] GoodData . logger . info ( "Updating schedule #{schedule_spec[:name]} for process #{remote_process.name}" ) schedule . params = ( schedule_spec [ :params ] || { } ) schedule . cron = schedule_spec [ :cron ] if schedule_spec [ :cron ] schedule . after = schedule_cache [ schedule_spec [ :after ] ] if schedule_spec [ :after ] schedule . hidden_params = schedule_spec [ :hidden_params ] || { } if process_spec . type != :dataload schedule . executable = schedule_spec [ :executable ] || ( process_spec . type == :ruby ? 'main.rb' : 'main.grf' ) end schedule . reschedule = schedule_spec [ :reschedule ] schedule . name = schedule_spec [ :name ] schedule . save schedule_cache [ schedule . name ] = schedule results << { state : :changed , process : remote_process , schedule : schedule } end end diff [ :removed ] . each do | removed_schedule | GoodData . logger . info ( "Removing schedule #{removed_schedule[:name]}" ) removed_schedule [ :remote_schedule ] . delete results << { state : :removed , process : removed_schedule . process , schedule : removed_schedule } end results end
Clones project along with etl and schedules.
Clones project along with etl and schedules .
# rubocop:disable Metrics/BlockLength
from_project
Object to be cloned from. Can be either segment in which case we take the master, client in which case we take its project, string in which case we treat is as an project object or directly project
Object to be cloned from . Can be either segment in which case we take the master client in which case we take its project string in which case we treat is as an project object or directly project
to_project
client
GoodData client to be used for connection
GoodData client to be used for connection