2011-02-01 09:57:29 +08:00
#
2017-04-28 04:06:18 +08:00
# Copyright (C) 2011 - present Instructure, Inc.
2011-02-01 09:57:29 +08:00
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class SummaryMessageConsolidator
def self . process
SummaryMessageConsolidator . new . process
end
2012-09-18 12:33:36 +08:00
2011-02-01 09:57:29 +08:00
def initialize ( n = nil )
2013-03-08 08:08:47 +08:00
@logger = Rails . logger
2011-02-01 09:57:29 +08:00
end
2012-09-18 12:33:36 +08:00
2011-02-01 09:57:29 +08:00
def process
2013-03-13 23:11:14 +08:00
batch_ids = delayed_message_batch_ids
dm_id_batches = batch_ids . map do | batch_id |
dm_ids = delayed_message_ids_for_batch ( batch_id )
@logger . info ( " Scheduled summary with #{ dm_ids . length } messages for communication channel id #{ batch_id [ 'communication_channel_id' ] } and root account id #{ batch_id [ 'root_account_id' ] || 'null' } " )
dm_ids
2011-02-01 09:57:29 +08:00
end
2012-09-18 12:33:36 +08:00
dm_id_batches . in_groups_of ( Setting . get ( 'summary_message_consolidator_batch_size' , '500' ) . to_i , false ) do | batches |
2013-03-19 23:49:31 +08:00
DelayedMessage . where ( :id = > batches . flatten ) .
update_all ( :batched_at = > Time . now . utc , :workflow_state = > 'sent' , :updated_at = > Time . now . utc )
2012-09-18 12:33:36 +08:00
Delayed :: Batch . serial_batch do
batches . each do | dm_ids |
DelayedMessage . send_later_enqueue_args ( :summarize , { :priority = > Delayed :: LOWER_PRIORITY } , dm_ids )
end
end
end
dm_id_batches . size
2011-02-01 09:57:29 +08:00
end
2013-03-13 23:11:14 +08:00
def delayed_message_batch_ids
2013-04-03 01:53:08 +08:00
Shackles . activate ( :slave ) do
2013-03-13 23:11:14 +08:00
DelayedMessage . connection . select_all (
2017-03-15 01:48:55 +08:00
DelayedMessage . select ( 'communication_channel_id' ) . select ( 'root_account_id' ) . distinct .
2013-03-13 23:11:14 +08:00
where ( " workflow_state = ? AND send_at <= ? " , 'pending' , Time . now . to_s ( :db ) ) .
to_sql )
end
end
def delayed_message_ids_for_batch ( batch )
DelayedMessage .
where ( " workflow_state = ? AND send_at <= ? " , 'pending' , Time . now . to_s ( :db ) ) .
where ( batch ) . # hash condition will properly handle the case where root_account_id is null
2017-07-26 01:40:04 +08:00
pluck ( :id )
2013-03-13 23:11:14 +08:00
end
2011-02-01 09:57:29 +08:00
end