完成另一个作业后重新创建作业 [英] Recreate job after another job is completed

查看:80
本文介绍了完成另一个作业后重新创建作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:job1和job2同时进入服务器,并且它们都以状态401返回,这意味着我的令牌访问已到期,需要刷新.我启动了job3,它又返回了新令牌.在这种情况下,我必须根据请求使用新令牌重新创建job1和job2并启动它们. 我有一个jobDispatcher,但在某些情况下似乎无济于事.这是:

I have the following situation: job1 and job2 go to the server in the same time and both of them came back with status 401, which means that my token access has expired and I need to make a refresh. I start job3 which came back the new token. In this case, I have to recreate the job1 and job2 with the new token on request and start them. I have a jobDispatcher, but it seems that it not help me in situation. Here it is :

class JobDispatcher : CoroutineDispatcher() {
  private val queue: Queue<Runnable> = LinkedList()
  private var isPaused: Boolean = false
  private var lastExecutedBlock: Runnable? = null

  @Synchronized
  override fun dispatch(context: CoroutineContext, block: Runnable) {
    if (isPaused) {
      queue.add(block)
    } else {
      thread {
        lastExecutedBlock = block
        block.run()
      }
    }
  }

  @Synchronized
  fun pause() {
    isPaused = true
    if (lastExecutedBlock != null) {
      queue.add(lastExecutedBlock)
      lastExecutedBlock = null
    }
  }

  @Synchronized
  fun resume() {
    isPaused = false
    runQueue()
  }
}

在job3启动之前调用暂停方法,当结果成功时,调用resume方法.问题是job1和job2现在已经完成,现在我应该重新创建它们.是否有可能克隆作业并将其放入队列?

Pause method is called before job3 to start, and when the result is successful, resume method is called. The problem is that job1 and job2 are now completed and now I should recreate them. Is there any possibility to clone the job, and put it in a queue?

我的问题是:哪个是解决此问题的最佳解决方案?使用协程时,我有点迷失了.我的代码非常复杂,然后在这里描述,我只需要一些有关这种情况的准则,以及如何进行管理.协程之间如何通信?

My question is: which is the best solution to solve this? I fell a little lost when working with coroutines. My code is much complicated then I described here, I need just some guidelines for this situation, how to manage it. How to communicate between coroutines?

任何想法都值得欢迎.谢谢!

Any idea is welcome. Thank you!

推荐答案

我将使job1和job2在其开始处获取访问令牌(从redis或其他方式获取访问令牌).然后,如果访问令牌过期,请启动job3以更新访问令牌,然后恢复所有作业. 我不知道您的工作人员和工作调度员是什么样子,但是开源世界中的许多实现都有延迟和重试失败工作的能力.如果您不具备此功能,则必须重新创建作业,而不必担心访问令牌,因为它将在作业开始时获取.

I would make job1 and job2 fetch the access token at the beginning of themselves (fetch the access token from redis or something else). And then if access token expires, start job3 to update the access token, and then resume all the jobs. I don't know how your job worker and job dispatcher are like, but many implementations in the open source world have the ability to delay and retry a failed job. If yours doesn't have this ability, you have to recreate jobs, but no need to worry about the access token, since it will be fetched at the beginning of the job.

简短说明:请勿在源代码中对访问令牌进行硬编码.不要在作业数据中对访问令牌进行编码.只需在需要时获取所需的访问令牌即可.

IN SHORT: Don't hardcode the access token in source code. Don't encode the access token in the job data. Just fetch the needed access token when it is needed.

这篇关于完成另一个作业后重新创建作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