canvas_sync 0.16.4 → 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +235 -151
  3. data/app/controllers/{api → canvas_sync/api}/v1/health_check_controller.rb +1 -1
  4. data/app/controllers/canvas_sync/api/v1/live_events_controller.rb +122 -0
  5. data/app/models/canvas_sync/sync_batch.rb +5 -0
  6. data/config/initializers/apartment.rb +10 -1
  7. data/config/routes.rb +7 -0
  8. data/db/migrate/20170915210836_create_canvas_sync_job_log.rb +12 -31
  9. data/db/migrate/20180725155729_add_job_id_to_canvas_sync_job_logs.rb +4 -13
  10. data/db/migrate/20190916154829_add_fork_count_to_canvas_sync_job_logs.rb +3 -11
  11. data/db/migrate/20201018210836_create_canvas_sync_sync_batches.rb +11 -0
  12. data/db/migrate/20201030210836_add_full_sync_to_canvas_sync_sync_batch.rb +7 -0
  13. data/lib/canvas_sync/batch_processor.rb +41 -0
  14. data/lib/canvas_sync/concerns/ability_helper.rb +72 -0
  15. data/lib/canvas_sync/concerns/account/ancestry.rb +2 -0
  16. data/lib/canvas_sync/concerns/account/base.rb +15 -0
  17. data/lib/canvas_sync/concerns/api_syncable.rb +17 -10
  18. data/lib/canvas_sync/concerns/live_event_sync.rb +46 -0
  19. data/lib/canvas_sync/concerns/role/base.rb +57 -0
  20. data/lib/canvas_sync/concerns/sync_mapping.rb +120 -0
  21. data/lib/canvas_sync/engine.rb +80 -0
  22. data/lib/canvas_sync/generators/install_generator.rb +1 -0
  23. data/lib/canvas_sync/generators/install_live_events_generator.rb +0 -1
  24. data/lib/canvas_sync/generators/templates/migrations/create_content_migrations.rb +24 -0
  25. data/lib/canvas_sync/generators/templates/migrations/create_course_nicknames.rb +17 -0
  26. data/lib/canvas_sync/generators/templates/migrations/create_grading_period_groups.rb +18 -0
  27. data/lib/canvas_sync/generators/templates/migrations/create_grading_periods.rb +22 -0
  28. data/lib/canvas_sync/generators/templates/migrations/create_learning_outcome_results.rb +46 -0
  29. data/lib/canvas_sync/generators/templates/migrations/create_learning_outcomes.rb +30 -0
  30. data/lib/canvas_sync/generators/templates/migrations/create_rubric_assessments.rb +31 -0
  31. data/lib/canvas_sync/generators/templates/migrations/create_rubric_associations.rb +36 -0
  32. data/lib/canvas_sync/generators/templates/migrations/create_rubrics.rb +38 -0
  33. data/lib/canvas_sync/generators/templates/migrations/create_user_observers.rb +17 -0
  34. data/lib/canvas_sync/generators/templates/migrations/create_users.rb +0 -1
  35. data/lib/canvas_sync/generators/templates/models/account.rb +3 -0
  36. data/lib/canvas_sync/generators/templates/models/admin.rb +2 -0
  37. data/lib/canvas_sync/generators/templates/models/assignment.rb +3 -0
  38. data/lib/canvas_sync/generators/templates/models/assignment_group.rb +3 -0
  39. data/lib/canvas_sync/generators/templates/models/content_migration.rb +12 -0
  40. data/lib/canvas_sync/generators/templates/models/context_module.rb +3 -0
  41. data/lib/canvas_sync/generators/templates/models/context_module_item.rb +3 -0
  42. data/lib/canvas_sync/generators/templates/models/course.rb +11 -0
  43. data/lib/canvas_sync/generators/templates/models/course_nickname.rb +13 -0
  44. data/lib/canvas_sync/generators/templates/models/enrollment.rb +14 -0
  45. data/lib/canvas_sync/generators/templates/models/grading_period.rb +10 -0
  46. data/lib/canvas_sync/generators/templates/models/grading_period_group.rb +9 -0
  47. data/lib/canvas_sync/generators/templates/models/group.rb +2 -0
  48. data/lib/canvas_sync/generators/templates/models/group_membership.rb +2 -0
  49. data/lib/canvas_sync/generators/templates/models/learning_outcome.rb +24 -0
  50. data/lib/canvas_sync/generators/templates/models/learning_outcome_result.rb +48 -0
  51. data/lib/canvas_sync/generators/templates/models/pseudonym.rb +2 -0
  52. data/lib/canvas_sync/generators/templates/models/role.rb +2 -0
  53. data/lib/canvas_sync/generators/templates/models/rubric.rb +29 -0
  54. data/lib/canvas_sync/generators/templates/models/rubric_assessment.rb +17 -0
  55. data/lib/canvas_sync/generators/templates/models/rubric_association.rb +14 -0
  56. data/lib/canvas_sync/generators/templates/models/section.rb +9 -0
  57. data/lib/canvas_sync/generators/templates/models/submission.rb +3 -0
  58. data/lib/canvas_sync/generators/templates/models/term.rb +3 -0
  59. data/lib/canvas_sync/generators/templates/models/user.rb +11 -0
  60. data/lib/canvas_sync/generators/templates/models/user_observer.rb +13 -0
  61. data/lib/canvas_sync/generators/templates/services/live_events/assignment_event.rb +1 -1
  62. data/lib/canvas_sync/generators/templates/services/live_events/assignment_group_event.rb +1 -1
  63. data/lib/canvas_sync/generators/templates/services/live_events/course_event.rb +1 -3
  64. data/lib/canvas_sync/generators/templates/services/live_events/course_section_event.rb +1 -1
  65. data/lib/canvas_sync/generators/templates/services/live_events/enrollment_event.rb +1 -1
  66. data/lib/canvas_sync/generators/templates/services/live_events/grade_event.rb +1 -1
  67. data/lib/canvas_sync/generators/templates/services/live_events/module_event.rb +1 -1
  68. data/lib/canvas_sync/generators/templates/services/live_events/module_item_event.rb +1 -1
  69. data/lib/canvas_sync/generators/templates/services/live_events/submission_event.rb +1 -1
  70. data/lib/canvas_sync/generators/templates/services/live_events/syllabus_event.rb +1 -1
  71. data/lib/canvas_sync/generators/templates/services/live_events/user_event.rb +1 -3
  72. data/lib/canvas_sync/importers/bulk_importer.rb +138 -31
  73. data/lib/canvas_sync/job.rb +7 -5
  74. data/lib/canvas_sync/job_batches/active_job.rb +108 -0
  75. data/lib/canvas_sync/job_batches/batch.rb +543 -0
  76. data/lib/canvas_sync/job_batches/callback.rb +149 -0
  77. data/lib/canvas_sync/job_batches/chain_builder.rb +249 -0
  78. data/lib/canvas_sync/job_batches/context_hash.rb +159 -0
  79. data/lib/canvas_sync/job_batches/hier_batch_ids.lua +25 -0
  80. data/lib/canvas_sync/job_batches/jobs/base_job.rb +7 -0
  81. data/lib/canvas_sync/job_batches/jobs/concurrent_batch_job.rb +22 -0
  82. data/lib/canvas_sync/job_batches/jobs/managed_batch_job.rb +170 -0
  83. data/lib/canvas_sync/job_batches/jobs/serial_batch_job.rb +22 -0
  84. data/lib/canvas_sync/job_batches/pool.rb +245 -0
  85. data/lib/canvas_sync/job_batches/pool_refill.lua +47 -0
  86. data/lib/canvas_sync/job_batches/redis_model.rb +69 -0
  87. data/lib/canvas_sync/job_batches/redis_script.rb +163 -0
  88. data/lib/canvas_sync/job_batches/schedule_callback.lua +14 -0
  89. data/lib/canvas_sync/job_batches/sidekiq/web/batches_assets/css/styles.less +182 -0
  90. data/lib/canvas_sync/job_batches/sidekiq/web/batches_assets/js/batch_tree.js +108 -0
  91. data/lib/canvas_sync/job_batches/sidekiq/web/batches_assets/js/util.js +2 -0
  92. data/lib/canvas_sync/job_batches/sidekiq/web/helpers.rb +41 -0
  93. data/lib/canvas_sync/job_batches/sidekiq/web/views/_batch_tree.erb +6 -0
  94. data/lib/canvas_sync/job_batches/sidekiq/web/views/_batches_table.erb +44 -0
  95. data/lib/canvas_sync/job_batches/sidekiq/web/views/_common.erb +13 -0
  96. data/lib/canvas_sync/job_batches/sidekiq/web/views/_jobs_table.erb +21 -0
  97. data/lib/canvas_sync/job_batches/sidekiq/web/views/_pagination.erb +26 -0
  98. data/lib/canvas_sync/job_batches/sidekiq/web/views/batch.erb +81 -0
  99. data/lib/canvas_sync/job_batches/sidekiq/web/views/batches.erb +23 -0
  100. data/lib/canvas_sync/job_batches/sidekiq/web/views/pool.erb +137 -0
  101. data/lib/canvas_sync/job_batches/sidekiq/web/views/pools.erb +47 -0
  102. data/lib/canvas_sync/job_batches/sidekiq/web.rb +218 -0
  103. data/lib/canvas_sync/job_batches/sidekiq.rb +136 -0
  104. data/lib/canvas_sync/job_batches/status.rb +91 -0
  105. data/lib/canvas_sync/jobs/begin_sync_chain_job.rb +99 -0
  106. data/lib/canvas_sync/jobs/canvas_process_waiter.rb +41 -0
  107. data/lib/canvas_sync/jobs/report_checker.rb +70 -8
  108. data/lib/canvas_sync/jobs/report_processor_job.rb +4 -7
  109. data/lib/canvas_sync/jobs/report_starter.rb +34 -20
  110. data/lib/canvas_sync/jobs/sync_accounts_job.rb +3 -5
  111. data/lib/canvas_sync/jobs/sync_admins_job.rb +2 -4
  112. data/lib/canvas_sync/jobs/sync_assignment_groups_job.rb +2 -4
  113. data/lib/canvas_sync/jobs/sync_assignments_job.rb +2 -4
  114. data/lib/canvas_sync/jobs/sync_content_migrations_job.rb +20 -0
  115. data/lib/canvas_sync/jobs/sync_context_module_items_job.rb +2 -4
  116. data/lib/canvas_sync/jobs/sync_context_modules_job.rb +2 -4
  117. data/lib/canvas_sync/jobs/sync_provisioning_report_job.rb +16 -50
  118. data/lib/canvas_sync/jobs/sync_roles_job.rb +2 -5
  119. data/lib/canvas_sync/jobs/sync_rubric_assessments_job.rb +15 -0
  120. data/lib/canvas_sync/jobs/sync_rubric_associations_job.rb +15 -0
  121. data/lib/canvas_sync/jobs/sync_rubrics_job.rb +15 -0
  122. data/lib/canvas_sync/jobs/sync_simple_table_job.rb +11 -32
  123. data/lib/canvas_sync/jobs/sync_submissions_job.rb +6 -4
  124. data/lib/canvas_sync/jobs/sync_terms_job.rb +9 -8
  125. data/lib/canvas_sync/jobs/term_batches_job.rb +50 -0
  126. data/lib/canvas_sync/{generators/templates/services/live_events/base_event.rb → live_events/base_handler.rb} +6 -10
  127. data/lib/canvas_sync/live_events/process_event_job.rb +26 -0
  128. data/lib/canvas_sync/live_events.rb +38 -0
  129. data/lib/canvas_sync/misc_helper.rb +63 -0
  130. data/lib/canvas_sync/processors/assignment_groups_processor.rb +3 -8
  131. data/lib/canvas_sync/processors/assignments_processor.rb +3 -8
  132. data/lib/canvas_sync/processors/content_migrations_processor.rb +19 -0
  133. data/lib/canvas_sync/processors/context_module_items_processor.rb +3 -8
  134. data/lib/canvas_sync/processors/context_modules_processor.rb +3 -8
  135. data/lib/canvas_sync/processors/model_mappings.yml +420 -0
  136. data/lib/canvas_sync/processors/normal_processor.rb +3 -3
  137. data/lib/canvas_sync/processors/provisioning_report_processor.rb +42 -55
  138. data/lib/canvas_sync/processors/report_processor.rb +15 -9
  139. data/lib/canvas_sync/processors/rubric_assessments_processor.rb +19 -0
  140. data/lib/canvas_sync/processors/rubric_associations_processor.rb +19 -0
  141. data/lib/canvas_sync/processors/rubrics_processor.rb +19 -0
  142. data/lib/canvas_sync/processors/submissions_processor.rb +3 -8
  143. data/lib/canvas_sync/record.rb +103 -0
  144. data/lib/canvas_sync/version.rb +1 -1
  145. data/lib/canvas_sync.rb +124 -125
  146. data/spec/canvas_sync/canvas_sync_spec.rb +224 -155
  147. data/spec/canvas_sync/jobs/canvas_process_waiter_spec.rb +34 -0
  148. data/spec/canvas_sync/jobs/job_spec.rb +9 -17
  149. data/spec/canvas_sync/jobs/report_checker_spec.rb +1 -3
  150. data/spec/canvas_sync/jobs/report_processor_job_spec.rb +0 -3
  151. data/spec/canvas_sync/jobs/report_starter_spec.rb +19 -28
  152. data/spec/canvas_sync/jobs/sync_admins_job_spec.rb +1 -4
  153. data/spec/canvas_sync/jobs/sync_assignment_groups_job_spec.rb +2 -1
  154. data/spec/canvas_sync/jobs/sync_assignments_job_spec.rb +3 -2
  155. data/spec/canvas_sync/jobs/sync_content_migrations_job_spec.rb +30 -0
  156. data/spec/canvas_sync/jobs/sync_context_module_items_job_spec.rb +3 -2
  157. data/spec/canvas_sync/jobs/sync_context_modules_job_spec.rb +3 -2
  158. data/spec/canvas_sync/jobs/sync_provisioning_report_job_spec.rb +7 -41
  159. data/spec/canvas_sync/jobs/sync_roles_job_spec.rb +1 -4
  160. data/spec/canvas_sync/jobs/sync_simple_table_job_spec.rb +5 -12
  161. data/spec/canvas_sync/jobs/sync_submissions_job_spec.rb +8 -2
  162. data/spec/canvas_sync/jobs/sync_terms_job_spec.rb +1 -4
  163. data/spec/canvas_sync/live_events/live_event_sync_spec.rb +27 -0
  164. data/spec/canvas_sync/live_events/live_events_controller_spec.rb +54 -0
  165. data/spec/canvas_sync/live_events/process_event_job_spec.rb +38 -0
  166. data/spec/canvas_sync/misc_helper_spec.rb +58 -0
  167. data/spec/canvas_sync/models/assignment_spec.rb +1 -1
  168. data/spec/canvas_sync/processors/content_migrations_processor_spec.rb +13 -0
  169. data/spec/canvas_sync/processors/provisioning_report_processor_spec.rb +101 -1
  170. data/spec/canvas_sync/processors/rubric_assessments_spec.rb +16 -0
  171. data/spec/canvas_sync/processors/rubric_associations_spec.rb +16 -0
  172. data/spec/canvas_sync/processors/rubrics_processor_spec.rb +17 -0
  173. data/spec/dummy/app/models/account.rb +6 -0
  174. data/spec/dummy/app/models/admin.rb +2 -0
  175. data/spec/dummy/app/models/assignment.rb +3 -0
  176. data/spec/dummy/app/models/assignment_group.rb +3 -0
  177. data/spec/dummy/app/models/content_migration.rb +18 -0
  178. data/spec/dummy/app/models/context_module.rb +3 -0
  179. data/spec/dummy/app/models/context_module_item.rb +3 -0
  180. data/spec/dummy/app/models/course.rb +11 -0
  181. data/spec/dummy/app/models/course_nickname.rb +19 -0
  182. data/spec/dummy/app/models/enrollment.rb +14 -0
  183. data/spec/dummy/app/models/grading_period.rb +16 -0
  184. data/spec/dummy/app/models/grading_period_group.rb +15 -0
  185. data/spec/dummy/app/models/group.rb +2 -0
  186. data/spec/dummy/app/models/group_membership.rb +2 -0
  187. data/spec/dummy/app/models/learning_outcome.rb +30 -0
  188. data/spec/dummy/app/models/learning_outcome_result.rb +54 -0
  189. data/spec/dummy/app/models/pseudonym.rb +16 -0
  190. data/spec/dummy/app/models/role.rb +2 -0
  191. data/spec/dummy/app/models/rubric.rb +35 -0
  192. data/spec/dummy/app/models/rubric_assessment.rb +22 -0
  193. data/spec/dummy/app/models/rubric_association.rb +20 -0
  194. data/spec/dummy/app/models/section.rb +9 -0
  195. data/spec/dummy/app/models/submission.rb +4 -0
  196. data/spec/dummy/app/models/term.rb +3 -0
  197. data/spec/dummy/app/models/user.rb +11 -0
  198. data/spec/dummy/app/models/user_observer.rb +19 -0
  199. data/spec/dummy/app/services/live_events/assignment_event.rb +1 -1
  200. data/spec/dummy/app/services/live_events/course_event.rb +1 -3
  201. data/spec/dummy/app/services/live_events/course_section_event.rb +1 -1
  202. data/spec/dummy/app/services/live_events/enrollment_event.rb +1 -1
  203. data/spec/dummy/app/services/live_events/grade_event.rb +1 -1
  204. data/spec/dummy/app/services/live_events/module_event.rb +1 -1
  205. data/spec/dummy/app/services/live_events/module_item_event.rb +1 -1
  206. data/spec/dummy/app/services/live_events/submission_event.rb +1 -1
  207. data/spec/dummy/app/services/live_events/syllabus_event.rb +1 -1
  208. data/spec/dummy/app/services/live_events/user_event.rb +1 -3
  209. data/spec/dummy/config/environments/test.rb +2 -0
  210. data/spec/dummy/config/routes.rb +1 -0
  211. data/spec/dummy/db/migrate/20201016181346_create_pseudonyms.rb +24 -0
  212. data/spec/dummy/db/migrate/20210907233329_create_user_observers.rb +23 -0
  213. data/spec/dummy/db/migrate/20210907233330_create_grading_periods.rb +28 -0
  214. data/spec/dummy/db/migrate/20211001184920_create_grading_period_groups.rb +24 -0
  215. data/spec/dummy/db/migrate/20220308072643_create_content_migrations.rb +30 -0
  216. data/spec/dummy/db/migrate/20220712210559_create_learning_outcomes.rb +36 -0
  217. data/spec/dummy/db/migrate/{20190702203620_create_users.rb → 20220926221926_create_users.rb} +0 -1
  218. data/spec/dummy/db/migrate/20240408223326_create_course_nicknames.rb +23 -0
  219. data/spec/dummy/db/migrate/20240509105100_create_rubrics.rb +44 -0
  220. data/spec/dummy/db/migrate/20240510094100_create_rubric_associations.rb +42 -0
  221. data/spec/dummy/db/migrate/20240510101100_create_rubric_assessments.rb +37 -0
  222. data/spec/dummy/db/migrate/20240523101010_create_learning_outcome_results.rb +52 -0
  223. data/spec/dummy/db/schema.rb +244 -5
  224. data/spec/factories/user_factory.rb +0 -1
  225. data/spec/job_batching/active_job_spec.rb +107 -0
  226. data/spec/job_batching/batch_spec.rb +489 -0
  227. data/spec/job_batching/callback_spec.rb +38 -0
  228. data/spec/job_batching/context_hash_spec.rb +54 -0
  229. data/spec/job_batching/flow_spec.rb +82 -0
  230. data/spec/job_batching/integration/fail_then_succeed.rb +42 -0
  231. data/spec/job_batching/integration/integration.rb +57 -0
  232. data/spec/job_batching/integration/nested.rb +88 -0
  233. data/spec/job_batching/integration/simple.rb +47 -0
  234. data/spec/job_batching/integration/workflow.rb +134 -0
  235. data/spec/job_batching/integration_helper.rb +50 -0
  236. data/spec/job_batching/pool_spec.rb +161 -0
  237. data/spec/job_batching/sidekiq_spec.rb +125 -0
  238. data/spec/job_batching/status_spec.rb +76 -0
  239. data/spec/job_batching/support/base_job.rb +14 -0
  240. data/spec/job_batching/support/sample_callback.rb +2 -0
  241. data/spec/spec_helper.rb +17 -0
  242. data/spec/support/fixtures/reports/content_migrations.csv +3 -0
  243. data/spec/support/fixtures/reports/course_nicknames.csv +3 -0
  244. data/spec/support/fixtures/reports/grading_period_groups.csv +2 -0
  245. data/spec/support/fixtures/reports/grading_periods.csv +3 -0
  246. data/spec/support/fixtures/reports/learning_outcome_results.csv +3 -0
  247. data/spec/support/fixtures/reports/learning_outcomes.csv +3 -0
  248. data/spec/support/fixtures/reports/provisioning_csv_unzipped/courses.csv +3 -0
  249. data/spec/support/fixtures/reports/provisioning_csv_unzipped/users.csv +4 -0
  250. data/spec/support/fixtures/reports/rubric_assessments.csv +3 -0
  251. data/spec/support/fixtures/reports/rubric_associations.csv +3 -0
  252. data/spec/support/fixtures/reports/rubrics.csv +3 -0
  253. data/spec/support/fixtures/reports/user_observers.csv +3 -0
  254. data/spec/support/fixtures/reports/users.csv +3 -2
  255. data/spec/support/fixtures/reports/xlist.csv +1 -1
  256. metadata +329 -27
  257. data/app/controllers/api/v1/live_events_controller.rb +0 -18
  258. data/lib/canvas_sync/job_chain.rb +0 -57
  259. data/lib/canvas_sync/jobs/fork_gather.rb +0 -59
  260. data/spec/canvas_sync/jobs/fork_gather_spec.rb +0 -73
