Skip to content

Commit

Permalink
Maintenance: Refactored script for background service processing (a.k…
Browse files Browse the repository at this point in the history
….a scheduler).

- Added possibility to control process forking for better scalability via env variables. See `script/background-services.rb -h` for details.
- New handle `script/background-services.rb` does not daemonize any more.
- `script/scheduler.rb` is now deprecated and will be removed with Zammad 6.

Co-authored-by: Martin Gruner <[email protected]>
Co-authored-by: Mantas Masalskis <[email protected]>
  • Loading branch information
mantas and mgruner committed Jun 1, 2022
1 parent 3cd6f24 commit 9ef701e
Show file tree
Hide file tree
Showing 67 changed files with 2,011 additions and 1,180 deletions.
5 changes: 0 additions & 5 deletions .rubocop/todo.rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ RSpec/ContextWording:
- 'spec/models/object_manager/attribute_spec.rb'
- 'spec/models/overview_spec.rb'
- 'spec/models/role_spec.rb'
- 'spec/models/scheduler_spec.rb'
- 'spec/models/smime_certificate_spec.rb'
- 'spec/models/store/file_spec.rb'
- 'spec/models/store/provider/file_spec.rb'
Expand Down Expand Up @@ -234,7 +233,6 @@ RSpec/ExampleLength:
- 'spec/models/recent_view_spec.rb'
- 'spec/models/role_group_spec.rb'
- 'spec/models/role_spec.rb'
- 'spec/models/scheduler_spec.rb'
- 'spec/models/sla/has_escalation_calculation_impact_examples.rb'
- 'spec/models/store/file_spec.rb'
- 'spec/models/store/provider/file_spec.rb'
Expand Down Expand Up @@ -324,7 +322,6 @@ RSpec/InstanceVariable:
Exclude:
- 'spec/lib/notification_factory/renderer_spec.rb'
- 'spec/models/import_job_spec.rb'
- 'spec/models/scheduler_spec.rb'
- 'spec/requests/api_auth_from_spec.rb'
- 'spec/requests/integration/monitoring_spec.rb'
- 'spec/requests/integration/sipgate_spec.rb'
Expand Down Expand Up @@ -417,7 +414,6 @@ RSpec/MessageSpies:
- 'spec/models/import_job_spec.rb'
- 'spec/models/object_manager/attribute/validation/backend_spec.rb'
- 'spec/models/object_manager/attribute/validation_spec.rb'
- 'spec/models/scheduler_spec.rb'
- 'spec/models/ticket/number_spec.rb'
- 'spec/models/ticket_spec.rb'
- 'spec/models/user_spec.rb'
Expand Down Expand Up @@ -489,7 +485,6 @@ RSpec/MultipleExpectations:
- 'spec/models/object_manager/attribute/validation_spec.rb'
- 'spec/models/object_manager/attribute_spec.rb'
- 'spec/models/overview_spec.rb'
- 'spec/models/scheduler_spec.rb'
- 'spec/models/session_spec.rb'
- 'spec/models/sla/has_escalation_calculation_impact_examples.rb'
- 'spec/models/smime_certificate_spec.rb'
Expand Down
5 changes: 0 additions & 5 deletions .rubocop/todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ Metrics/AbcSize:
- 'app/models/report.rb'
- 'app/models/role.rb'
- 'app/models/role/assets.rb'
- 'app/models/scheduler.rb'
- 'app/models/setting.rb'
- 'app/models/sla/assets.rb'
- 'app/models/smime_certificate.rb'
Expand Down Expand Up @@ -383,7 +382,6 @@ Metrics/BlockLength:
- 'app/models/organization/search.rb'
- 'app/models/package.rb'
- 'app/models/package/migration.rb'
- 'app/models/scheduler.rb'
- 'app/models/ticket.rb'
- 'app/models/ticket/overviews.rb'
- 'app/models/ticket/search.rb'
Expand Down Expand Up @@ -546,7 +544,6 @@ Metrics/CyclomaticComplexity:
- 'app/models/recent_view/assets.rb'
- 'app/models/role.rb'
- 'app/models/role/assets.rb'
- 'app/models/scheduler.rb'
- 'app/models/setting.rb'
- 'app/models/sla/assets.rb'
- 'app/models/store.rb'
Expand Down Expand Up @@ -774,7 +771,6 @@ Metrics/PerceivedComplexity:
- 'app/models/recent_view/assets.rb'
- 'app/models/role.rb'
- 'app/models/role/assets.rb'
- 'app/models/scheduler.rb'
- 'app/models/setting.rb'
- 'app/models/sla/assets.rb'
- 'app/models/taskbar.rb'
Expand Down Expand Up @@ -913,7 +909,6 @@ Style/OptionalBooleanParameter:
- 'app/models/karma/activity_log.rb'
- 'app/models/object_manager/attribute.rb'
- 'app/models/package.rb'
- 'app/models/scheduler.rb'
- 'app/models/setting.rb'
- 'app/models/store/file.rb'
- 'app/models/text_module.rb'
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: bundle exec script/rails server -b ${ZAMMAD_BIND_IP:=127.0.0.1} -p ${ZAMMAD_RAILS_PORT:=3000}
websocket: bundle exec script/websocket-server.rb -b ${ZAMMAD_BIND_IP:=127.0.0.1} -p ${ZAMMAD_WEBSOCKET_PORT:=6042} start
worker: bundle exec script/scheduler.rb start -t
worker: bundle exec script/background-services.rb start
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vite: bin/vite dev
web: bundle exec script/rails server -b ${ZAMMAD_BIND_IP:=127.0.0.1} -p ${ZAMMAD_RAILS_PORT:=3000}
websocket: bundle exec script/websocket-server.rb -b ${ZAMMAD_BIND_IP:=127.0.0.1} -p ${ZAMMAD_WEBSOCKET_PORT:=6042} start
worker: bundle exec script/scheduler.rb start -t
worker: bundle exec script/background-services.rb start
28 changes: 28 additions & 0 deletions app/models/import_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/

class ImportJob < ApplicationModel
extend ::Mixin::StartFinishLogger

store :payload
store :result
Expand Down Expand Up @@ -161,4 +162,31 @@ def self.backends
true
end || []
end

# Checks for killed import jobs and marks them as finished and adds a note.
#
# @param [ActiveSupport::TimeWithZone] after the time the cleanup was started
#
# @example
# Scheduler.cleanup_import_jobs(TimeZone.now)
#
# return [nil]
def self.cleanup_import_jobs(after)
log_start_finish(:info, "Cleanup of left over import jobs #{after}") do
error = __('Interrupted by scheduler restart. Please restart manually or wait until the next execution time.').freeze

# we need to exclude jobs that were updated at or since we started
# cleaning up (via the #reschedule? call) because they might
# were started `.delay`-ed and are flagged for restart
ImportJob.running.where('updated_at < ?', after).each do |job|

job.update!(
finished_at: after,
result: {
error: error
}
)
end
end
end
end
Loading

0 comments on commit 9ef701e

Please sign in to comment.