[英]DateTime.now.strftime('Y%m%d%H%M%S') proper string format in Rails
I'm trying to build a reference number for booking in Rails. 我正在尝试建立一个用于在Rails中进行预订的参考号。 I'm using
我正在使用
DateTime.now.strftime('Y%m%d%H%M%S')
for the reference number and concatenate some string. 为参考数字并连接一些字符串。 I used Sidekiq for performing this as a background job but then was confused with the result because it gave a different value in
strftime
. 我使用Sidekiq作为后台作业来执行此操作,但后来却对结果感到困惑,因为它在
strftime
给出了不同的值。
def to_reserve
venue = Venue.find(params[:id])
venue_name = venue.name.delete(' ')
reference_number = ReferencesNumberWorker.perform_in(5.seconds, venue_name)
................
end
Sidekiq: Sidekiq:
class ReferencesNumberWorker
include Sidekiq::Worker
def perform(venue_name)
reference_number = DateTime.now.strftime('Y%m%d%H%M%S')
return venue_name + reference_number
end
end
This is a sample result: 这是一个示例结果:
HotelMatt67fb02cb5da7871f92347df9
which I didn't expect with the right result. 我没想到会得到正确的结果。
That's appending the hash for a Sidekiq job to your reference number where you are expecting it to append the return of the Sidekiq result. 那就是将Sidekiq作业的哈希值附加到您的参考号上,您希望该哈希值附加Sidekiq结果的返回值。
So essentially this method 所以本质上这个方法
def to_reserve
venue = Venue.find(params[:id])
venue_name = venue.name.delete(' ')
reference_number = ReferencesNumberWorker.perform_in(5.seconds, venue_name) <- This line will return Sidekiq job number, not the actual result from Sidekiq perform operation.
................
end
When you define reference number you're assuming it'll give you the Sidekiq return, which it won't. 当您定义参考号时,您会假设它会给您Sidekiq返回值,而不会。 This is the fundamental point of background jobs.
这是后台作业的基本要点。 You're disconnecting the Sidekiq job from what you're doing, ie offloading to a separate process.
您正在断开Sidekiq作业与正在执行的操作的连接,即,将其卸载到一个单独的进程中。 So you can't expect a return, you're offloading the method to Sidekiq.
因此,您不能期望得到回报,而是将方法卸载到Sidekiq。 Sidekiq's return is basically the job number.
Sidekiq的回报基本上是工作编号。
If you want this to work then move the reference number computation that you're doing to Sidekiq in the to_reserve
method OR if it must be done in Sidekiq move all of the logic from to_reserve
to Sidekiq's perform
method. 如果您希望此方法有效,则将您正在执行的参考数字计算通过
to_reserve
方法移至Sidekiq,或者如果必须在Sidekiq中进行,则将所有逻辑从to_reserve
移至Sidekiq的perform
方法。
Also note, to use Sidekiq you should pass (to Sidekiq) the object's id so you can relook it up in Sidekiq and perform some idempotent operation on the object. 还要注意,要使用Sidekiq,您应该将对象的ID传递(传递给Sidekiq),以便可以在Sidekiq中查找它并对该对象执行一些幂等操作。 In my opinion, I don't think in this case you need to use Sidekiq.
我认为,在这种情况下,您不需要使用Sidekiq。 Its unnecessary complexity, but to each their own.
它不必要的复杂性,但是要各有千秋。
Sidenote: Prefer the Time class ( Time#now
vs. DateTime#now
) over DateTime to compute the strftime
method. 旁注:与
DateTime#now
相比,首选Time类( Time#now
与DateTime#now
)来计算strftime
方法。 Its a pure Ruby method and is faster than the DateTime class (a known performance bottleneck). 它是纯Ruby方法,并且比DateTime类(已知的性能瓶颈)要快。 Also your code won't have a hard dependency on Rails.
同样,您的代码对Rails也不会有严格的依赖性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.