@@ -0,0 +1,57 @@
1
+ require 'integration_helper'
2
+
3
+ # Simple test of adding jobs to the current batch
4
+ # Batches:
5
+ # - Overall (TestWoker) + Another worker
6
+
7
+ class AnotherWorker
8
+ include Sidekiq::Worker
9
+
10
+ def perform
11
+ Sidekiq.logger.info "Another Worker"
12
+ end
13
+ end
14
+
15
+ class TestWorker
16
+ include Sidekiq::Worker
17
+
18
+ def perform
19
+ Sidekiq.logger.info "Test Worker"
20
+ if bid
21
+ batch.jobs do
22
+ AnotherWorker.perform_async
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ class MyCallback
29
+ def on_success(status, options)
30
+ Sidekiq.logger.info "Success #{options} #{status.data}"
31
+ end
32
+ alias_method :multi, :on_success
33
+
34
+ def on_complete(status, options)
35
+ Sidekiq.logger.info "Complete #{options} #{status.data}"
36
+ end
37
+ end
38
+
39
+ batch = CanvasSync::JobBatches::Batch.new
40
+ batch.description = 'Test batch'
41
+ batch.callback_queue = :default
42
+ batch.on(:success, 'MyCallback#on_success', to: 'success@gmail.com')
43
+ batch.on(:success, 'MyCallback#multi', to: 'success@gmail.com')
44
+ batch.on(:complete, MyCallback, to: 'complete@gmail.com')
45
+
46
+ batch.jobs do
47
+ 10.times do
48
+ TestWorker.perform_async
49
+ end
50
+ end
51
+ puts CanvasSync::JobBatches::Batch::Status.new(batch.bid).data
52
+
53
+ dump_redis_keys
54
+
55
+ Sidekiq::Worker.drain_all
56
+
57
+ dump_redis_keys
@@ -0,0 +1,88 @@
1
+ require 'integration_helper'
2
+
3
+ # Tests deep nesting of batches
4
+ # Batches:
5
+ # - Overall (Worker 1)
6
+ # - Worker 2
7
+ # - Worker 3
8
+ # - Worker 4
9
+
10
+ class Worker1
11
+ include Sidekiq::Worker
12
+
13
+ def perform
14
+ Sidekiq.logger.info "Work1"
15
+ batch = CanvasSync::JobBatches::Batch.new
16
+ batch.on(:success, Worker2)
17
+ batch.jobs do
18
+ Worker2.perform_async
19
+ end
20
+ end
21
+ end
22
+
23
+ class Worker2
24
+ include Sidekiq::Worker
25
+
26
+ def perform
27
+ Sidekiq.logger.info "Work2"
28
+ batch = CanvasSync::JobBatches::Batch.new
29
+ batch.on(:success, Worker3)
30
+ batch.jobs do
31
+ Worker3.perform_async
32
+ end
33
+ end
34
+
35
+ def on_success status, opts
36
+ Sidekiq.logger.info "Worker 2 Success"
37
+ end
38
+ end
39
+
40
+ class Worker3
41
+ include Sidekiq::Worker
42
+
43
+ def perform
44
+ Sidekiq.logger.info "Work3"
45
+ batch = CanvasSync::JobBatches::Batch.new
46
+ batch.on(:success, Worker4)
47
+ batch.jobs do
48
+ Worker4.perform_async
49
+ end
50
+ end
51
+
52
+ def on_success status, opts
53
+ Sidekiq.logger.info "Worker 3 Success"
54
+ end
55
+ end
56
+
57
+ class Worker4
58
+ include Sidekiq::Worker
59
+
60
+ def perform
61
+ Sidekiq.logger.info "Work4"
62
+ end
63
+
64
+ def on_success status, opts
65
+ Sidekiq.logger.info "Worker 4 Success"
66
+ end
67
+ end
68
+
69
+
70
+ class SomeClass
71
+ def on_complete(status, options)
72
+ Sidekiq.logger.info "Overall Complete #{options} #{status.data}"
73
+ end
74
+ def on_success(status, options)
75
+ Sidekiq.logger.info "Overall Success #{options} #{status.data}"
76
+ end
77
+ end
78
+ batch = CanvasSync::JobBatches::Batch.new
79
+ batch.on(:success, SomeClass, 'uid' => 3)
80
+ batch.on(:complete, SomeClass, 'uid' => 3)
81
+ batch.jobs do
82
+ Worker1.perform_async
83
+ end
84
+
85
+ puts "Overall bid #{batch.bid}"
86
+
87
+ output, keys = process_tests
88
+ overall_tests output, keys
@@ -0,0 +1,47 @@
1
+ require 'integration_helper'
2
+
3
+ # Simple nested batch without callbacks
4
+ # Batches:
5
+ # - Overall (Worker1)
6
+ # - Worker2
7
+
8
+ class Worker1
9
+ include Sidekiq::Worker
10
+
11
+ def perform
12
+ Sidekiq.logger.info "Work1"
13
+ batch = CanvasSync::JobBatches::Batch.new
14
+ batch.jobs do
15
+ Worker2.perform_async
16
+ end
17
+ end
18
+ end
19
+
20
+ class Worker2
21
+ include Sidekiq::Worker
22
+
23
+ def perform
24
+ Sidekiq.logger.info "Work2"
25
+ end
26
+ end
27
+
28
+ class SomeClass
29
+ def on_complete(status, options)
30
+ Sidekiq.logger.info "Overall Complete #{options} #{status.data}"
31
+ end
32
+ def on_success(status, options)
33
+ Sidekiq.logger.info "Overall Success #{options} #{status.data}"
34
+ end
35
+ end
36
+
37
+ batch = CanvasSync::JobBatches::Batch.new
38
+ batch.on(:success, SomeClass)
39
+ batch.on(:complete, SomeClass)
40
+ batch.jobs do
41
+ Worker1.perform_async
42
+ end
43
+
44
+ puts "Overall bid #{batch.bid}"
45
+
46
+ output, keys = process_tests
47
+ overall_tests output, keys
@@ -0,0 +1,134 @@
1
+ require 'integration_helper'
2
+
3
+ # Complex workflow with sequential and nested
4
+ # Also test sub batches without callbacks
5
+ # Batches:
6
+ # - Overall
7
+ # - Worker1
8
+ # - Worker3
9
+ # - Worker2 + Worker3
10
+ # - Worker1
11
+ # - Worker3
12
+ # - Worker4
13
+ # - Worker5
14
+
15
+ class Callbacks
16
+ def worker1 status, opts
17
+ Sidekiq.logger.info "Success 1 #{status.data}"
18
+
19
+ overall = CanvasSync::JobBatches::Batch.new status.parent_bid
20
+ overall.jobs do
21
+ batch = CanvasSync::JobBatches::Batch.new
22
+ batch.on(:success, "Callbacks#worker2")
23
+ batch.jobs do
24
+ Worker2.perform_async
25
+ end
26
+ end
27
+ end
28
+
29
+ def worker2 status, opts
30
+ Sidekiq.logger.info "Success 2 #{status.data}"
31
+ overall = CanvasSync::JobBatches::Batch.new status.parent_bid
32
+ overall.jobs do
33
+ batch = CanvasSync::JobBatches::Batch.new
34
+ batch.on(:success, "Callbacks#worker4")
35
+ batch.jobs do
36
+ Worker4.perform_async
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ def worker4 status, opts
43
+ Sidekiq.logger.info "Success 4 #{status.data}"
44
+ overall = CanvasSync::JobBatches::Batch.new status.parent_bid
45
+ overall.jobs do
46
+ batch = CanvasSync::JobBatches::Batch.new
47
+ batch.on(:success, "Callbacks#worker5")
48
+ batch.jobs do
49
+ Worker5.perform_async
50
+ end
51
+ end
52
+ end
53
+
54
+ def worker5 status, opts
55
+ Sidekiq.logger.info "Success 5 #{status.data}"
56
+ end
57
+ end
58
+
59
+ class Worker1
60
+ include Sidekiq::Worker
61
+
62
+ def perform
63
+ Sidekiq.logger.info "Work 1"
64
+ batch = CanvasSync::JobBatches::Batch.new
65
+ batch.jobs do
66
+ Worker3.perform_async
67
+ end
68
+ end
69
+ end
70
+
71
+ class Worker2
72
+ include Sidekiq::Worker
73
+
74
+ def perform
75
+ Sidekiq.logger.info "Work 2"
76
+ if bid
77
+ batch.jobs do
78
+ Worker3.perform_async
79
+ end
80
+ newb = CanvasSync::JobBatches::Batch.new
81
+ newb.jobs do
82
+ Worker1.perform_async
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ class Worker3
89
+ include Sidekiq::Worker
90
+ def perform
91
+ Sidekiq.logger.info "Work 3"
92
+ end
93
+ end
94
+
95
+ class Worker4
96
+ include Sidekiq::Worker
97
+ def perform
98
+ Sidekiq.logger.info "Work 4"
99
+ end
100
+ end
101
+
102
+ class Worker5
103
+ include Sidekiq::Worker
104
+ def perform
105
+ Sidekiq.logger.info "Work 5"
106
+ end
107
+ end
108
+
109
+ class MyCallback
110
+ def on_success(status, options)
111
+ Sidekiq.logger.info "Overall Success #{options} #{status.data}"
112
+ end
113
+ alias_method :multi, :on_success
114
+
115
+ def on_complete(status, options)
116
+ Sidekiq.logger.info "Overall Complete #{options} #{status.data}"
117
+ end
118
+ end
119
+
120
+ overall = CanvasSync::JobBatches::Batch.new
121
+ overall.on(:success, MyCallback, to: 'success@gmail.com')
122
+ overall.on(:complete, MyCallback, to: 'success@gmail.com')
123
+ overall.jobs do
124
+ batch1 = CanvasSync::JobBatches::Batch.new
125
+ batch1.on(:success, "Callbacks#worker1")
126
+ batch1.jobs do
127
+ Worker1.perform_async
128
+ end
129
+ end
130
+
131
+ puts "Overall bid #{overall.bid}"
132
+
133
+ output, keys = process_tests
134
+ overall_tests output, keys
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'sidekiq/testing'
3
+
4
+ Sidekiq::Testing.server_middleware do |chain|
5
+ chain.add CanvasSync::JobBatches::Sidekiq::ServerMiddleware
6
+ end
7
+
8
+ CanvasSync.redis { |r| r.flushdb }
9
+
10
+ def redis_keys
11
+ CanvasSync.redis { |r| r.keys('BID-*') }
12
+ end
13
+
14
+ def dump_redis_keys
15
+ puts redis_keys.inspect
16
+ end
17
+
18
+ def process_tests
19
+ out_buf = StringIO.new
20
+ Sidekiq.logger = Logger.new out_buf
21
+
22
+ # Sidekiq.logger.level = :info
23
+
24
+ Sidekiq::Worker.drain_all
25
+
26
+ output = out_buf.string
27
+ keys = redis_keys
28
+ puts out_buf.string
29
+
30
+ [output, keys]
31
+ end
32
+
33
+ def overall_tests(output, keys, file: nil)
34
+ test_name = "Batch Integration Test"
35
+ test_name = File.basename(file, ".*") if file
36
+
37
+ Rspec.describe test_name do
38
+ it "runs overall complete callback" do
39
+ expect(output).to include "Overall Complete"
40
+ end
41
+
42
+ it "runs overall success callback" do
43
+ expect(output).to include "Overall Success"
44
+ end
45
+
46
+ it "cleans redis keys" do
47
+ expect(keys).to eq([])
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,161 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CanvasSync::JobBatches::Pool do
4
+ include ActiveJob::TestHelper
5
+
6
+ subject { described_class.new(order: pool_order, concurrency: pool_concurrency) }
7
+
8
+ let(:pool) { subject }
9
+
10
+ let(:pool_order) { :fifo }
11
+ let(:pool_concurrency) { 2 }
12
+
13
+ describe '#initialize' do
14
+ subject { described_class }
15
+
16
+ it 'creates pid when called without it' do
17
+ expect(subject.new.pid).not_to be_nil
18
+ end
19
+
20
+ it 'reuses pid when called with it' do
21
+ batch = subject.new('dayPO5KxuRXXxw')
22
+ expect(batch.pid).to eq('dayPO5KxuRXXxw')
23
+ end
24
+ end
25
+
26
+ def load_pool(count = 3)
27
+ jobs = count.times.map do |i|
28
+ { job: "BatchTestJobBase", args: [1] }
29
+ end
30
+ subject.add_jobs(jobs, skip_refill: true)
31
+ end
32
+
33
+ describe "#job_checked_in" do
34
+ it "gets called" do
35
+ expect(CanvasSync::JobBatches::Pool).to receive(:job_checked_in).twice
36
+ load_pool
37
+ perform_enqueued_jobs do
38
+ pool.send :refill_allotment
39
+ Sidekiq::Worker.drain_all
40
+ end
41
+ end
42
+
43
+ it "refills the pool" do
44
+ expect(CanvasSync::JobBatches::Pool).to receive(:job_checked_in).and_call_original.exactly(3).times
45
+
46
+ load_pool
47
+ perform_enqueued_jobs do
48
+ pool.send :refill_allotment
49
+ Sidekiq::Worker.drain_all
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#cleanup_if_empty" do
55
+ it "cleans if pool is empty and allowed to close" do
56
+ expect(pool).to receive(:cleanup_redis)
57
+ pool.cleanup_if_empty
58
+ end
59
+
60
+ it "doesn't clean if pool has pending" do
61
+ load_pool
62
+ expect(pool).to_not receive(:cleanup_redis)
63
+ pool.cleanup_if_empty
64
+ end
65
+
66
+ it "doesn't clean if pool has active" do
67
+ subject.redis.hset("#{subject.send(:redis_key)}-active", "blocked", "{}")
68
+ expect(pool).to_not receive(:cleanup_redis)
69
+ pool.cleanup_if_empty
70
+ end
71
+
72
+ it "doesn't clean if pool has activating" do
73
+ subject.redis.hincrby(subject.send(:redis_key), "_active_count", 1)
74
+ expect(pool).to_not receive(:cleanup_redis)
75
+ pool.cleanup_if_empty
76
+ end
77
+
78
+ it "doesn't clean if pool is empty but is kept open" do
79
+ pool.keep_open!
80
+ expect(pool).to_not receive(:cleanup_redis)
81
+ pool.cleanup_if_empty
82
+ end
83
+
84
+ it "doesn't clean if pool is empty but clean_when_empty is false" do
85
+ subject.redis.hset(subject.send(:redis_key), "clean_when_empty", "false")
86
+ expect(pool).to_not receive(:cleanup_redis)
87
+ pool.cleanup_if_empty
88
+ end
89
+ end
90
+
91
+ shared_examples "basic pool tests" do
92
+ describe "#push_job_to_pool" do
93
+ it "adds a job to the pool" do
94
+ subject.send(:push_job_to_pool, { job: 'job1' })
95
+ expect(subject.pending_count).to eq(1)
96
+ end
97
+ end
98
+
99
+ describe "#refill_allotment" do
100
+ it "refills the pool with jobs" do
101
+ load_pool
102
+ expect(CanvasSync::JobBatches::ChainBuilder).to receive(:enqueue_job).twice
103
+ subject.send(:refill_allotment)
104
+ end
105
+
106
+ it "limits to the concurrency count" do
107
+ load_pool
108
+ expect(CanvasSync::JobBatches::ChainBuilder).to receive(:enqueue_job).twice
109
+ expect(subject.send(:refill_allotment)).to eql 2
110
+ expect(subject.pending_count).to eql 1
111
+ end
112
+
113
+ it "considers already active jobs against concurrency" do
114
+ load_pool
115
+ subject.redis.hset("#{subject.send(:redis_key)}-active", "blocked", "{}")
116
+ expect(CanvasSync::JobBatches::ChainBuilder).to receive(:enqueue_job).once
117
+ expect(subject.send(:refill_allotment)).to eql 2
118
+ expect(subject.pending_count).to eql 2
119
+ end
120
+
121
+ it "considers activating jobs against concurrency" do
122
+ load_pool
123
+ subject.redis.hincrby(subject.send(:redis_key), "_active_count", 1)
124
+ expect(CanvasSync::JobBatches::ChainBuilder).to receive(:enqueue_job).once
125
+ expect(subject.send(:refill_allotment)).to eql 2
126
+ expect(subject.pending_count).to eql 2
127
+ end
128
+
129
+ it "doesn't fail if the pool is gone" do
130
+ load_pool
131
+ subject.cleanup_redis
132
+ expect(CanvasSync::JobBatches::ChainBuilder).not_to receive(:enqueue_job)
133
+ expect(subject.send(:refill_allotment)).to eql -1
134
+ end
135
+
136
+ it "doesn't fail if the pool is empty" do
137
+ expect(subject.send(:refill_allotment)).to eql 0
138
+ end
139
+ end
140
+ end
141
+
142
+ context "FIFO Pool" do
143
+ let(:pool_order) { :fifo }
144
+ it_behaves_like "basic pool tests"
145
+ end
146
+
147
+ context "LIFO Pool" do
148
+ let(:pool_order) { :lifo }
149
+ it_behaves_like "basic pool tests"
150
+ end
151
+
152
+ context "Random Pool" do
153
+ let(:pool_order) { :random }
154
+ it_behaves_like "basic pool tests"
155
+ end
156
+
157
+ context "Priority Pool" do
158
+ let(:pool_order) { :priority }
159
+ it_behaves_like "basic pool tests"
160
+ end
161
+ end
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CanvasSync::JobBatches::Sidekiq do
4
+ describe CanvasSync::JobBatches::Sidekiq::ServerMiddleware do
5
+ context 'when without batch' do
6
+ it 'just yields' do
7
+ yielded = false
8
+ expect(CanvasSync::JobBatches::Batch).not_to receive(:process_successful_job)
9
+ expect(CanvasSync::JobBatches::Batch).not_to receive(:process_failed_job)
10
+ subject.call(nil, {}, nil) { yielded = true }
11
+ expect(yielded).to be_truthy
12
+ end
13
+ end
14
+
15
+ context 'when in batch' do
16
+ let(:bid) { 'SAMPLEBID' }
17
+ let(:jid) { 'SAMPLEJID' }
18
+
19
+ context 'when successful' do
20
+ it 'yields' do
21
+ yielded = false
22
+ subject.call(nil, { 'bid' => bid, 'jid' => jid }, nil) { yielded = true }
23
+ expect(yielded).to be_truthy
24
+ end
25
+
26
+ it 'calls process_successful_job' do
27
+ expect(CanvasSync::JobBatches::Batch).to receive(:process_successful_job).with(bid, nil)
28
+ subject.call(nil, { 'bid' => bid }, nil) {}
29
+ end
30
+ end
31
+
32
+ context 'when failed' do
33
+ it 'calls process_failed_job and reraises exception' do
34
+ reraised = false
35
+ expect(CanvasSync::JobBatches::Batch).to receive(:process_failed_job)
36
+ begin
37
+ subject.call(nil, { 'bid' => bid }, nil) { raise 'ERR' }
38
+ rescue
39
+ reraised = true
40
+ end
41
+ expect(reraised).to be_truthy
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe CanvasSync::JobBatches::Sidekiq::ClientMiddleware do
48
+ context 'when without batch' do
49
+ it 'just yields' do
50
+ yielded = false
51
+ expect(CanvasSync::JobBatches::Batch).not_to receive(:increment_job_queue)
52
+ subject.call(nil, {}, nil) { yielded = true }
53
+ expect(yielded).to be_truthy
54
+ end
55
+ end
56
+
57
+ context 'when in batch' do
58
+ let(:bid) { 'SAMPLEBID' }
59
+ let(:jid) { 'SAMPLEJID' }
60
+ before { Thread.current[CanvasSync::JobBatches::CURRENT_BATCH_THREAD_KEY] = CanvasSync::JobBatches::Batch.new(bid) }
61
+ after { Thread.current[CanvasSync::JobBatches::CURRENT_BATCH_THREAD_KEY] = nil }
62
+
63
+ it 'yields' do
64
+ yielded = false
65
+ subject.call(nil, { 'jid' => jid }, nil) { yielded = true }
66
+ expect(yielded).to be_truthy
67
+ end
68
+
69
+ it 'increments job queue' do
70
+ # expect(CanvasSync::JobBatches::Batch).to receive(:increment_job_queue).with(bid)
71
+ # subject.call(nil, { 'jid' => jid }, nil) {}
72
+ end
73
+
74
+ it 'assigns bid to msg' do
75
+ msg = { 'jid' => jid }
76
+ subject.call(nil, msg, nil) {}
77
+ expect(msg[:bid]).to eq(bid)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ RSpec.describe CanvasSync::JobBatches::Sidekiq do
84
+ let(:config) { class_double(Sidekiq) }
85
+ let(:client_middleware) { double(Sidekiq::Middleware::Chain) }
86
+
87
+ context 'client' do
88
+ it 'adds client middleware' do
89
+ expect(Sidekiq).to receive(:configure_client).and_yield(config)
90
+ expect(config).to receive(:client_middleware).and_yield(client_middleware)
91
+ expect(client_middleware).to receive(:add).with(CanvasSync::JobBatches::Sidekiq::ClientMiddleware)
92
+ CanvasSync::JobBatches::Sidekiq.configure
93
+ end
94
+ end
95
+
96
+ context 'server' do
97
+ let(:server_middleware) { double(Sidekiq::Middleware::Chain) }
98
+ let(:death_handlers) { double(Array) }
99
+
100
+ it 'adds client and server middleware' do
101
+ expect(Sidekiq).to receive(:configure_server).and_yield(config)
102
+ expect(config).to receive(:client_middleware).and_yield(client_middleware)
103
+ expect(config).to receive(:server_middleware).and_yield(server_middleware)
104
+ expect(config).to receive(:death_handlers).and_return(death_handlers)
105
+ expect(client_middleware).to receive(:add).with(CanvasSync::JobBatches::Sidekiq::ClientMiddleware)
106
+ expect(server_middleware).to receive(:add).with(CanvasSync::JobBatches::Sidekiq::ServerMiddleware)
107
+ expect(death_handlers).to receive(:<<)
108
+ CanvasSync::JobBatches::Sidekiq.configure
109
+ end
110
+ end
111
+
112
+ context 'worker' do
113
+ it 'defines method bid' do
114
+ expect(Sidekiq::Worker.instance_methods).to include(:bid)
115
+ end
116
+
117
+ it 'defines method batch' do
118
+ expect(Sidekiq::Worker.instance_methods).to include(:batch)
119
+ end
120
+
121
+ it 'defines method valid_within_batch?' do
122
+ expect(Sidekiq::Worker.instance_methods).to include(:valid_within_batch?)
123
+ end
124
+ end
125
+ end