Repository: sidekiq-cron/sidekiq-cron (Ruby)

Issue:
The `date_as_argument` option cannot be changed from `true` to `false`
### Versions

| target | vertions |
| :--- | :--- |
| Ruby | 3.3.3 |
| Rails | 7.1.3.4 |
| sidekiq | 7.3.0 |
| sidekiq-cron | 1.12.0 |

### Summary

The `date_as_argument` option cannot be changed from `true` to `false`.

### Detail

I define the following.

`config/sidekiq_cron.yml`

```yml
send_sidekiq_metrics_job:
  cron: 0 * * * * * # Runs once per minute
  class: SendSidekiqMetricsJob
  date_as_argument: false
```

`config/initializers/sidekiq.rb`

```rb
require 'sidekiq/web'

Sidekiq.configure_server do |config|
  config.redis = {
    url: ENV.fetch('SIDEKIQ_REDIS_URL', ''),
  }

  config.on(:startup) do
    cron = YAML.load_file(File.expand_path('../../sidekiq_cron.yml', __FILE__))
    Sidekiq::Cron::Job.load_from_hash!(cron, source: 'schedule')
  end
end

Sidekiq.configure_client do |config|
  config.redis = {
    url: ENV.fetch('SIDEKIQ_REDIS_URL', ''),
  }
end
```

And when I make the following changes, the changes do not take effect.

① Define `date_as_argument` with false.（When I start sidekiq, it's `false`）

```rb
irb(main):001> Sidekiq::Cron::Job.all
2024-07-12T08:21:23.392Z pid=88304 tid=22es INFO: Sidekiq 7.3.0 connecting to Redis with options {:size=>10, :pool_name=>"internal", :url=>"redis://localhost:6379"}
=>
[#<Sidekiq::Cron::Job:0x0000000135d65e80
  ...
  @date_as_argument=false,
  @klass="SendSidekiqMetricsJob",
  ...
]
```

② `date_as_argument` be changed from false to true（When I start sidekiq, it's `true`）

```rb
irb(main):001> Sidekiq::Cron::Job.all
2024-07-12T08:22:43.534Z pid=88600 tid=22qk INFO: Sidekiq 7.3.0 connecting to Redis with options {:size=>10, :pool_name=>"internal", :url=>"redis://localhost:6379"}
=>
[#<Sidekiq::Cron::Job:0x000000012bcc9260
  ...
  @date_as_argument=true,
  @klass="SendSidekiqMetricsJob",
  ...
]
```

③ `date_as_argument` be changed from true to false（When I start sidekiq, it's still `true`）

```rb
irb(main):001> Sidekiq::Cron::Job.all
2024-07-12T08:23:21.695Z pid=88887 tid=22yb INFO: Sidekiq 7.3.0 connecting to Redis with options {:size=>10, :pool_name=>"internal", :url=>"redis://localhost:6379"}
=>
[#<Sidekiq::Cron::Job:0x0000000132c6e250
  ...
  @date_as_argument=true,
  @klass="SendSidekiqMetricsJob",
  ...
]
```


Discussion:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@satyakampandya Would you mind to take a look at this one? 
@markets I found that once `date_as_argument` is set to `true`, and then we try to set it back to `false`, we're not updating anything in Redis, which is why it stays `true` afterwards. This issue might have been introduced by this [PR](https://github.com/sidekiq-cron/sidekiq-cron/pull/392/files#diff-45ac47a9faa0840538f2cae00fdd31667a33fb6b4f94a7428d9c80bceaf8a87eL407). I have a potential solution in mind for this issue. If you can confirm the issue, I’d be happy to raise a PR with the fix.





Patch:
diff --git a/lib/sidekiq/cron/job.rb b/lib/sidekiq/cron/job.rb
index b40882b3..d85141f6 100644
--- a/lib/sidekiq/cron/job.rb
+++ b/lib/sidekiq/cron/job.rb
@@ -413,7 +413,7 @@ def jid_history_from_redis
 
       # Export job data to hash.
       def to_hash
