# Scheduling jobs from a plugin

**URL:** <https://meta.discourse.org/t/scheduling-jobs-from-a-plugin/40238>\
**Category:** Development\
**Created:** [February 26, 2016, 7:01pm UTC](https://meta.discourse.org/t/scheduling-jobs-from-a-plugin/40238 "2016-02-26T19:01:09Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![bletch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bletch/32/69088_2.png) [@bletch](https://meta.discourse.org/u/bletch)\
**Post date:** [June 7, 2023, 7:50pm UTC](https://meta.discourse.org/t/scheduling-jobs-from-a-plugin/40238/12 "2023-06-07T19:50:47Z")

</div>

It’s been a while - 5 years apparently but from memory…

The job code, for example:

```ruby
# school-points\jobs\deferred_topic_creation.rb
module Jobs
  class DeferredTopicCreation < Jobs::Base
    def execute(args)
      creator_user_id = args[:creator_user_id]
      custom_fields = {
        event_start: args[:event_start],
        event_end: args[:event_end]
      }

      creator_user = User.find(creator_user_id) || User.find(Discourse::SYSTEM_USER_ID)

      post_creator = PostCreator.new(
        creator_user,
        args.except!(:creator_user_id, :event_start, :event_end)
      )
      post = post_creator.create

      topic = Topic.find(post.topic_id)
      topic.custom_fields = custom_fields
      topic.save!
    end
  end
end

```

should be LOADED in the `after_initialize` block. If this is not done, then the job can not be created. This does not create a topic, instead it simply registers the code as a job that can be enqueued later.

Then, when you actually want to do deferred creation of a topic (for you this is after your AJAX request), you would execute something like this:

```ruby
opts = {
  title: title,
  raw: content,
  category: child_category[:id],
  creator_user_id: creator_user_id,
  event_start: event_start.to_time.to_i,
  event_end: event_end.to_time.to_i
}
Jobs.enqueue(:deferred_topic_creation, opts)

```

I hope that makes things clearer.

---

_[View the full topic](https://meta.discourse.org/t/scheduling-jobs-from-a-plugin/40238)._