-        hash = {
+        {
           name: @name,
           namespace: @namespace,
           klass: @klass.to_s,
@@ -421,6 +421,7 @@ def to_hash
           description: @description,
           source: @source,
           args: @args.is_a?(String) ? @args : Sidekiq.dump_json(@args || []),
+          date_as_argument: date_as_argument? ? "1" : "0",
           message: @message.is_a?(String) ? @message : Sidekiq.dump_json(@message || {}),
           status: @status,
           active_job: @active_job ? "1" : "0",
@@ -429,12 +430,6 @@ def to_hash
           last_enqueue_time: serialized_last_enqueue_time,
           symbolize_args: symbolize_args? ? "1" : "0",
         }
-
-        if date_as_argument?
-          hash.merge!(date_as_argument: "1")
-        end
-
-        hash
       end
 
       def errors


Test patch:
diff --git a/test/unit/job_test.rb b/test/unit/job_test.rb
index 90a25efa..99cd15d4 100644
--- a/test/unit/job_test.rb
+++ b/test/unit/job_test.rb
@@ -354,6 +354,49 @@
     end
   end
 
+  describe 'handling date_as_argument' do
+    before do
+      @args = {
+        name: 'Test',
+        cron: '* * * * *',
+        queue: 'default',
+        klass: 'CronTestClass'
+      }
+    end
+
+    it 'sets date_as_argument to true' do
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: true))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal true, stored_job.date_as_argument?
+    end
+
+    it 'sets date_as_argument to false' do
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: false))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal false, stored_job.date_as_argument?
+    end
+
+    it 'updates date_as_argument from true to false' do
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: true))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal true, stored_job.date_as_argument?
+
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: false))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal false, stored_job.date_as_argument?
+    end
+
+    it 'updates date_as_argument from false to true' do
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: false))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal false, stored_job.date_as_argument?
+
+      Sidekiq::Cron::Job.create(@args.merge(date_as_argument: true))
+      stored_job = Sidekiq::Cron::Job.find(@args[:name])
+      assert_equal true, stored_job.date_as_argument?
+    end
+  end
+
   describe '#sidekiq_worker_message' do
     before do
       @args = {
@@ -868,58 +911,6 @@
     end
   end
 
-  # @note sidekiq-cron 1.6.0 cannot process options correctly if any date_as_argument evaluates to true.
-  # This has been tested to resolve issues in environments where multiple sidekiq-cron versions are running when updating from 1.6.0
-  # See https://github.com/sidekiq-cron/sidekiq-cron/issues/350#issuecomment-1409798837 for more information.
-  describe "compat with sidekiq cron 1.6.0" do
-    describe "#to_hash with date_as_argument false" do
-      before do
-        @args = {
-          name: "Test",
-          cron: "* * * * *",
-          klass: "CronTestClass",
-          date_as_argument: false,
-        }
-        @job = Sidekiq::Cron::Job.new(@args)
-      end
-
-      it "should not have date_as_argument property" do
-        assert !@job.to_hash.key?(:date_as_argument)
-      end
-    end
-
-    describe "#to_hash with no date_as_argument option" do
-      before do
-        @args = {
-          name: "Test",
-          cron: "* * * * *",
-          klass: "CronTestClass",
-        }
-        @job = Sidekiq::Cron::Job.new(@args)
-      end
-
-      it "should not have date_as_argument property" do
-        assert !@job.to_hash.key?(:date_as_argument)
-      end
-    end
-
-    describe "#to_hash with date_as_argument" do
-      before do
-        @args = {
-          name: "Test",
-          cron: "* * * * *",
-          klass: "CronTestClass",
-          date_as_argument: true,
-        }
-        @job = Sidekiq::Cron::Job.new(@args)
-      end
-
-      it "should have date_as_argument property with value '1'" do
-        assert_equal @job.to_hash[:date_as_argument], '1'
-      end
-    end
-  end
-
   describe "save" do
     before do
       @args = {